luôn tiện đây mọi người giúp mình tìm cho mình cái lỗi ở hàm xóa,nó báo lỗi khi xóa phần tử đầu tiên trong dslk,sao lúc debug thì mình thấy nó gán lại head rùi mà khi in ra thì nó báo là truy cập bị lỗi do head=NULL,mọi người giúp mình nhá,đây là đề thi thực hành ctdl c23
Code:
#include"conio.h" #include"stdio.h" #include"stdlib.h" typedef struct toado { int x; int y; }toado; typedef struct node { toado info; struct node *next; }node; typedef struct list { node *head; node *tail; }list; void taolist(list &l) { l.head=NULL; l.tail=NULL; } node *taonode(toado &a) { node *tam; tam=new node; if(tam==NULL) exit(1); else { tam->info=a; tam->next=NULL; } return tam; } void nhaptoado(toado &a) { printf("nhap toa do x:="); scanf("%d",&a.x); printf("nhap toa do y:="); scanf("%d",&a.y); } void themnode(list &l,node *p) { if(l.head==NULL) { l.head=p; l.tail=l.head; } else { l.tail->next=p; l.tail=p; } } void nhapdstoado(list &l) { toado a; int n; printf("nhap so luong diem:="); scanf("%d",&n); for(int i=0;i<n;i++) { printf("nhap toa do diem thu %d: ",i+1); nhaptoado(a); themnode(l,taonode(a)); } } void xoadau(list &l) { node *p; if(l.head!=NULL) { p=l.head; l.head=p->next; delete p; if(l.head==NULL) l.tail=NULL; } else printf("danh sach rong: "); } void xoa(list l,toado b) { node *p,*q; p=l.head; if((p->info.x==b.x)&&(p->info.y=b.y)) { toado c; c=p->info; l.head=p->next; delete p; if(l.head==NULL) l.tail=NULL; } else { while(p!=l.tail) { q=p->next; if((q->info.x==b.x)&&(q->info.y=b.y)) { toado c=q->info; p->next=q->next; delete q; break; } else { p=q; } } if(p=l.tail) printf("Khong tim thay phan tu can xoa:" ); } } void intoado(list &l) { node *tam; tam=l.head; int dem=1; while(tam) { printf("TOA DO DIEM THU %d:(%d,%d)\n",dem,tam->info.x,tam->info.y); tam=tam->next; dem++; } } void hoanvi(toado &a,toado &b) { toado tam; tam=a; a=b;b=tam; } void sapxep(list &l) { node *p,*q; p=l.head; while(p!=l.tail) { q=p->next; while(q) { if(p->info.x>q->info.x) hoanvi(p->info,q->info); q=q->next; } p=p->next; } } void main() { toado b; list l; taolist(l); nhapdstoado(l); intoado(l); printf("nhap toa do can xoa: "); nhaptoado(b); xoa(l,b); /*sapxep(l);*/ intoado(l); getch(); }
Comment