fork(1) download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct listnode
  4. {
  5. int data;
  6. struct listnode* next;
  7. struct listnode* prev;
  8.  
  9. };
  10.  
  11. struct listnode* head=NULL;
  12. struct listnode* tail=NULL;
  13.  
  14. struct listnode* createnode(int data)
  15. {
  16. struct listnode* temp = (struct listnode*)malloc(sizeof(struct listnode));
  17. temp->data=data;
  18. temp->next=NULL;
  19. temp->prev=NULL;
  20. return temp;
  21. }
  22.  
  23. void insertlast(int data)
  24. {
  25. if(head==NULL)
  26. {
  27. head=createnode(data);
  28. tail=head;
  29. }
  30. else
  31. {
  32. struct listnode* temp = createnode(data) ;
  33. tail->next=temp ;
  34. temp->prev=tail;
  35. tail=temp;
  36. }
  37. }
  38. void insertfirst(int data)
  39. {
  40. if(head==NULL)
  41. {
  42. head=createnode(data);
  43. tail=head;
  44. }
  45. else
  46. {
  47. struct listnode* temp = createnode(data) ;
  48. temp->next=head;
  49. head->prev=temp;
  50. head=temp;
  51. }
  52. }
  53.  
  54. struct listnode* searchnote(int key)
  55. {
  56. for(struct listnode* i=head; i!=NULL; i=i->next)
  57. {
  58. if(key== i->data)
  59. {
  60. return i;
  61. }
  62. }
  63. }
  64.  
  65. void insertbefore(int data,int key)
  66. {
  67. struct listnode* loc = searchnote(key);
  68. if(loc==NULL)
  69. {
  70. return;
  71. }
  72. struct listnode* temp = createnode(data) ;
  73. temp->prev=loc->prev;
  74. loc->prev->next=temp;
  75. temp->next=loc;
  76. loc->prev=temp;
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. }
  84.  
  85. void deleteany(int key){
  86. struct listnode* loc = searchnote(key);
  87. if(loc == NULL){
  88. return;
  89. }
  90. else{
  91. loc->prev->next=loc->next;
  92. loc->next->prev=loc->prev;
  93. free(loc);
  94. loc=NULL;
  95. }
  96.  
  97. }
  98.  
  99.  
  100. void printlist()
  101. {
  102. for(struct listnode* i=head; i!=NULL; i=i->next)
  103. {
  104. printf("%d -> ",i->data) ;
  105. }
  106. }
  107.  
  108. int main()
  109. {
  110. insertlast(9);
  111. insertlast(6);
  112. insertlast(3);
  113. insertlast(4);
  114. insertlast(16);
  115. insertlast(0);
  116. insertlast(2);
  117. insertlast(8);
  118.  
  119. insertbefore(15,0);
  120. deleteany(4);
  121. deleteany(16);
  122.  
  123.  
  124.  
  125. printf("print elements: ");
  126. printlist();
  127.  
  128. }
  129.  
  130.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
print elements: 9 -> 6 -> 3 -> 15 -> 0 -> 2 -> 8 ->