#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 5
#define MAXNAME 30
typedef struct node{
int num;
char name[MAXNAME];
struct node *next;
}linklist;
char get_char(void);
int get_digit(void);
char show_menu(void);
void show(linklist *h);
void add(linklist *h);
void eatline(void);
void findbyname(linklist *h);
void findbynum(linklist *h);
void deletnode(linklist *h);
void sort_bobble_down(linklist *h);
int main(){
int i;
char ch;
linklist *head,*p,*r;
head=(linklist *)malloc(sizeof(linklist));
p=head;
head->next=NULL;
head->num=0;
strcpy(head->name,"head");
r=head;
printf("Now enter data to create %d nodes./n",MAXSIZE-1);
for(i=1;i<MAXSIZE;i++){
p=(linklist *)malloc(sizeof(linklist));
p->num=i;
printf("%d. name please:/n",i);
gets(p->name);
p->next=NULL;
r->next=p;
r=p;
}
puts("Now the linklist was built.");
show(head);
while(1){
ch=show_menu();
if('q'==ch)break;
switch(ch){
case 'a':puts("the linklist is:/n");show(head);break;
case 'b':add(head);break;
case 'c':findbyname(head);break;
case 'd':findbynum(head);break;
case 'e':deletnode(head);break;
case 'f':sort_bobble_down(head);break;
}
}
puts("/n/nbye!/n");
getchar();
return 0;
}
int get_digit(void){
int i;
char ch;
while(scanf("%d",&i)!=1){
while((ch=getchar())!='/n'){
putchar(ch);
puts(" is not a integer!");
}
}
return i;
}
char get_char(void){//取得输入选择
char ch1,ch2;
scanf("%c",&ch1);
while((ch2=getchar())!='/n'){}
while(ch1!='a'&&ch1!='b'&&ch1!='c'&&ch1!='d'&&ch1!='q'&&ch1!='e'&&ch1!='f'){
putchar(ch1);
puts(" is not a letter or is not need,/nplease enter a letter such as a,b,c,d,e and q");
scanf("%c",&ch1);
while((ch2=getchar())!='/n'){}
}
return ch1;
}
char show_menu(void){//显示菜单并返回选择
char ch1;
puts('"enter to continue!");
getchar();
puts("/n/n---------------enter a choice to run:---------------------------/n");
puts(" a.show the linklist b.add a new node");
puts(" c.find a node by name d.find a node by number");
puts(" e.delet a node by number f.sort by bobble down");
puts(" q.quit/n");
puts("---------------------------------------------------------------------");
ch1=get_char();
return ch1;
}
void show(linklist *h){//遍历链表
linklist *p;
p=h;
puts("The content of the linklist is:");
puts("**********************************************************");
puts("number: name:");
while(p!=NULL){
printf(" %d %s/n",p->num,p->name);
p=p->next;
}
puts("/n**********************the end*****************************");
}
void add(linklist *h){//按值添加一个结点
int i;
linklist *p,*r,*n;
p=r=h;
puts("enter the position to be follow.");
puts("the valid positiion is:");
while(p!=NULL){
printf("%d ",p->num);
p=p->next;
}
printf("/n");
i=get_digit();
if(i<0){
puts("Error!");
i=get_digit();
}
eatline();
p=r=h;
while(p!=NULL){
if(p->num==i){
r=p;
p=p->next;
n=(linklist *)malloc(sizeof(linklist));
puts("name please:");
gets(n->name);
puts("number please:");
n->num=get_digit();
r->next=n;
n->next=p;
break;
}
if(p->num>i){
printf("there are no such %d position exist,/n",i);
printf("so it is before number %d./n",p->num);
n=(linklist *)malloc(sizeof(linklist));
puts("name please:");
gets(n->name);
puts("number please:");
n->num=get_digit();
r->next=n;
n->next=p;
break;
}
r=p;
p=p->next;
}
if(i>r->num&&p==NULL){//p=NULL防止在p->num>i的情况下,多余执行此段
printf("the position %d is biggest,/n",i);
printf("so it is after all the nodes./n");
n=(linklist *)malloc(sizeof(linklist));
puts("name please:");
gets(n->name);
puts("number please:");
n->num=get_digit();
r->next=n;
n->next=NULL;
}
puts("-----Done,now show the linklist------");
eatline();
show(h);
}
void findbyname(linklist *h){
linklist *p;
char temp[MAXNAME];
p=h;
puts("enter the name that you want to find:");
gets(temp);
while(p!=NULL){
if(strcmp(temp,p->name)==0){
puts("------found!------");
printf("name %s the number is %d/n",p->name,p->num);
break;
}
p=p->next;
}
if(p==NULL){
puts("------not found!--------");
printf("such a name %s is not exist!/n",temp);
}
}
void findbynum(linklist *h){
int i;
linklist *p;
p=h;
puts("enter the number that you want to find:");
i=get_digit();
eatline();
while(p!=NULL){
if(i==p->num){
puts("------found!------");
printf("number %d the name is %s/n/n",p->num,p->name);
break;
}
p=p->next;
}
if(p==NULL){
puts("------not found!--------");
printf("such a number %d is not exist!/n",i);
}
}
void deletnode(linklist *h){
int i;
bool find;
linklist *p,*r;
p=h;
find=false;
puts("enter the number you want to delet!");
puts("the valid number is:");
while(p!=NULL){
printf("%d ",p->num);
p=p->next;
}
printf("/n");
i=get_digit();
eatline();
if(i==0){
puts("the head node can not be deleted!");
i=get_digit();
eatline();
}
p=r=h;
while(p!=NULL){
if(p->num==i){
printf("now delet the node,the number is %d .../n",p->num);
p=p->next;
r->next=p;
puts("delet!");
show(h);
find=true;
break;
}
r=p;
p=p->next;
}
if(!find){
puts("Can not delet!");
printf("such a number %d is not exist!/n",i);
}
}
void sort_bobble_down(linklist *h){//用逆冒泡法对链表根据num进行排序,即重的下沉。
linklist *p,*q,*r,*temp,*pre;
bool change;
change=false;
p=q=r=pre=h;
while(r!=NULL){
pre=h;
q=pre->next;
if(q!=NULL)p=q->next;
if(q==NULL)break;
while(p!=NULL){
change=false;
if(q->num>p->num){
pre->next=p;
temp=p->next;
p->next=q;
q->next=temp;
change=true;
if(q==NULL)break;
}
if(change){
pre=pre->next;
p=q->next;
}
if((!change)&&(p!=NULL)){
pre=pre->next;
q=q->next;
p=p->next;
}
}
r=r->next;
}
puts("sort by bobble down!");
puts("---done!---");
show(h);
}
void eatline(void){
char ch;
while((ch=getchar())!='/n'){continue;}
}