ขอถามเรื่อง doubly link list.c
Doubly linklist แบบให้มีเมนูดังนี้ครับ
1 insert 2 delete 3 print ข้อมูลจากน้อยไปมาก 4 print ข้อมูลจากมากไปน้อย 5 find (ค้นหาข้อมูลถ้าเจอ พิมพ์ yes ไม่เจอ พิมพ์ no 6 exit
โค้ดที่ผมมีเป็นแบบนี้ครับ ช่วยแก้ให้หน่อยนะครับ
#include<stdio.h> #include<conio.h> struct node { int data; struct node *next,*prev; }; int main() { struct node *head=NULL,*tail=NULL,*p,*a,*tmp; int find,cas; while(1) { printf("++++++++++Menu++++++++++\n"); printf("1 Insert\n"); printf("2 Delete\n"); printf("3 Print\n"); printf("4 Exit\n"); printf("\nPlease choose-> "); scanf("%d",&cas); switch(cas) { case(1): { int choice; printf("\n++++++++++Menu Insert++++++++++\n"); printf(" 1 Insert before all list\n"); printf(" 2 Insert after all list\n"); printf(" 3 Insert by you find\n"); printf(" Please choose your choice-> "); scanf("%d",&choice); if(choice==1) { p=(struct node*)malloc(sizeof(struct node)); printf("Input number -> "); scanf("%d",&p->data); p->next=NULL; p->prev=NULL; if(head==NULL) { head=p; tail=p; } else { tmp=head; while(tmp->prev!=NULL) tmp=tmp->prev; p->prev=NULL; p->next=tmp; tmp->prev=p; head=p; } } else if(choice==2) { p=(struct node*)malloc(sizeof(struct node)); printf("Input number -> "); scanf("%d",&p->data); p->next=NULL; p->prev=NULL; if(head==NULL) { head=p; tail=p; } else { tmp=head; while(tmp->next!=NULL) tmp=tmp->next; p->next=NULL; p->prev=tmp; tmp->next=p; } } else if(choice==3) { int find,a; printf("Enter number you want to find:"); scanf("%d",&find); tmp=head; while(tmp->next!=NULL) { if(find==tmp->data) { break; } else tmp=tmp->next; } printf("1 Enter before %d\n",tmp->data); printf("2 Enter after %d\n",tmp->data); printf("please choose:"); scanf("%d",&a); if(a==1) { p=(struct node*)malloc(sizeof(struct node)); printf("Input number -> "); scanf("%d",&p->data); tmp->prev->next=p; p->prev=tmp->prev; p->next=tmp; tmp->prev=tmp; } else if(a==2) { p=(struct node*)malloc(sizeof(struct node)); printf("Input number -> "); scanf("%d",&p->data); tmp->next->prev=p; p->next=tmp->next; p->prev=tmp; tmp->next=p; } } } break; case(2): { printf("Enter number you want to delete:"); scanf("%d",&find); tmp=head; while(tmp->next!=NULL) { if(find==tmp->data) { tmp->next->prev=tmp->prev; tmp->prev->next=tmp->next; free(tmp); break; } else { tmp=tmp->next; } } break; } case(3): { a=head; while(a) { printf("%d ",a->data); a=a->next; } printf("\n"); break; } case(4): { exit(0); } } } getch(); }
|