题目:单链表操作验证(一定改成c语言)
- 提问者网友:寂寞梧桐
- 2021-07-25 05:20
- 五星知识达人网友:摆渡翁
- 2021-07-25 06:58
#include "stdafx.h"
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int Boolean;
typedef int ElemType;
struct LNode
{
ElemType data;
struct LNode *next;
};
typedef struct LNode *LinkList;
Status InitList(LinkList *L)
{
*L=(LinkList)malloc(sizeof(struct LNode));
if(!*L)
exit(OVERFLOW);
(*L)->next=NULL;
return OK;
}
Status DestroyList(LinkList *L)
{
LinkList q;
while(*L)
{
q=(*L)->next;
free(*L);
*L=q;
}
return OK;
}
Status ClearList(LinkList L)
{
LinkList p,q;
p=L->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
L->next=NULL;
return OK;
}
Status ListEmpty(LinkList L)
{
if(L->next)
return FALSE;
else
return TRUE;
}
int ListLength(LinkList L)
{
int i=0;
LinkList p=L->next;
while(p)
{
i++;
p=p->next;
}
return i;
}
Status GetElem(LinkList L,int i,ElemType *e)
{
int j=1;
LinkList p=L->next;
while(p&&j<i)
{
p=p->next;
j++;
}
if(!p||j>i)
return ERROR;
*e=p->data;
return OK;
}
int LocateElem(LinkList L,ElemType e,Status(*compare)(ElemType,ElemType))
{
int i=0;
LinkList p=L->next;
while(p)
{
i++;
if(compare(p->data,e))
return i;
p=p->next;
}
return 0;
}
Status PriorElem(LinkList L,ElemType cur_e,ElemType *pre_e)
{
LinkList q,p=L->next;
while(p->next)
{
q=p->next;
if(q->data==cur_e)
{
*pre_e=p->data;
return OK;
}
p=q;
}
return INFEASIBLE;
}
Status NextElem(LinkList L,ElemType cur_e,ElemType *next_e)
{
LinkList p=L->next;
while(p->next)
{
if(p->data==cur_e)
{
*next_e=p->next->data;
return OK;
}
p=p->next;
}
return INFEASIBLE;
}
Status ListInsert(LinkList L,int i,ElemType e)
{
int j=0;
LinkList p=L,s;
while(p&&j<i-1)
{
p=p->next;
j++;
}
if(!p||j>i-1)
return ERROR;
s=(LinkList)malloc(sizeof(struct LNode));
s->data=e;
s->next=p->next;
p->next=s;
return OK;
}
Status ListDelete(LinkList L,int i,ElemType *e)
{
int j=0;
LinkList p=L,q;
while(p->next&&j<i-1)
{
p=p->next;
j++;
}
if(!p->next||j>i-1)
return ERROR;
q=p->next;
p->next=q->next;
*e=q->data;
free(q);
return OK;
}
Status ListTraverse(LinkList L,void(*vi)(ElemType))
{
LinkList p=L->next;
while(p)
{
vi(p->data);
p=p->next;
}
printf("\n");
return OK;
}
Status comp(ElemType c1,ElemType c2)
{
if(c1==c2)
return TRUE;
else
return FALSE;
}
void visit(ElemType c)
{
printf("%d ",c);
}
int _tmain(int argc, _TCHAR* argv[])
//void main()
{
LinkList L;
ElemType e,e0;
Status i;
int j,k;
i=InitList(&L);
for(j=1;j<=5;j++)
i=ListInsert(L,1,j);
printf("在L的表头依次插入1~5后:L=");
ListTraverse(L,visit);
i=ListEmpty(L);
printf("L是否空:i=%d(1:是 0:否)\n",i);
i=ClearList(L);
printf("清空L后:L=");
ListTraverse(L,visit);
i=ListEmpty(L);
printf("L是否空:i=%d(1:是 0:否)\n",i);
for(j=1;j<=10;j++)
ListInsert(L,j,j);
printf("在L的表尾依次插入1~10后:L=");
ListTraverse(L,visit);
GetElem(L,5,&e);
printf("第5个元素的值为:%d\n",e);
for(j=0;j<=1;j++)
{
k=LocateElem(L,j,comp);
if(k)
printf("第%d个元素的值为%d\n",k,j);
else
printf("没有值为%d的元素\n",j);
}
for(j=1;j<=2;j++)
{
GetElem(L,j,&e0);
i=PriorElem(L,e0,&e);
if(i==INFEASIBLE)
printf("元素%d无前驱\n",e0);
else
printf("元素%d的前驱为:%d\n",e0,e);
}
for(j=ListLength(L)-1;j<=ListLength(L);j++)
{
GetElem(L,j,&e0);
i=NextElem(L,e0,&e);
if(i==INFEASIBLE)
printf("元素%d无后继\n",e0);
else
printf("元素%d的后继为:%d\n",e0,e);
}
k=ListLength(L);
for(j=k+1;j>=k;j--)
{
i=ListDelete(L,j,&e);
if(i==ERROR)
printf("删除第%d个数据失败\n",j);
else
printf("删除的元素为:%d\n",e);
}
printf("依次输出L的元素:");
ListTraverse(L,visit);
DestroyList(&L);
printf("销毁L后:L=%u\n",L);
system("pause");
}