#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; } } } */ // this is my code , check out the borrowBook and the returnBook functions . BTW I am not sure this works, it just compiles fine. #include #include //#include #define NLEN 30 struct book { int isbn; char name[NLEN]; char auther[NLEN]; char publisher[NLEN]; struct user *hasMe; struct book *left; struct book *right; }; struct user { int id; char name[NLEN]; char address[NLEN]; struct book *borrowed; struct user *next; struct user *prev; }; struct user *head; struct user *tail; void addToList (struct user *p) { if (!head) { head = p; tail = p; p->next = NULL; p->prev = NULL; } else { tail->next = p; p->prev = tail; p->next = NULL; tail = p; } } struct user *searchInList(int x) { struct user *ptr; ptr = head; while (ptr && ptr->id != x) ptr = ptr->next; return (ptr); } int deleteFromList(int id) { struct user *ptr; int delFlag = 0; ptr = searchInList(id); if (ptr) { if (ptr == head) { head = head->next; head->prev = NULL; } else if (ptr == tail) { tail = tail->prev; tail->next = NULL; } else { ptr->next->prev = ptr->prev; ptr->prev->next = ptr->next; } free(ptr); delFlag = 1; } return delFlag; } int borrowBook(struct user *u, struct book *b) { if (u->borrowed) return 1; else if (b->hasMe) return 2; else { u->borrowed = b; b->hasMe = u; return 0; } } void returnBook(struct user *u, struct book *b) { u->borrowed = NULL; b->hasMe = NULL; } struct book *addToTree (struct book *root, struct book *leaf, struct book *p) { if (!leaf) { p->left = NULL; p->right = NULL; if (root) { if (p->isbn <= root->isbn) root->left = p; else root->right = p; return root; } return p; } if (p->isbn <= leaf->isbn) addToTree (leaf, leaf->left, p); else addToTree (leaf, leaf->right, p); return root; } struct book *searchInTree ( struct book *root, int x) { while (root && root->isbn != x) { if (root->isbn >= x) root = root->left; else root= root->right; } return root; } int main () { //clrscr(); struct book *p; struct book *root; struct user *u; int x, y, z; while (1) { printf("Please select an operation:\n\n"); printf("\t1.Add a book\n\t2.Find a book\n\t3.Borrrow a book\n\t4.Add a user\n\t5.Find a user\n\t6.Delete a user\n\t7.Return a book\n\t8.Exit\n"); printf("Selection: "); scanf("%d", &y); switch (y) { case 1: // add a book p = (struct book *) malloc (sizeof(struct book)); printf ("Enter the ISBN: "); scanf("%d", &p->isbn); printf("Enter the book name: "); scanf("%s", p->name); printf("Enter the auther name: "); scanf("%s", p->auther); printf("Enter the publisher: "); scanf("%s", p->publisher); p->hasMe = NULL; root = addToTree(root, root, p); break; case 2: // find a book printf("Enter the ISBN: "); scanf("%d", &x); p = searchInTree (root, x); if (!p) printf("\nBook not found!\n"); else printf("\n->Code: %d\n->Name: %s\n->Auther: %s\n->Publisher:%s", p->isbn, p->name, p->auther, p->publisher); break; case 3: // borrow a book printf("Enter the book's ISBN: "); scanf("%d", &x); printf("Enter the user's ID: "); scanf("%d", &z); if(borrowBook (searchInList (z), searchInTree (root, x))) printf("Book is not available or user already has a book!\n"); else printf("done!\n"); break; case 4: // add a user u = (struct user *) malloc (sizeof(struct user)); printf("Enter the user ID: "); scanf("%d", &u->id); printf("Enter the user name: "); scanf("%s", u->name); printf("Enter the user address: "); scanf("%s", u->address); u->borrowed = NULL; addToList (u); break; case 5: // find a user printf("Enter the user ID: "); scanf("%d", &x); u = searchInList (x); if (!u) printf("User not found!\n"); else printf("->ID: %d\n->Name: %s\n->Address: %s\n", u->id, u->name, u->address); case 6: //delete a user printf("Enter the user ID: "); scanf("%d", &x); if (!deleteFromList (x)) printf("Can't delete user!\n"); break; case 7: // return a book printf("Enter the book's ISBN: "); scanf("%d", &x); printf("Enter the user's ID: "); scanf("%d", &z); returnBook (searchInList (z), searchInTree (root, x)); break; case 8: return (0); break; default: printf("Invalid operation!\n"); break; } } return 0; }