Advertisement
tariq_zaghal

doubly linked list

Apr 30th, 2023
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. struct nodeD{
  6.     int data;
  7.     struct nodeD* next;
  8.     struct nodeD* prev;
  9. };
  10.  
  11. struct nodeD* makeEmpty(struct nodeD* L);
  12. void insertAtFirst(int data_, struct nodeD* L);
  13. void insertAfterPosition(int x,struct nodeD* P, struct nodeD* L);
  14. struct nodeD* findPrevious(int x,struct nodeD* L);
  15. int isLast(struct nodeD* P , struct nodeD* L);
  16. void delete(int x, struct nodeD* L);
  17. struct nodeD* find(int x, struct nodeD* L);
  18. void printList(struct nodeD* L);
  19. void reverse(struct nodeD* L);
  20.  
  21.  
  22. int main(){
  23.  
  24.     struct nodeD* head1 = NULL;
  25.     head1 = makeEmpty(head1);
  26.     insertAtFirst(1,head1);
  27.     insertAtFirst(2,head1);
  28.     insertAtFirst(3,head1);
  29.     insertAtFirst(4,head1);
  30.     insertAtFirst(5,head1);
  31.     insertAtFirst(6,head1);
  32.     printList(head1);
  33.     reverse(head1);
  34.     printList(head1);
  35.  
  36.  
  37.     return 0;
  38. }
  39.  
  40.  
  41. struct nodeD* makeEmpty(struct nodeD* L){
  42.     L = malloc(sizeof(struct nodeD));
  43.     L->next = NULL;
  44.     L->prev = NULL;
  45.  
  46.     return L;
  47. }
  48.  
  49.  
  50. void insertAtFirst(int data_, struct nodeD* L){
  51.     struct nodeD* newNode = malloc(sizeof(struct nodeD));
  52.     newNode->data = data_;
  53.     newNode->next = L->next;
  54.     newNode->prev = L;
  55.      L->next = newNode;
  56.     L->next->prev = newNode;
  57.    
  58.    
  59. }
  60.  
  61. void insertAfterPosition(int x,struct nodeD* P, struct nodeD* L){
  62.     struct nodeD* newNode = malloc(sizeof(struct nodeD));
  63.  
  64.     newNode->data = x;
  65.     newNode->next = P->next;
  66.     newNode->prev = P;
  67.     P->next->prev = newNode;
  68.     P->next = newNode;
  69.  
  70. }
  71.  
  72. struct nodeD* findPrevious(int x,struct nodeD* L){
  73.  
  74.     struct nodeD* p = L;
  75.  
  76.     while(p->next->data!=x && p->next!=NULL){
  77.         p = p->next;
  78.     }
  79.  
  80.     return p;
  81.    
  82. }
  83.  
  84. int isLast(struct nodeD* P , struct nodeD* L){
  85.     return P->next == NULL ;  
  86. }
  87.  
  88.  
  89. void delete(int x, struct nodeD* L){
  90.     struct nodeD* p = findPrevious(x,L);
  91.  
  92.     if(!isLast(p,L)){
  93.         struct nodeD* temp= p->next;
  94.         temp->next->prev = p;
  95.         p->next = temp->next;
  96.         temp->prev = NULL;
  97.         temp->next = NULL;
  98.         free(temp);
  99.     }
  100. }
  101.  
  102.  
  103. struct nodeD* find(int x, struct nodeD* L){
  104.     struct nodeD* p = L;
  105.     p = p->next;
  106.  
  107.     while(p->data!=x && p!=NULL){
  108.             p = p->next;
  109.     }
  110.  
  111.     return p;
  112. }
  113.  
  114.  
  115. void printList(struct nodeD* L){
  116.     struct nodeD* p = L->next;
  117.  
  118.     while(p!=NULL){
  119.         printf("%d   ",p->data);
  120.         p = p->next;
  121.     }
  122.  
  123.     printf(" \n");
  124. }
  125.  
  126.  
  127.  
  128. void reverse(struct nodeD* L){
  129.     struct nodeD* curr  = L->next;
  130.     struct nodeD* prev = NULL;
  131.     struct nodeD* nxt  = curr->next;
  132.  
  133.  
  134.     while(curr!=NULL){
  135.         curr->next = prev;
  136.         curr->prev = nxt;
  137.         prev = curr;
  138.         curr = nxt;
  139.  
  140.         if(nxt!=NULL)
  141.             nxt = nxt->next;
  142.     }
  143.  
  144.     L->next = prev;
  145.     prev->prev = L;
  146.  
  147.     printList(L);
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement