#include #include #include struct book { int code; char name [30]; struct book*pleft; struct book*pright; }; //add function struct book* add(struct book*proot,struct book*pleaf,struct book*pnode) { if (!pleaf) { pnode->pleft=NULL; pnode->pright=NULL; if (proot) { if (pnode->code<=proot->code) proot->pleft=pnode; else { proot->pright=pnode; } return proot; } return pnode; } if(pnode->code<=pleaf->code) {add(pleaf,pleaf->pleft,pnode); } else {add (pleaf,pleaf->pright,pnode); } return proot; } //search function struct book* search(struct book*proot,int code) { while (proot && proot->code!=code) { if (proot->code>=code) {proot=proot->pleft; } else {proot=proot->pright; } }return proot; } // print function void print (struct book*proot) { if (proot) {print(proot->pleft); printf("%d%s",proot->code,proot->name); print(proot->pright); } } // main void main() { clrscr (); int x; struct book*proot=NULL; struct book*pnode; while(x!=000) { printf("\n\t to add a book enter 1\n \ to search for a book enter 2\n \ to print book list enter 3\n\ to Quit enter 000\n"); scanf("%d",&x); switch (x) { case 1: pnode=(struct book*)malloc(sizeof(struct book)); printf("enter book code\n"); scanf("%d",&pnode->code); printf("enter book name\n"); scanf("%s",&pnode->name); proot=add(proot,proot,pnode); break; case 2: struct book*ps; int code; printf("enter code\n"); scanf("%d",&code); ps=search(proot,code); printf("%d%s",ps->code,ps->name); break; case 3: print(proot); break; default: break; } } } /* struct mem*phead=NULL; struct mem*ptail=NULL; struct mem {int code; char name[30]; struct mem*pn; struct mem*pp; }; //Add function void addNode(struct mem*pnode) {if(!phead) {phead=pnode; ptail=pnode; pnode->pn=NULL; pnode->pp=NULL; } else {ptail->pn=pnode; pnode->pp=ptail; ptail=pnode; ptail->pn=NULL; } } // find function struct mem*find(int x) {struct mem*ptr; ptr=phead; while(ptr&&ptr->code!=x) {ptr=ptr->pn; } return ptr; } //delete function int deleteNode (int code) {struct mem*pnode; int delflag=0; pnode=find(code); if (pnode) { if(pnode==phead) {phead=phead->pn; phead->pp=NULL; } else if (pnode==ptail) {ptail=ptail->pp; ptail->pn=NULL; } else {pnode->pn->pp=pnode->pp; pnode->pp->pn=pnode->pn; } free (pnode); delflag=1; } return delflag; }; //insert function void insert (struct mem *pnode) {struct mem *ptrans; if (!phead) {addNode (pnode);} else { ptrans=phead; while(ptrans&&ptrans->codecode) {ptrans=ptrans->pn;} if (ptrans==phead) {pnode->pp=NULL; pnode->pn=phead; phead->pp=pnode; phead=pnode; } else if(!ptrans) {pnode->pp=ptail; pnode->pn=NULL; ptail->pn=pnode; ptail=pnode; } else { pnode->pn=ptrans; pnode->pp=ptrans->pp; pnode->pp->pn=pnode; ptrans->pp=pnode; } } } // print function void printList() {struct mem*ptrans; ptrans=phead; while(ptrans) {printf("%d",ptrans->code); printf("%s",ptrans->name); ptrans=ptrans->pn; } } // free function void freeList () { struct mem*ptrans; while(ptrans) { ptrans=phead; phead=phead->pn; free (ptrans); } } void main() { clrscr (); int x; struct mem*pnode; while(x!=000) { printf(" to add a member enter 1\n \ to find a member enter 2\n \ to delete a member enter 3\n\ to insert a member enter 4\n\ to print member list enter 5\n\ to free member list enter 6\n\ To Quit enter 000\n"); scanf("%d",&x); switch (x) { case 1: pnode=(struct mem*)malloc(sizeof(struct mem)); printf("enter member code\n"); scanf("%d",&pnode->code); printf("enter member name\n"); scanf("%s",&pnode->name); addNode(pnode); break; case 2: int y; printf("Enter Member Code\n\t"); scanf("%d",&y); pnode=find(y); printf("\tMember Code\t%d\n\t",pnode->code); printf("Member Name\t%s\n\t",pnode->name); break; case 3: printf("Enter Member Code\n\t"); scanf("%d",&y); int z= deleteNode (y); if (z==1) {printf("\nMember Deleted");} else {printf("\nList Is Embty");} break; case 4: pnode=(struct mem*)malloc(sizeof(struct mem)); printf("enter member code\n"); scanf("%d",&pnode->code); printf("enter member name\n"); scanf("%s",&pnode->name); insert (pnode); break; case 5: printList (); break; case 6: freeList (); printf("List Freed"); break; default: break; } } } */