Points:
- 循环菜单程序,main中利用while(1)设置死循环
- 初始化一个空链表
- 创建链表
- 首插法增加新结点
- 删除结点
- 输出链表长度
- 输出链表元素
- 按结点数据域排序
- 查找结点
- 销毁链表
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
char data;
struct node *next;
}LNode;
void InitList(LNode **head){
(*head)=NULL;
}
void InitList2(LNode **head){
(*head)=(LNode*)malloc(sizeof(LNode));
if((*head)==NULL)
;
(*head)->next=NULL;
}
void CreateList(LNode**h){
LNode *p;
char e;
printf("Input node data(end with '#'):");
e=getchar();
while(e!='#'){
p=(LNode*)malloc(sizeof(LNode));
if(p==NULL)
;
p->data=e;
p->next=(*h);
(*h)=p;
e=getchar();
}
}
void CreateList2(LNode *h){
}
void InsertNode(LNode* *h,int i,char e){
LNode *p,*q;
p=(*h);
q=(LNode*)malloc(sizeof(LNode));
if(q==NULL)
;
q->data=e;
if(i==1){
q->next=(*h);
(*h)=q;
return ;
}
for(i=i-2;i;i--)
p=p->next;
q->next=p->next;
p->next=q;
}
void DeleteNode(LNode* *h,int i){
LNode *p=(*h),*q;
if(i==1){
(*h)=p->next;
free(p);
return ;
}
for(i=i-2;i;i--)
p=p->next;
q=p->next;
p->next=q->next;
free(q);
}
int ListLength(LNode *h){
int count=0;
LNode *p=h;
while(p!=NULL){
count++;
p=p->next;
}
return count;
}
void DispList(LNode *h){
LNode *p=h;
while(p!=NULL){
printf("%c ",p->data);
p=p->next;
}
printf("\n");
}
void InsertionSortList(LNode* *h){
LNode *s,*tmp,*pre,*p;
s=(LNode*)malloc(sizeof(LNode));
if(s==NULL)
;
s->next=(*h);
p=(*h)->next;
s->next->next=NULL;
while(p!=NULL){
pre=s;
tmp=s->next;
while((tmp!=NULL)&&(tmp->data)<(p->data)){
tmp=tmp->next;
pre=pre->next;
}
(*h)=p->next;
p->next=tmp;
pre->next=p;
p=(*h);
}
(*h)=s->next;
free(s);
printf("over\n");
}
void Search(LNode *h,int n){
LNode *s=h;
int count=1;
while(count!=n){
count++;
s=s->next;
}
printf("%c\n",s->data);
}
void DestroyList(LNode* *h){
LNode *p=(*h);
while(p!=NULL){
p=p->next;
free(*h);
(*h)=p;
}
}
void Menu(){
printf(" --- Single Linked List ---\n");
printf("1.Init list 2.Creat list\n");
printf("3.Insert node 4.Delete node\n");
printf("5.Length 6.Display list\n");
printf("7.Sort list 8.Search\n");
printf("0.Destroy list & exit\n");
printf("Input your choice:");
}
int main(){
LNode *head=NULL;
int i,choice;
char e,t,n;
while(1){
Menu();
scanf("%d",&choice);
getchar();
switch(choice){
case 1:
InitList(&head);
break;
case 2:
CreateList(&head);
break;
case 3://InsertNode
printf("Input Location(1~%d):",ListLength(head)+1);
scanf("%d",&i);
if(i>(ListLength(head)+1))
;
printf("Input data:");
t=getchar();
scanf("%c",&e);
InsertNode(&head,i,e);
break;
case 4://DeleteNode
printf("Input Location(1~%d):",ListLength(head));
scanf("%d",&i);
if(i>(ListLength(head)+1))
;
DeleteNode(&head,i);
break;
case 5://ListLength
printf("the length of list is:%d\n",ListLength(head));
break;
case 6://DispList
DispList(head);
break;
case 7://InsertionSortList
InsertionSortList (&head);
break;
case 8://Search
printf("Input Location(1~%d):",ListLength(head));
scanf("%d",&n);
if(n<=0)
printf("error\n");
else
Search(head,n);
break;
case 0:
DestroyList(&head);
exit(0);
break;
default:
printf("Input error,retry\n");
break;
}
}
return 0;
}