MMwan Newbie
Tổng số bài gửi : 1 Points : 4649 Reputation : 0 Join date : 22/08/2011
| Tiêu đề: nhơ` giúp đơ~ 1 xíu Mon Aug 22, 2011 4:10 pm | |
| code gôc' là của bạn chiên' thi fai? mình chỉ viêt' thêm hàm isertprev insertnext va` ham` deleteprev nhưng không bít mình viê't sai ơ? đâu mà hàm deleteprev ko chạy đúng ai pro giúp mình cái tk nhìu - Code:
-
#include<stdio.h> #include<malloc.h> struct node { int info; struct node *prev; struct node *next; }; typedef struct node *nodeptr; nodeptr newnode() { nodeptr p; p=(nodeptr)malloc(sizeof(struct node)); return p; } void Init(nodeptr *phead) { *phead=NULL; } void freenode(nodeptr p) { free(p); } int IsEmpty(nodeptr phead) { if(phead==NULL) return 1; return 0; } void InsertFirst(nodeptr *phead,int x) { nodeptr p; p=newnode(); p->info=x; if(*phead==NULL) { *phead=p; p->prev=*phead; p->next=*phead; } else { p->prev=(*phead)->prev; (*phead)->prev->next=p; (*phead)->prev=p; p->next=*phead; *phead=p; } } void InserLast(nodeptr *phead,int x) { nodeptr p; p=newnode(); p->info=x; if(*phead==NULL) { *phead=p; p->prev=*phead; p->next=*phead; } else { p->prev=(*phead)->prev; p->next=(*phead)->prev->next; (*phead)->prev->next=p; (*phead)->prev=p; } } void ShowList(nodeptr phead) { nodeptr p; if(phead==NULL) { printf("Danh sach rong\n"); return; } p=phead;
do { printf("%d ",p->info);
p=p->next; } while(p!=phead);
} void DeleteFirst(nodeptr *phead) { nodeptr p; if(*phead==NULL) { printf("Danh sach rong\n"); return; } else if((*phead)->next==*phead) { p=*phead; freenode(p); *phead=NULL; } else { p=*phead; p->prev->next=p->next; p->next->prev=p->prev; (*phead)=(*phead)->next; freenode(p); } } void DeleteLast(nodeptr *phead) { nodeptr p; if(*phead==NULL) { printf("Danh sach rong\n"); return; } else if((*phead)->next==*phead) { p=*phead; freenode(p); *phead=NULL; } else { p=(*phead)->prev; (*phead)->prev=p->prev; p->prev->next=*phead; freenode(p); } } void ShowInvert(nodeptr phead) { nodeptr p; if(phead==NULL) { printf("Danh sach rong\n"); return; }
p=phead; do { p=p->prev; printf("%d ",p->info);
} while(p!=phead);
} void Search(nodeptr phead,int x) { nodeptr p; int vitri=0,found=0; if(phead==NULL) { printf("Danh sach rong\n"); return; } p=phead; do { vitri++; if(p->info==x) { printf("Tim thay o vi tri thu %d\n",vitri); found=1; } p=p->next; } while(p!=phead); if(!found) printf("Khong tim thay\n"); } void Sort(nodeptr phead) { if(phead==NULL) { printf("Danh sach rong\n"); return; } int temp; nodeptr p,q; for(p=phead;p->next!=phead;p=p->next) for(q=p->next;q!=phead;q=q->next) if(p->info>q->info) { temp=p->info; p->info=q->info; q->info=temp; } } void ClearList(nodeptr *phead) { nodeptr last,p; if(*phead==NULL) { printf("Danh sach rong\n"); return; } last=(*phead)->prev; while(*phead!=last) { p=*phead; (*phead)=(*phead)->next; freenode(p); } freenode(last); *phead=NULL; }
int Count(nodeptr phead) { if(phead==NULL) return 0; nodeptr last; int dem=1; last=phead->prev; while(phead!=last) { dem++; phead=phead->next; } return dem; }
void InserPrev(nodeptr *phead, int x, int y) { nodeptr tam; nodeptr p; p= newnode(); p->info = y; if(*phead==NULL) { *phead=p; p->prev=*phead; p->next=*phead; } else { tam=*phead; do { tam= tam->next; } while(&tam!=phead && tam->info !=x); if (tam==*phead) {InsertFirst(phead,y);} else { tam->prev->next=p; p->prev = tam->prev; p->next = tam; tam->prev = p; } } } void InserNext(nodeptr *phead, int x, int y) { nodeptr tam; nodeptr p; p= newnode(); p->info = y; if(*phead==NULL) { *phead=p; p->prev=*phead; p->next=*phead; } else { tam=*phead; do { tam= tam->next; } while (&tam->next != phead && tam->info !=x); } if ( tam == (*phead)->prev) {InserLast(phead,y);} else { p->next = tam->next; (tam->next)->prev = p; tam->next=p; p->prev = tam; } }
void DeletePrev(nodeptr *phead, int x) { nodeptr p,tam; if(*phead==NULL) { printf("Danh sach rong\n"); return; } else if((*phead)->next==(*phead)) { printf("lam gi co nut nao o truoc ma xoa\n"); return; } else { tam=*phead; do {tam= tam->next;} while(&tam!=phead && tam->info !=x); } if ( tam == *phead) {DeleteLast(phead);} else { p = tam->prev; // q= p->prev; tam->prev = q; p->prev->next = tam;
free(p); } }
void menu() { printf("------Menu------\n"); printf("1 - Them 1 nut vao dau danh sach\n"); printf("2 - Them 1 nut vao cuoi danh sach\n"); printf("3 - Hien thi noi dung danh sach\n"); printf("4 - Hien thi noi dung danh sach dao nguoc\n"); printf("5 - Xoa nut dau danh sach\n"); printf("6 - Xoa nut cuoi danh sach\n"); printf("7 - Dem so phan tu cua danh sach\n"); printf("8 - Sap xep danh sach\n"); printf("9 - Tim kiem danh sach\n"); printf("10 - Xoa toan bo danh sach\n"); printf("11 - Them 1 nut vao truoc 1 nut\n"); printf("12 - Them 1 nut vao sau 1 nut\n"); printf("13 - Xoa 1 nut vao truoc 1 nut\n"); printf("0 - Thoat\n"); } void makemenu() { nodeptr head; Init(&head); int chucnang,a,b,c; do { printf("Lua chon chuc nang: "); scanf("%d",&chucnang); switch(chucnang) { case 1: printf("Nhap gia tri can them: "); scanf("%d",&a); InsertFirst(&head,a); break; case 2: printf("Nhap gia tri can them: "); scanf("%d",&a); InserLast(&head,a); break; case 3: ShowList(head); printf("\n"); break; case 4: ShowInvert(head); printf("\n"); break; case 5: DeleteFirst(&head); break; case 6: DeleteLast(&head); break; case 7: printf("So phan tu cua danh sach = %d\n",Count(head)); break; case 8: Sort(head); break; case 9: printf("Nhap vao gia tri can tim: "); scanf("%d",&a); Search(head,a); break; case 10: ClearList(&head); printf("Da xoa toan bo danh sach\n"); break; case 11: printf("Nhap vao gia tri cua nut lam moc: "); scanf("%d",&b); printf("Nhap vao gia tri nut them vao: "); scanf("%d",&c); InserPrev(&head,b,c); break; case 12: printf("Nhap vao gia tri cua nut lam moc: "); scanf("%d",&b); printf("Nhap vao gia tri nut them vao: "); scanf("%d",&c); InserNext(&head,b,c); break; case 13: printf("Nhap vao gia tri cua nut lam moc: "); scanf("%d",&b); DeletePrev(&head,b); break; } } while(chucnang!=0); } main() { menu(); makemenu(); } | |
|