Write a library for a generic doubly linked list(void pointers to handle all dat
ID: 3634463 • Letter: W
Question
Write a library for a generic doubly linked list(void pointers to handle all data types) containing a head and a tail pointer pointing to the first and last nodes. the node structure is as follows:typedef struct llnode {
void *data;
struct llnode *prev;
struct llnode *next;
} LLNode;
the list structure is as follows:
typedef struct list {
LLNode *head;
LLNode *tail;
} List;
your library should contain the following functions:
InitializeList
Takes in: Nothing. Returns a List structure with head and tail initialized to NULL
AddToFront and AddToTail
Takes in: Address of a list structure and a pointer to the data to be added.
Delete
Takes in: Address of a list structure, a pointer to target data that defines what is to be deleted and a comparison function. Also an int called the delete all flag. If set to zero we delete the first occurence of the target. if we set it to 1 we delete all that meet the target criteria.
Find
Takes in: Address of a list structure, a pointer to target data that defines what is to be found and a comparison function. Returns: Pointer to desired node if found, Null if not.
Explanation / Answer
#include #include struct node { struct node *prev; int info; struct node *next; }*start; main() { int choice,n,m,po,i; start=NULL; while(1) { printf("1.Create List "); printf("2.Add at begining "); printf("3.Add after "); printf("4.Delete "); printf("5.Display "); printf("6.Count "); printf("7.Reverse "); printf("8.exit "); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("How many nodes you want : "); scanf("%d",&n); for(i=0;iinfo=num; tmp->next=NULL; if(start==NULL) { tmp->prev=NULL; start->prev=tmp; start=tmp; } else { q=start; while(q->next!=NULL) q=q->next; q->next=tmp; tmp->prev=q; } }/*End of create_list()*/ addatbeg(int num) { struct node *tmp; tmp=malloc(sizeof(struct node)); tmp->prev=NULL; tmp->info=num; tmp->next=start; start->prev=tmp; start=tmp; }/*End of addatbeg()*/ addafter(int num,int c) { struct node *tmp,*q; int i; q=start; for(i=0;inext; if(q==NULL) { printf("There are less than %d elements ",c); return; } } tmp=malloc(sizeof(struct node) ); tmp->info=num; q->next->prev=tmp; tmp->next=q->next; tmp->prev=q; q->next=tmp; }/*End of addafter() */ del(int num) { struct node *tmp,*q; if(start->info==num) { tmp=start; start=start->next; /*first element deleted*/ start->prev = NULL; free(tmp); return; } q=start; while(q->next->next!=NULL) { if(q->next->info==num) /*Element deleted in between*/ { tmp=q->next; q->next=tmp->next; tmp->next->prev=q; free(tmp); return; } q=q->next; } if(q->next->info==num) /*last element deleted*/ { tmp=q->next; free(tmp); q->next=NULL; return; } printf("Element %d not found ",num); }/*End of del()*/ display() { struct node *q; if(start==NULL) { printf("List is empty "); return; } q=start; printf("List is : "); while(q!=NULL) { printf("%d ", q->info); q=q->next; } printf(" "); }/*End of display() */ count() { struct node *q=start; int cnt=0; while(q!=NULL) { q=q->next; cnt++; } printf("Number of elements are %d ",cnt); }/*End of count()*/ rev() { struct node *p1,*p2; p1=start; p2=p1->next; p1->next=NULL; p1->prev=p2; while(p2!=NULL) { p2->prev=p2->next; p2->next=p1; p1=p2; p2=p2->prev; /*next of p2 changed to prev */ } start=p1; }/*End of rev()*/Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.