#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
typedef int elemtype;
struct lnode
{
elemtype data;
struct lnode *next;
};
struct lnode *head;
setnull()
{
head->next=NULL;
}
int n=0;
creat()/*创建一个单链表*/
{
struct lnode *p1,*p2;
p1=p2=head;
printf("input 0 to stop creat !/n");
while(1)
{
printf("the %d lnode:",n+1);
p1=(struct lnode *)malloc(sizeof(struct lnode));
scanf("%d",&p1->data); if(n==0)
head=p1;
p2->next=p1;p2=p1;
if(p1->data==0)break;
n++;
}
p1->next=NULL;
printf("the created lnode is:/n");
print();
}
del()/*删除第i 个元素*/
{
int flag=1,deletenum;
struct lnode *p,*q;
printf("please input the place you want to delete:/n");
scanf("%d",&deletenum);
p=head;
if(p==NULL)
printf("list null!/n");
else if(deletenum==1)
head=p->next;
else
{
while(p->next!=NULL&&flag<deletenum)
{
flag++;
q=p;
p=p->next;
}
if(deletenum==flag)
{q->next=p->next;free(p);n--;}
else printf("Position error!/n");
}
print();
}
deleting()/*删除从第i个位置的k个元素*/
{
int i,k,place=1;
struct lnode *p,*pla;
pla=p=head;
printf("please input the delet lnode from i to k.i and k__");
scanf("%d%d",&i,&k);
while((p!=NULL)&&(place<i))
{
p=p->next;
pla=p;
place++;
}
if(i>length()&&i<1)
printf("i error!/n");
else
{
if((i+k)>length())
printf("k is too much large!");
else
{
while(k)
{
pla=pla->next;
k--;
n--;
}
/*while(p!=pla){free(p);p=p->next;}*/
if(i==1)
head=pla;
else
p->next=pla->next;
}
}
printf("the lnode has been delet k lnodes:/n");
print();
}
stat()/*统计数值为一个数的结点的个数*/
{
struct lnode *p;
elemtype x;
int i=0;
p=head;
printf("please input the data you want to stat:__");
scanf("%d",&x);
while(p!=NULL)
{
if(p->data==x)
i++;
p=p->next;
}
printf("the %d node in the lnode is %d",x,i);
}
insert()/*在第i个位置插入一个值为x的元素*/
{
int k=1,i;
struct lnode *p,*s;
elemtype x;
printf("Please input the insert data x and position i:/n");
scanf("%d%d",&x,&i);
p=head;
s=(struct lnode *)malloc(sizeof(struct lnode));
s->data=x;s->next=NULL;
if(i==1)
{head=s;s->next=p;}
else
{
while((p->next!=NULL)&&(k!=i-1))
{
p=p->next;
k++;
}
if(k==i-1)
{
s->next=p->next;
p->next=s;
n++;
}
else
printf("position error!/n");
}
print();
}
sort()/*对链表进行排序*/
{
struct lnode *p,*t,*q;
elemtype temp;
p=head;
while(p->next!=NULL)
{
t=p->next;
q=p;
while(t->next!=NULL)
{
if(q->data<t->data)
q=t;t=t->next;
}
if(p!=q)
{
temp=p->data;p->data=q->data;q->data=temp;
}
p=p->next;
}
printf("the lnode has been sorted:/n");
print();
}
int length()/*返回链表长度*/
{
int k=0;
struct lnode *p;
p=head;
while(p!=NULL)
{
k++;
p=p->next;
}
return(k);
}
arrange()/*倒置*/
{
int i,a[100];
struct lnode *p;
p=head;
for(i=0,p=head;p!=NULL;i++,p=p->next)
a[i]=p->data;
for(i=length()-2,p=head;p!=NULL;i--,p=p->next)
p->data=a[i];
printf("the lnode has been arrangeed:/n");
print();
}
insertsort()/*向有序链表中插入一个数,使其仍然有序*/
{
struct lnode *p,*s,*t;
elemtype x;
printf("Please input the insert data x:");
scanf("%d",&x);
p=head;
s=(struct lnode *)malloc(sizeof(struct lnode));
s->data=x;s->next=NULL;
while((p!=NULL)&&(p->data>x))
{t=p;p=p->next;}
if(p==head)
{
head=s;
s->next=p;
}
else
{
s->next=t->next;
t->next=s;
}
n++;
printf("the new data has been insert!/n");
print();
}
find()/*查找值为x的元素的个数*/
{
int i=1;
elemtype x;
struct lnode *p;
printf("please input the data you want to find:");
scanf("%d",&x);
p=head;
while(p->next!=NULL)
{
if(p->data==x)
printf("Find! the %d data is int the %d place!",x,i);
p=p->next;
i++;
}
if(p==NULL)printf("the %d data is not in the lnode!/n",x);
}
change()/*把值为x的元素改变为c*/
{
struct lnode *p;
elemtype x,c;
p=head;
printf("please input the data you want to change and the change number:");
scanf("%d%d",&x,&c);
while(p!=NULL)
{
if(p->data==x)
p->data=c;
p=p->next;
}
printf("the change lnode is below:/n");
print();
}
print()/*打印链表*/
{
struct lnode *p;
p=head;
printf("/n");
if(p==NULL)
printf("list null/n");
else
{printf("the %d lnode is below:/nhead->",n);
while(p->next!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
printf("NULL/n");}
}
main()/*主函数*/
{
int place;
char order;
printf("order list:/ncreat lnode:c/ndel node:d:/ninsert node:i/nbreak:0/nprint lnode:p/nsort lnode:s/n");
printf("daozhi lnode:a/n");
printf("insert a number to a sorted lnode:x/n");
printf("find number:f/nchange number:w/n");
printf("delete the k node from i:e/nstat the x numbers:t/n");
while(1)
{
printf("/nexit please input the 0!input the order:__");
order=getchar();
if(order=='0')
break;
switch(order)
{
case 'c':creat();break;
case 'd':del();break;
case 'i':insert();break;
case 'p':print();break;
case 's':sort();break;
case 'a':arrange();break;
case 'x':insertsort();break;
case 'f':find();break;
case 'w':change();break;
case 'e':deleting();break;
case 't':stat();break;
/* default:printf("you input an error order!please input again!/n");
continue;*/
}
}
}