// DSTC - Linklst1.h - SIMPLE LINK LIST... // list of operations are //1. list *makelist(void) //2. void addnodeatend(int n,list *l) //3. void addnodeatbegin(int n,list *l) //4. void delnode(list *l,int v) //5. void killlist(list *l) //6. void printlist(list *l) //7. void appendnode(list *l,int v) //8. list *reverselist(list *p) //9. void splitlist(list *l,list *newl,int nd) //10. int countlist(list *l) #include "assert.h"
// defining one node containing the value(info) and pointer to next node // i.e. *next typedef struct linklistnode { int info ; struct linklistnode *next ; } node; typedef struct linklist { node *first; }list; list *makelist(void) { list *l; l = (list *)malloc (sizeof(list)); // printf("\nMaking a list List Size = %d",sizeof(list)); l->first = NULL; return (l); }
// adding a node in the END of the list void addnodeatend(int n,list *l) { node *nd,*t ; nd = (node *)malloc(sizeof(node)); nd->info = n ; nd->next = NULL; if (l->first == NULL) // means list was empty and it is the first node l->first = nd; else { t = l->first; while(t->next !=NULL ) // transversing the whole list to move in END { t = t->next ; } t->next = nd ; // assigning the node's address in the last node *NEXT } } // adds the node in the BEGINING of the list void addnodeatbegin(int n,list *l) { node *nd ; nd = (node *)malloc(sizeof(node)); nd->info = n; nd->next = l->first; l->first = nd ; } // deletes the node with the value of "v" in the list "l", // if not found then gives the message NODE not found // otherwise deletes it and reassign the pointers NEXT void delnode(list *l,int v) { assert(l->first != NULL); node *temp=l->first , *temp1=NULL; if (l->first==NULL) Get Paid by Reading Ads on your Mobiles
printf("\nList not Found"); // when list is empty else { while(temp!=NULL) { if(temp->info==v) // if value is found then break the loop break ; temp1 = temp; // holds the address of the previous node temp = temp1->next ; // moves to the next node } if (temp == l->first) // if the found nodes value is the First Node { // assigning the address to the first of the next node l->first = temp->next ; free(temp); // deleting the found node } else if (temp==NULL) printf("\n Node not Found"); else { // if node is existing in between the list.... temp1->next = temp->next ; free(temp); } } } void killlist(list *l) { node *save,*temp ; save = l->first; while(save) { temp = save->next ; free(save); save = temp; } free(l); } void printlist(list *l) { node *save ; save = l->first ; while (save != NULL) { printf("%d, ",save->info); save = save->next ; } } /* void appendnode(list *l,int v) { node *n,*k ; n=(node *)malloc(sizeof(node)); n->info = v ; n->next = NULL ; if (l->first == NULL) l->first = n ; else { k = l->first ; while (k->next != NULL) { k = k->next ; } k->next = n ; } } */ list *reverselist(list *p) { list *temp ; node *save; temp = makelist(); while (p->first) { save = p->first ; p->first = p->first->next; save->next = temp->first; temp->first = save; } return (temp); } void splitlist(list *l,list *newl,int nd) { assert(l->first != NULL); Get Paid by Reading Ads on your Mobiles
assert(nd!=0); node *temp=l->first , *temp1=NULL; if (l->first==NULL) printf("\nList not Found"); else { while(temp!=NULL ) { temp1=temp; temp = temp->next ; if(temp->info==nd) break ; } newl->first=temp; temp1->next=NULL; } } int countlist(list *l) { node *t = l->first ; int c=0; while (t != NULL) { t = t->next ; c++; } return (c); } |
No comments:
Post a Comment