- #include <stdio.h>
- #include <stdlib.h>
- #include <windows.h>
- #define N 5
- typedef struct LNode
- {
- int data;
- struct LNode *next;
- }LNode,*LinkList;
- //**********************函数部分**************************
- int CreateList(LinkList L,int n) //尾插法
- {
- int ShowList(LinkList L);
- int i;
- char m;
- LinkList p,s,q;
- if(L->next!=NULL)
- {
- loop:printf("链表已存在,是否放弃当前链表创建新链表?(y/n)");
- m=getchar();
- if(m=='y')
- {
- for(p=L,q=L->next;q->next!=NULL;)
- {
- p=q;
- q=q->next;
- free(p);
- }
- free(q);
- L->next=NULL;//重新初始化
- CreateList(L,n);
- return 1;}//覆盖已有链表
- if(m=='n') return 0;
- else goto loop;
- }
- L->next = NULL;
- p=L;
- for(i=0;i<n;i++)
- {
- system("cls");
- s=(LinkList) malloc (sizeof(LNode));
- ShowList(L);
- printf("开始输入第%d个数:",i+1);
- scanf("%d",&s->data);
- s->next = NULL;
- p->next = s;
- p = s;
- }
- return 1;
- }
- int InsertList(LinkList L,int i,int e)//把e插入第i的位置
- {
- LinkList p,q;
- int j;
- p=L; //取头
- for(j=0;p&&j<i-1;j++) p=p->next; //取到第i个的地址
- if(!p||j>i-1) {printf("链表为空或者插入位置不对!\\n");system("pause");return 0;}
- q = (LinkList) malloc (sizeof(LNode)); //生成新结点
- q->data=e;
- q->next=p->next;
- p->next=q;
- return 1;
- }
- int LengthList(LinkList L)
- {
- int i=0;
- LinkList p;
- for(p=L;p->next!=NULL;p=p->next,i++)
- return i;
- }
- void LocateList(LinkList L,int e,int *p)
- {
- int i,j=0;
- LinkList s;
- for(s=L->next,i=0;s!=NULL;s=s->next,i++)
- if(s->data==e){ p[j]=i+1; j++; }
- p[j]='\\0';
- }
- int ShowList(LinkList L)//查看
- {
- LinkList p;
- p=L;
- if(L->next==NULL) {printf("链表为空!!!\\n"); return 0;}
- for(;p->next!=NULL;)
- {
- p=p->next;
- printf("%-5d",p->data);
- }
- printf("\\n");
- return 1;
- }
- int ChangeList(LinkList L) //转置
- {
- int length,cup,i,j;
- LinkList p,q;
- if(L->next==NULL) {printf("链表为空!!!\\n"); return 0;}
- for(length=0,q=L;q->next!=NULL;length++)
- q=q->next; //得到链表长度
- for(i=0,p=L->next;i<length/2;i++)
- {
- for(j=0,q=L;j<length-i;j++)
- q=q->next; //得到后交换结点
- cup=p->data;p->data=q->data;q->data=cup;
- p=p->next;
- }
- return 1;
- }
- int DeleteList(LinkList L,int e) //删除结点
- {
- LinkList p,cup;
- if(L->next==NULL) {printf("链表为空!!!\\n"); return 0;}
- for(p=L;p->next!=NULL;)
- {
- cup=p->next;
- if(cup->data==e)
- {
- p->next=cup->next;
- free(cup);
- continue;
- }
- p=p->next;
- }
- return 0;
- }
- int DeleteSame(LinkList L)
- {
- LinkList p,cup,q;
- if(L->next==NULL) {printf("链表为空!!!\\n"); return 0;}
- for(p=L; p->next!=NULL ;)//被对比对象;
- {
- p=p->next;
- for(cup=p->next,q=p; cup!=NULL ;)//对比对象;
- {
- if(p->data == cup->data)
- {
- q->next=cup->next;
- free(cup);
- cup=q->next;
- continue;
- }
- cup=cup->next;
- q=q->next;
- }
- }
- return 1;
- }
- int BubbleSort(LinkList L)
- {
- int cup,length,i,j,change;
- LinkList p,q;
- if(L->next==NULL) {printf("链表为空!!!\\n"); return 0;}
- for(p=L,length=0;p->next!=NULL;p=p->next)
- length++; //取到长度;
- for(i=length,change=1;i>1&&change;i--)
- {
- change=0;
- for(j=0,p=L->next,q=L->next; j<i && q->next!=NULL ;j++)
- {
- q=q->next;
- if(p->data > q->data)
- {
- cup=p->data;p->data=q->data;q->data=cup;
- change=1;
- }
- p=p->next;
- }
- }
- return 1;
- }
- int SumSame(LinkList L,int e) //统计相同元素
- {
- int n;
- LinkList p;
- if(L->next==NULL) {printf("链表为空!!!\\n"); return 0;}
- for(p=L,n=0;p->next!=NULL;)
- {
- p=p->next;
- if(p->data==e) n++; //计数
- }
- return n;
- }
- //*********************************************************
- void menu(LinkList L,int m)
- {
- int i,e,data[N];
- char s='r',w='y';
- if(m==-1)
- {
- loop:system("cls");
- printf(" Welcome to vell001!!!\\n\\n");
- printf("1. 从键盘输入N个整数,创建新链表\\n");
- printf("2. 输出链表至屏幕\\n");
- printf("3. 查找值为某个整数的元素的位序\\n");
- printf("4. 在链表中插入一个元素\\n");
- printf("5. 删除链表中某个元素\\n");
- printf("6. 逆序排列链表表中的数据元素\\n");
- printf("7. 合并相同元素\\n");
- printf("8. 给顺序表排序\\n");
- printf("9. 统计链表中的某个元素的个数\\n");
- printf("0. 退出\\n");
- printf("\\n你选择:");
- scanf("%d",&m);
- if(m<-1||m>9) goto loop;
- }
- if(m==1)
- {
- while(s=='r')
- {
- system("cls");
- CreateList(L,N);
- loop1:system("cls");
- ShowList(L);
- printf("\\n重新开始 请按'R'键\\n返回menu 请按'M'键\\n直接退出 请按'Q'键\\n");
- s=getch();
- if(s=='r') continue;
- if(s=='m') {menu(L,-1); break;}
- if(s=='q') {menu(L,0); break;}
- else goto loop1;
- }
- }
- if(m==2)
- {
- while(s=='r')
- {
- loop2:system("cls");
- printf(" Welcome to vell001!!!\\n\\n");
- ShowList(L);
- printf("\\n重新开始 请按'R'键\\n返回menu 请按'M'键\\n直接退出 请按'Q'键\\n");
- s=getch();
- if(s=='r') continue;
- if(s=='m') {menu(L,-1); break;}
- if(s=='q') {menu(L,0); break;}
- else goto loop2;
- }
- }
- if(m==3)
- {
- while(s=='r')
- {
- system("cls");
- printf(" Welcome to vell001!!!\\n\\n");
- printf("\\n输入你想查找的元素:");
- scanf("%d",&e);
- LocateList(L,e,data);
- loop3:system("cls");
- printf("元素%d的位置有:\\n",e);
- for(i=0;data[i]!='\\0';i++)
- printf("%-5d",data[i]);
- printf("\\n重新开始 请按'R'键\\n返回menu 请按'M'键\\n直接退出 请按'Q'键\\n");
- s=getch();
- if(s=='r') continue;
- if(s=='m') {menu(L,-1); break;}
- if(s=='q') {menu(L,0); break;}
- else goto loop3;
- }
- }
- if(m==4)
- {
- while(s=='r')
- {
- system("cls");
- printf(" Welcome to vell001!!!\\n\\n");
- ShowList(L);
- printf("\\n依次输入你想插入的元素和位置:");
- scanf("%d%d",&e,&i);
- InsertList(L,i,e);
- loop4:system("cls");
- ShowList(L);
- printf("\\n重新开始 请按'R'键\\n返回menu 请按'M'键\\n直接退出 请按'Q'键\\n");
- s=getch();
- if(s=='r') continue;
- if(s=='m') {menu(L,-1); break;}
- if(s=='q') {menu(L,0); break;}
- else goto loop4;
- }
- }
- if(m==5)
- {
- while(s=='r')
- {
- system("cls");
- printf(" Welcome to vell001!!!\\n\\n");
- ShowList(L);
- printf("\\n输入你想删除的元素:");
- scanf("%d",&e);
- DeleteList(L,e);
- loop5:system("cls");
- printf(" Welcome to vell001!!!\\n\\n");
- ShowList(L);
- printf("\\n重新开始 请按'R'键\\n返回menu 请按'M'键\\n直接退出 请按'Q'键\\n");
- s=getch();
- if(s=='r') continue;
- if(s=='m') {menu(L,-1); break;}
- if(s=='q') {menu(L,0); break;}
- else goto loop5;
- }
- }
- if(m==6)
- {
- while(s=='r')
- {
- system("cls");
- printf(" Welcome to vell001!!!\\n\\n");
- printf("转置前:\\n");
- ShowList(L);
- system("pause");
- ChangeList(L);
- loop6:system("cls");
- ShowList(L);
- printf("\\n重新开始 请按'R'键\\n返回menu 请按'M'键\\n直接退出 请按'Q'键\\n");
- s=getch();
- if(s=='r') continue;
- if(s=='m') {menu(L,-1); break;}
- if(s=='q') {menu(L,0); break;}
- else goto loop6;
- }
- }
- if(m==7)
- {
- while(s=='r')
- {
- system("cls");
- printf(" Welcome to vell001!!!\\n\\n");
- printf("合并前:\\n");
- ShowList(L);
- system("pause");
- DeleteSame(L);
- loop7:system("cls");
- printf(" Welcome to vell001!!!\\n\\n");
- printf("合并后:\\n");
- ShowList(L);
- printf("\\n重新开始 请按'R'键\\n返回menu 请按'M'键\\n直接退出 请按'Q'键\\n");
- s=getch();
- if(s=='r') continue;
- if(s=='m') {menu(L,-1); break;}
- if(s=='q') {menu(L,0); break;}
- else goto loop7;
- }
- }
- if(m==8)
- {
- while(s=='r')
- {
- system("cls");
- printf(" Welcome to vell001!!!\\n\\n");
- printf("排序前:\\n");
- ShowList(L);
- system("pause");
- BubbleSort(L);
- loop8:system("cls");
- printf(" Welcome to vell001!!!\\n\\n");
- printf("排序后:\\n");
- ShowList(L);
- printf("\\n重新开始 请按'R'键\\n返回menu 请按'M'键\\n直接退出 请按'Q'键\\n");
- s=getch();
- if(s=='r') continue;
- if(s=='m') {menu(L,-1); break;}
- if(s=='q') {menu(L,0); break;}
- else goto loop8;
- }
- }
- if(m==9)
- {
- while(s=='r')
- {
- system("cls");
- printf(" Welcome to vell001!!!\\n\\n");
- printf("请输入你想要统计的元素:");
- scanf("%d",&e);
- i=SumSame(L,e);
- loop9:system("cls");
- printf("元素%d有%d个。。。\\n",e,i);
- printf("\\n重新开始 请按'R'键\\n返回menu 请按'M'键\\n直接退出 请按'Q'键\\n");
- s=getch();
- if(s=='r') continue;
- if(s=='m') {menu(L,-1); break;}
- if(s=='q') {menu(L,0); break;}
- else goto loop9;
- }
- }
- if(m==0)
- {
- system("cls");
- printf("\\n\\n\\n\\n 谢谢使用!!!\\n\\n\\n vell001");
- Sleep(1000);
- }
- }
- int main(void)
- {
- LinkList L;
- L=(LinkList) malloc (sizeof(LNode));//得到一个链表;
- L->next=NULL;
- menu(L,-1);
- }
- //该片段来自于http://www.codesnippet.cn/detail/081120137008.html
来源: http://www.codesnippet.cn/detail/081120137008.html