/*动态单链表的结构体定义*///定义结构体typedefint ElemType;typedefstruct LNode
{
ElemType data;//数据域struct LNode* next;//指针域}LNode,*LinkList;/*动态单链表的12个基本操作和2个辅助函数声明*/
Status InitList(LinkList* L);
Status DestoryList(LinkList* L);
Status ListEmpty(LinkList L);
Status ClearList(LinkList L);intListLength(LinkList L);
Status GetElem(LinkList L,int i, ElemType* e);intLocateElem(LinkList L, ElemType e,Status(*compare)(ElemType, ElemType));
Status PriorElem(LinkList L, ElemType cur_e, ElemType* pre_e);
Status NextElem(LinkList L, ElemType cur_e, ElemType* next_e);
Status ListInsert(LinkList L,int i, ElemType e);
Status ListDelete(LinkList L,int i, ElemType* e);
Status ListTraverse(LinkList L, Status (*visit)(ElemType));
Status compare(ElemType x, ElemType y);
Status visit(ElemType);
函数定义
/*动态单链表的12个基本操作函数定义*/
Status InitList(LinkList* L){*L =(LinkList)malloc(sizeof(LNode));if(!(*L))return ERROR;//空间分配失败(*L)->next =NULL;//指针域为空return OK;}
Status DestoryList(LinkList *L){
LinkList p;while(*L)//当*L为空时循环结束{
p =(*L)->next;//p指向下一个结点free(*L);//释放*L*L = p;}return OK;}
Status ListEmpty(LinkList L){if(L->next)return FALSE;elsereturn TRUE;}
Status ClearList(LinkList L)//不改变头结点和主调函数中的头指针,使用一级指针即可{
LinkList p, q;
p = L->next;//p指向链表第一个结点while(p)//当p指向空时循环结束{
q = p->next;free(p);
p = q;}
L->next =NULL;//头结点指针域为空return OK;}intListLength(LinkList L){int i =0;//计数
LinkList p = L->next;//p指向第一个元素while(p){
i++;
p = p->next;}return i;}
Status GetElem(LinkList L,int i, ElemType* e){
LinkList p = L->next;//p指向头结点后的第一个节点int j =1;//计数器while(p && j < i)//当j = i时,p指向的内容即为所求,结束循环{
p = p->next;
j++;}if(!p || j > i)return ERROR;//当p为空或者i不符合范围时*e = p->data;//获取第i个元素return OK;}intLocateElem(LinkList L, ElemType e,Status(*compare)(ElemType, ElemType)){int i =1;
LinkList p = L->next;while(p &&!(*compare)(p->data, e)){
i++;
p = p->next;}if(!p)return0;elsereturn i;}
Status PriorElem(LinkList L, ElemType cur_e, ElemType* pre_e){
LinkList p = L->next;//p指向第一个结点
LinkList q;//q为p的后继while(p->next)//当p指向的结点的下一个结点为空时循环结束{
q = p->next;if(q->data == cur_e)//若e与q的数据域相等{*pre_e = p->data;//p的数据域赋值给*prereturn OK;}
p = p->next;//p后移}return INFEASIBLE;}
Status NextElem(LinkList L, ElemType cur_e, ElemType* next_e){
LinkList p;
p = L->next;while(p->next)//当p指向的结点的下一个结点为空时循环结束{if(p->data == cur_e){*next_e = p->next->data;//将p下一个元素数据域赋值给*nextreturn OK;}
p = p->next;}return INFEASIBLE;}
Status ListInsert(LinkList L,int i, ElemType e)//在带头节点单链表中第i个元素之前插入元素e{
LinkList p = L;int j =0;//计数器while(p && j < i -1)//当p为空或者j=i-1时结束循环,此时p指向第i-1个结点{
p = p->next;
j++;}if(!p || j > i -1)return ERROR;//当p为空//创建一个新的节点储存e
LinkList s =(LinkList)malloc(sizeof(LNode));if(!s)return ERROR;
s->data = e;//连接
s->next = p->next;//先后连
p->next = s;//在前挂return OK;}
Status ListDelete(LinkList L,int i, ElemType* e){
LinkList p = L;int j =0;//计数器//当第i个结点为空,或者j = i-1时结束循环,此时p指向第i-1个结点while(p->next && j < i -1){
p = p->next;
j++;}if(!(p->next)|| j > i -1)return ERROR;
LinkList q = p->next;//q保存删除结点地址
p->next = p->next->next;//删除*e = q->data;//e存放删除节点的数据free(q);//释放return OK;}
Status ListTraverse(LinkList L, Status (*visit)(ElemType)){
LinkList p = L->next;while(p)//当p为空时结束循环{(*visit)(p->data);//每一个数据都进行visit函数操作,例如visit可以是打印函数
p = p->next;}printf("\n");return OK;}
测试
intmain(){//初始化链表
LinkList L;if(InitList(&L))printf("初始化成功!\n");elseprintf("初始化失败!\n");//判表空if(ListEmpty(L))printf("空!\n");elseprintf("非空!\n");//表长printf("表长为:%d\n",ListLength(L));//插入元素
ElemType i;for(i =1; i <=8; i++){if(ListInsert(L,1, i))printf("头插入成功:%d\n", i);elseprintf("头插入失败!\n");}//判表空if(ListEmpty(L))printf("空!\n");elseprintf("非空!\n");//表长printf("表长为:%d\n",ListLength(L));//遍历ListTraverse(L, visit);//取元素if(GetElem(L,2,&i))printf("取第2个元素:%d\n", i);elseprintf("获取失败!\n");//定位元素int n =LocateElem(L,2, compare);if(i !=0)printf("与2相关的元素是第%d个\n", n);elseprintf("定位失败!\n");
n =LocateElem(L,9, compare);if(n !=0)printf("与9相关的元素是第%d个\n", n);elseprintf("定位失败!\n");//前后继元素if(PriorElem(L,2,&i))printf("2的前元素:%d\n", i);elseprintf("获取失败!\n");if(PriorElem(L,8,&i))printf("8的前元素:%d\n", i);elseprintf("获取失败!\n");if(NextElem(L,2,&i))printf("2的后元素:%d\n", i);elseprintf("获取失败!\n");if(NextElem(L,1,&i))printf("1的后元素:%d\n", i);elseprintf("获取失败!\n");//删除if(ListDelete(L,1,&i))printf("删除第%d个元素:%d\n",1, i);elseprintf("删除失败!\n");if(ListDelete(L,3,&i))printf("删除第%d个元素:%d\n",3, i);elseprintf("删除失败!\n");//表长printf("表长为:%d\n",ListLength(L));//遍历ListTraverse(L, visit);//清空if(ClearList(L))printf("清空成功!\n");elseprintf("清空失败!\n");//判表空if(ListEmpty(L))printf("空!\n");elseprintf("非空!\n");//表长printf("表长为:%d\n",ListLength(L));//遍历ListTraverse(L, visit);//销毁链表if(DestoryList(&L))printf("销毁成功!\n");elseprintf("销毁失败!\n");return0;}
Status compare(ElemType x, ElemType y){if(x == y)return OK;elsereturn ERROR;}
Status visit(ElemType e){printf("%d ", e);return OK;}