#include "stdio.h"
typedef struct linknode
{
int data;
struct linknode *next;
}nodetype;
/*建立单链表,由用户输入各节点data域值,以0表示输入结束*/
nodetype *InitList()
{
int d;
nodetype *h=NULL,*s,*t;
int i=1;
printf("Create a link/n");
while(1)
{
printf("Enter the %d node's data :",i);/*建立一个单链表*/
scanf("%d",&d);
if(d==0) break;
if(i==1) /*建立第一个节点*/
{
h=(nodetype *)malloc(sizeof(nodetype));
h->data=d;h->next=NULL;t=h;
}
else /*建立其余节点*/
{
s=(nodetype *)malloc(sizeof(nodetype));
s->data=d;s->next=NULL;t->next=s;
t=s;
}
i++;
}
return h;
}
void disp(nodetype *h) /*输出由h指向的单链表的所有data域值*/
{
nodetype *p=h;
printf("Output the link:/n");
if(p==NULL) printf("The link is empty");
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("/n");
}
int ListLength(nodetype *h)/*返回单链表的长度*/
{
int i=0;
nodetype *p=h;
while (p!=NULL)
{
p=p->next;i++;
}
return i;
}
nodetype *GetElem(nodetype *h,int i)/*返回第i个节点的指针*/
{
nodetype *p=h;
int j=1;
if(i>ListLength(h)||i<=0)
{
printf("Error node!");
return 0;/*i上溢或下溢*/
}
else
{
while (p!=NULL&&j<i) /*查找第i个节点 并由p指向该节点*/
{
j++;p=p->next;
}
return p;
}
}
nodetype *ListInsert(nodetype *h,int i,int x) /*在单链表head中第i个节点之后插入一个data域为x的节点*/
{
nodetype *p,*s;
s=(nodetype *)malloc(sizeof(nodetype));
s->data=x; s->next=NULL;
if (i==0)
{
s->next=h; h=s;
}
else
{
p=GetElem(h,i);
if (p!=NULL)
{
s->next=p->next;
p->next=s;
}
else printf("Error value/n");/*输入i不正确*/
}
return h;
}
nodetype *ListDelete(nodetype *h,int i) /*删除第i个节点*/
{
nodetype *p=h,*s;
if(i==1)
{
h=h->next;
free(p);
}
else
{
p=GetElem(h,i-1);
if(p!=NULL&&p->next!=NULL)
{
s=p->next;
p->next=s->next;
free(s);
}
else printf("Error value /n");
}
return h;
}
int ListEmpty(nodetype *h)
{
if(h->next==NULL)
return 1;
else
return 0;
}
nodetype *ClearList(nodetype *L)
{
nodetype *p,*q;
p=L; /* p指向第一个结点 */
while(p->next!=NULL) /* 没到表尾 */
{
q=p->next;
free(p);
p=q;
}
L->next=NULL; /* 头结点指针域为空 */
return L;
}
void PriorElem(nodetype *h,int i)
{
nodetype *p;
int e;
if(i<2||i>ListLength(h))
{
printf("Error!/n");
exit(0);;
}
p=GetElem(h,i-1);
e=p->data;
printf("The prior elemet is: %d",e);
printf("/n");
printf("/n");
}
void main()
{
nodetype *h;
int i,j,k,l,m,n;
/*选择功能*/
while(1)
{
printf("#===============================================================#/n");
printf("# 1.Create a new chain #/n");
printf("# 2.Output the link #/n");
printf("# 3.Insert data #/n");
printf("# 4.Delete node #/n");
printf("# 5.search node #/n");
printf("# 6.Judge whether the list is empty #/n");
printf("# 7.Clear the list #/n");
printf("# 8.Search the prior element #/n");
printf("# 9.exit #/n");
printf("#===============================================================#/n");
printf("Input a data from 1-9:/n");
scanf("%d",&i);
if(i<1||i>9)
{
printf("Input error! Input 1-9:");
scanf("%d",&i);
}
if(i==1) h=InitList();
if(i==2) disp(h);
if(i==3)
{
printf("/nEnter the node:");
scanf("%d",&j);
printf("/nEnter a data:");
scanf("%d",&k);
h=ListInsert(h,j,k);
}
if(i==4)
{
printf("/nEnter a node:");
scanf("%d",&l);
h=ListDelete(h,l);
}
if(i==5)
{
printf("/nEnter the location you want to find:");
scanf("%d",&m);
h=GetElem(h,m);
printf("The data is:%d",h->data);
printf("/n/n");
}
if(i==6)
{
if(ListEmpty(h))
printf("The list is empty!/n");
else
printf("No empty!/n");
}
if(i==7) ClearList(h);
if(i==8)
{
printf("/nEnter the node so to find the prior element!");
scanf("%d",&n);
PriorElem(h,n);
}
if(i==9) break;
}
}
数据结构 单链表运用 C语言
最新推荐文章于 2024-04-30 00:45:27 发布