单链表程序例子

单链表的程序例子。他首先让计算机随机产生一批整数,接着用表头插入结点的方法建立一个单链表,然后从该单链表中删除所有元素值重复的结点,最后利用该单链表建立一个有序单链表。

#include <stdio.h>
#include <stdlib.h>
#define NN 12
#define MM 20
typedef int ElemType;
struct sNode
{
ElemType data;
struct sNode* next;
};
void InitList(struct sNode** HL)
{
*HL=NULL;
}
void ClearList(struct sNode** HL)
{
struct sNode *cp,*np;
cp=*HL;
while(cp!=NULL)
{
np=cp->next;
free(cp);
cp=np;
}
}
int SizeList(struct sNode*  HL)
{
int i=0;
while(HL!=NULL)
{
i++;
HL=HL->next;
}
return i;
}
int EmptyList(struct sNode* HL)
{
if(HL==NULL) return 1;
else return 0;
}
ElemType GetElem(struct sNode* HL,int pos)
{
int i=0;
if(pos<1)
{
printf("pos值非法,退出运行!\n");
exit(1);
}
while (HL!=NULL)
{
i++;
if(i==pos) break;
HL=HL->next;
}
if(HL!=NULL)
return HL->data;
else{
printf("pos值非法,退出运行!\n");
exit(1);
}
}
void TraverseList(struct sNode* HL)
{
while(HL!=NULL)
{
printf("%5d",HL->data);
HL=HL->next;
}
printf("\n");
}
ElemType* FindList(struct sNode* HL,ElemType x)
{
while(HL!=NULL)
if(HL->data==x)
return &HL->data;
else HL=HL->next;
return NULL;
}
int UpdatePosLise(struct sNode* HL,int pos,ElemType x)
{
int i=0;
struct sNode* p=HL;
while(p!=NULL)
{
i++;
if(pos==i) break;
else p=p->next;
}
if(pos==i)
{
p->data=x;
return 1;
}
else return 0;
}
void InsertFirstList(struct sNode** HL,ElemType x)
{
struct sNode *newp;
newp=(struct sNode*)malloc(sizeof(struct sNode));
if(newp==NULL)
{
printf("内存空间用完,退出运行!\n");
exit(1);
}
newp->data=x;
newp->next=*HL;
*HL=newp;
}
void InsertLastList(struct sNode** HL,ElemType x)
{
struct sNode *newp;
newp=(struct sNode*)malloc(sizeof(struct sNode));
if(newp==NULL)
{
printf("内存动态空间用完,退出运行!\n");
exit(1);
}
newp->data=x;
newp->next=NULL;
if(*HL==NULL)  *HL=newp;
else
{
struct sNode* p=*HL;
while(p->next!=NULL) p=p->next;
p->next=newp;
}
}
int InsertPosList(struct sNode**HL,int pos,ElemType x)
{
int i=0;
struct sNode *newp;
struct sNode* cp=*HL,*ap=NULL;
if(pos<=0)
{
printf("pos值不正确,返回0表示插入失败!\n");
return 0;
}
while(cp!=NULL)
{
i++;
if(pos==i) break;
else
{
ap=cp;
cp=cp->next;
}
}
newp=(struct sNode*)malloc(sizeof(struct sNode));
if(newp==NULL)
{
printf("内存动态空间用完,无法插入!\n");
return 0;
}
newp->data=x;
if(ap==NULL)
{
newp->next=cp;
*HL=newp;
}
return 1;
}
void InsertOrderList(struct sNode** HL,ElemType x)
{
struct sNode* cp=*HL,*ap=NULL;
struct sNode *newp;
newp=(struct sNode*)malloc(sizeof(struct sNode));
if(newp==NULL)
{
printf("内存动态空间用完,退出运行!\n");
exit(1);
}
newp->data=x;
if(cp==NULL  ||  x<cp->data)
{
newp->next=cp;
*HL=newp;
return;
}
while(cp!=NULL)
{
if(x<cp->data) break;
else {ap=cp;cp=cp->next;}
}
newp->next=cp;
ap->next=newp;
}
ElemType DeleteFirstList(struct sNode** HL)
{
ElemType temp;
struct sNode* p=*HL;
if(*HL==NULL)
{
printf("单链表为空,无表头删除,退出运行!\n");
exit(1);
}
*HL=(*HL)->next;
temp=p->data;
free(p);
return temp;
}
ElemType DeleteLastList(struct sNode** HL)
{
ElemType temp;
struct sNode* cp=*HL;
struct sNode* ap=NULL;
if(cp==NULL)
{
printf("单链表为空,无表尾删除,退出运行!\n");
exit(1);
}
while(cp->next!=NULL)
{
ap=cp;
cp=cp->next;
}
if(ap==NULL)
*HL=(*HL)->next;
else
ap->next=NULL;
temp=cp->data;
free(cp);
return temp;
}
ElemType DelePosList(struct sNode** HL,int pos)
{
int i=0;
ElemType temp;
struct sNode* cp=*HL;
struct sNode* ap=NULL;
if(cp==NULL  ||   pos<=0)
{
printf("单链表为空或pos值不正确,退出运行!\n");
exit(1);
}
while(cp!=NULL)
{
i++;
if(i==pos)  break;
ap=cp;
cp=cp->next;
}
if(cp==NULL)
{
printf("pos值不正确,退出运行!\n");
exit(1);
}
if(pos==1)
*HL=(*HL)->next;
else
ap->next=cp->next;
temp=cp->data;
free(cp);
return temp;
}
int DeleteValueList(struct sNode** HL,ElemType x)
{
struct sNode* cp=*HL;
struct sNode* ap=NULL;
while(cp!=NULL)
{
if(cp->data==x) break;
ap=cp;
cp=cp->next;
}
if(cp==NULL) return 0;
if(ap==NULL)
*HL=(*HL)->next;
else
ap->next=cp->next;
free(cp);
return 1;
}
void main()
{
int a[NN];
int i;
struct sNode *p,*h,*s;
InitList(&p);
for(i=0;i<NN;i++)  a[i]=rand()%MM;
printf("随机数序列:");
for(i=0;i<NN;i++) printf("%5d",a[i]);
printf("\n");
printf("随机数逆序:");
for(i=0;i<NN;i++)  InsertFirstList(&p,a[i]);
TraverseList(p);
printf("单链表长度:%d\n",SizeList(p));
for(h=p;h!=NULL;h=h->next)
while(DeleteValueList(&(h->next),h->data));
printf("去除重复数:");
TraverseList(p);
printf("单链表长度:%d\n",SizeList(p));
h=NULL;
for(s=p;s!=NULL;s=s->next)
InsertOrderList(&h,s->data);
printf("有序表序列:");
TraverseList(h);
ClearList(&p);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值