点击打开链接此处是线性表的顺序存储 代码案例,但是顺序存储插入和删除操作需要移动大量元素,当线性表长度较大时难以确定。故选择用链式存储,代码测试如下所示:
测试框架:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"linklist.h"
using namespace std;
struct Teacher
{
LinkListNode node;
int age;
char name[64];
};
void main()
{
int i = 0, len = 0;
Linklist* list = NULL;
list = List_Create();
if (list==NULL)
{
return;
}
Teacher t1, t2, t3, t4, t5;
t1.age = 20;
strcpy(t1.name,"wang1");
t2.age = 30;
strcpy(t2.name, "wang2");
t3.age = 40;
strcpy(t3.name, "wang3");
t4.age = 50;
strcpy(t4.name, "wang4");
t5.age = 60;
strcpy(t5.name, "wang5");
len = List_Length(list);
List_Inster(list,(LinkListNode*)&t1,0);
List_Inster(list, (LinkListNode*)&t2, 0);
List_Inster(list, (LinkListNode*)&t3, 0);
List_Inster(list, (LinkListNode*)&t4, 0);
List_Inster(list, (LinkListNode*)&t5, 0);
//遍历
for ( i = 0; i < List_Length(list); i++)
{
Teacher* tmp = (Teacher*)List_Get(list,i);
if (tmp==NULL)
{
return;
}
printf("teacher->age :%d\n",tmp->age);
printf("teacher->name :%s\n", tmp->name);
}
//删除链表
while (List_Length(list)>0)
{
Teacher* tmp = (Teacher*)List_Delete(list,0);
if (tmp==NULL)
{
return;
}
printf("teacher->age :%d\n", tmp->age);
}
List_Destory(list);
system("pause");
}
头文件:
#pragma once
#ifdef _MYLIST_H_
#define _MYLIST_H_
#endif // _MYSEQLIST_H_
typedef void Linklist;
typedef struct LinkListNode
{
struct LinkListNode* next;
};
/*创建列表*/
Linklist * List_Create();
/*释放列表*/
void List_Destory(Linklist* list);
/*清空列表 回到初始状态*/
void List_Clear(Linklist* list);
/*获取链表的长度*/
int List_Length(Linklist* list);
/*获取指定位置的链表的元素数据*/
LinkListNode* List_Get(Linklist* list, int pos);
/*插入元素数据*/
int List_Inster(Linklist* list, LinkListNode* node, int pos);
/*元素数据删除*/
LinkListNode* List_Delete(Linklist* list, int pos);
头文件的实现:
#include"linklist.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct TLinkList
{
LinkListNode header;
int lenght;
};
/*创建列表*/
Linklist * List_Create()
{
TLinkList* ret = NULL;
ret = (TLinkList*)malloc(sizeof(TLinkList));
memset(ret,0,sizeof(TLinkList));
ret->lenght = 0;
ret->header.next = NULL;
return ret;
}
/*释放列表*/
void List_Destory(Linklist* list)
{
if (list==NULL)
{
free(list);
list = NULL;
}
return ;
}
/*清空列表 回到初始状态*/
void List_Clear(Linklist* list)
{
TLinkList *tlist = NULL;
if (list==NULL)
{
return;
}
tlist = (TLinkList*)list;
tlist->lenght = 0;
tlist->header.next = NULL;
return ;
}
/*获取链表的长度*/
int List_Length(Linklist* list)
{
TLinkList* tlist = NULL;
if (list == NULL)
{
return 0;
}
tlist = (TLinkList*)list;
return tlist->lenght;
}
/*获取指定位置的链表的元素数据*/
LinkListNode* List_Get(Linklist* list, int pos)
{
int i = 0;
if (list==NULL||pos<0)
{
return NULL;
}
LinkListNode* current = NULL;
TLinkList* tlist = NULL;
tlist = (TLinkList*)list;
current = &(tlist->header);
//让辅助指针指向链表的头部
for ( i = 0; i < pos&&(current->next!=NULL); i++)
{
current = current->next;
}
return current->next;
}
/*插入元素数据*/
int List_Inster(Linklist* list, LinkListNode* node, int pos)
{
if (list == NULL || pos<0||node==NULL)
{
return 0;
}
int i = 0, ret = 0;
TLinkList* tlist = NULL;
tlist = (TLinkList*)list;
LinkListNode* current = NULL;
current = &(tlist->header);
for ( i = 0; i < pos&&(current->next!=NULL); i++)
{
current = current->next;
}
node->next = current->next;
current->next = node;
tlist->lenght++;
return 0;
}
/*元素数据删除*/
LinkListNode* List_Delete(Linklist* list, int pos)
{
int i = 0;
if (list == NULL || pos<0 )
{
return 0;
}
TLinkList* tlist = NULL;
LinkListNode* current = NULL;
tlist = (TLinkList*)list;
current = &(tlist->header);
LinkListNode* ret = NULL;
for ( i = 0; i <pos&&(current->next!=NULL); i++)
{
current = current->next;
}
ret = current->next;
current->next = ret->next;
tlist->lenght--;
return ret;
}