Posts

Showing posts from March, 2021

#21_Doubly linked list

  #include   <stdio.h> #include   <stdlib.h> //Doubly linked list struct   node {      int  data;      struct   node   * prev;      struct   node   * next; }; void   traverse ( struct   node   * head ) {      struct   node   * ptr  =   head ;      while  (ptr->next  !=   NULL )     {          printf ( " %d  " ,  * ptr);         ptr  =  ptr->next;     }      printf ( " %d  " ,  * ptr);      while  (ptr->prev  !=   NULL )     {          printf ( " %d  " ,  * ptr);         ptr  =  ptr->prev;     }      printf ( " %d   \n " ,  * ptr); } struct   node   * insertion_1 ( struct   node   * head ,  int   data ) {  //Insertion of node at head in doubly linked list      struct   node   * newNode  =  ( struct   node   * ) malloc ( sizeof ( struct   node ));     newNode->data  =   data ;     newNode->next  =   head ;      head ->prev  =  newNode;      head   =  newNode;     newNode->prev  =   NULL ;      return   head ; } s

#20_Circular linked list

  #include   <stdio.h> #include   <stdlib.h> //Circular linked list struct   node {      int  data;      struct   node   * next; }; void   traverse ( struct   node   * head ); struct   node   * insertion_1 ( struct   node   * head ,  int   data );             //Insertion at first struct   node   * insertion_2 ( struct   node   * head ,  int   data );             //Insertion at end struct   node   * insertion_3 ( struct   node   * head ,  int   data ,  int   index );  //Insertion in between struct   node   * deletion_1 ( struct   node   * head );                        //Deletion at first struct   node   * deletion_2 ( struct   node   * head );                        //Deletion at end struct   node   * deletion_3 ( struct   node   * head ,  int   index );             //Deletion in between int   main () {      struct   node   * head;      struct   node   * second;      struct   node   * third;      struct   node   * fourth;     head  =  ( struct   node   * ) malloc ( sizeof (

#18_Deletion of a node in a linked list

  #include   <iostream> using   namespace   std ; //Deletion of a node in a linked list struct   node {      int  data;      struct   node   * next; }; void   linked_List_Traverse ( struct   node   * head ) {      while  ( head   !=   NULL )     {          printf ( " %d  " ,  * head );          head   =   head ->next;     }      printf ( " \n " ); } struct   node   * Deletion_1 ( struct   node   * head ) {  //Deletion at the beginning of linked list      head   =   head ->next;      return   head ; } struct   node   * Deletion_2 ( struct   node   * head ,  int   NodeNum ) {  //Deletion in between of linked list      struct   node   * p  =   head ;      struct   node   * p2  =   head ->next;      int  i  =   0 ;      while  (i  !=  ( NodeNum   -   2 ))     {         p  =  p->next;         p2  =  p2->next;         i ++ ;     }     p->next  =  p2->next;      delete  (p2);      return   head ; } struct   node   * Deletion_3 ( struct   node   *

#16_Insertion in a linked list

  #include   <stdio.h> #include   <stdlib.h> //Insertion in a linked list struct   node {      int  data;      struct   node   * next; }; void   linked_List_Traverse ( struct   node   * head ) {      while  ( head   !=   NULL )     {          printf ( " %d  " ,  * head );          head   =   head ->next;     }      printf ( " \n " ); } struct   node   * Insertion_1 ( struct   node   * head ,  int   data ) {  //Insertion of a node at the beginning of a linked list      struct   node   * ptr  =  ( struct   node   * ) malloc ( sizeof ( struct   node ));     ptr->data  =   data ;     ptr->next  =   head ;      head   =  ptr;      return   head ; } struct   node   * Insertion_2 ( struct   node   * head ,  int   data ,  int   index ) {  //Insertion of a node in between the linked list      struct   node   * NewNode  =  ( struct   node   * ) malloc ( sizeof ( struct   node ));      struct   node   * ptr  =   head ;      int  i  =   0 ;      while  (i 

#14_Linked list creation and deletion

  #include   <stdio.h> #include   <stdlib.h> //Linked list creation and deletion struct   node {      int  data;      struct   node   * next; }; void   linked_List_Traverse ( struct   node   * head ) {      while  ( head   !=   NULL )     {          printf ( " %d  " ,  * head );          head   =   head ->next;     }      printf ( " \n " ); } int   main () {      struct   node   * head;      struct   node   * first;      struct   node   * second;      struct   node   * third;      struct   node   * fourth;     head  =  ( struct   node   * ) malloc ( sizeof ( struct   node ));     first  =  ( struct   node   * ) malloc ( sizeof ( struct   node ));     second  =  ( struct   node   * ) malloc ( sizeof ( struct   node ));     third  =  ( struct   node   * ) malloc ( sizeof ( struct   node ));     fourth  =  ( struct   node   * ) malloc ( sizeof ( struct   node ));     head->data  =   34 ;     head->next  =  first;     first->data  =   0 ;     fir