Advertisement
shabbyheart

circular link list

Apr 28th, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Node{
  5. public:
  6. int data;
  7. Node *next;
  8. };
  9.  
  10. class LinkList{
  11. Node *last = NULL;
  12. public:
  13. void add_to_first(int value){
  14. if(last != NULL)
  15. return;
  16.  
  17. last = new Node();
  18. last->data = value;
  19. last->next = last;
  20. }
  21.  
  22. void add_begin(int x){
  23. if(last == NULL)
  24. {
  25. return add_to_first(x);
  26. }
  27.  
  28. Node *newNode = new Node();
  29. newNode->data = x;
  30. newNode->next = last->next;
  31. last->next = newNode;
  32. }
  33.  
  34. void add_end(int x){
  35. if(last == NULL)
  36. {
  37. return add_to_first(x);
  38. }
  39.  
  40. Node *newNode = new Node();
  41. newNode->data = x;
  42. newNode->next = last->next;
  43. last->next = newNode;
  44. last = newNode;
  45. }
  46.  
  47. void add_after(int x,int value)
  48. {
  49. if(last == NULL)
  50. {
  51. cout<<"Link list is empty";
  52. return ;
  53. }
  54.  
  55. Node *newNode,*head;
  56. head = last->next;
  57.  
  58. do{
  59. if(head->data == value)
  60. {
  61. newNode = new Node();
  62. newNode->data = x;
  63. newNode->next = head->next;
  64. head->next = newNode;
  65. if(head == last)
  66. last = newNode;
  67. return ;
  68. }
  69. head = head->next;
  70. }while(head != last->next);
  71.  
  72. cout<<value<<" not present in the list."<<endl;
  73.  
  74. }
  75.  
  76. void delete_Node(int key)
  77. {
  78. if(last == NULL)
  79. return ;
  80. //Node *curr = last-next, *prev;
  81. Node *curr , *prev;
  82. curr = last->next;
  83. while(curr->data != key)
  84. {
  85. if(curr->next == last->next)
  86. {
  87. cout<<"Given node id not found"<<endl;
  88. //break;
  89. return ;
  90. }
  91. prev = curr;
  92. curr = curr->next;
  93. }
  94.  
  95. if(curr->next == last->next)
  96. {
  97. last = NULL;
  98. delete(curr);
  99. return ;
  100. }
  101. if( curr == last->next)
  102. {
  103. prev = last->next;
  104. // while( prev->next != last->next)
  105. // {
  106. // prev = prev->next;
  107. // }
  108. last->next = curr->next;
  109. prev->next = last->next;
  110. delete(curr);
  111. }
  112. else if( curr->next == last->next)
  113. {
  114. prev->next = last->next;
  115. delete(curr);
  116. }
  117. else
  118. {
  119. prev->next = curr->next;
  120. delete(curr);
  121. }
  122. }
  123.  
  124. void display(){
  125. Node *temp;
  126.  
  127. if(last == NULL){
  128. cout<<"Link List is Empty."<<endl;
  129. return ;
  130. }
  131. temp = last->next;
  132. do{
  133.  
  134. cout<< temp->data<<" ";
  135. temp = temp->next;
  136. }while(temp != last->next);
  137. }
  138.  
  139. };
  140.  
  141. int main(){
  142. LinkList ll;
  143. ll.add_to_first(20);
  144. ll.add_begin(10);
  145. ll.add_end(30);
  146. ll.add_after(500,20);
  147. ll.delete_Node(500);
  148. ll.display();
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement