Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. // One Way Linked List
  6. // Insert and Delete Operation
  7.  
  8. struct node
  9. {
  10. int num;
  11. struct node *next;
  12. };
  13.  
  14. struct node *head,*tail,*temp,*prev;
  15.  
  16.  
  17. void insert_Item()
  18. {
  19. int val,key;
  20. cout<<"\n\n";
  21. cout<<"Enter the value to insert:";
  22. // take input
  23. cin>>val;
  24.  
  25. // insert after which element?
  26. cout<<"\n After which value to insert:";
  27. cin>>key;
  28.  
  29.  
  30. // construct new node
  31. temp = new struct node;
  32. temp->num = val;
  33. temp->next = NULL;
  34.  
  35. // insert the newly created node in the list
  36.  
  37. if(head==NULL)
  38. {
  39. // List is empty
  40. head = temp;
  41. tail = temp;
  42. }
  43. else
  44. {
  45. struct node *t;
  46. t = head;
  47.  
  48. while(t!=NULL)
  49. {
  50. // search for the item after which we need to insert new item,
  51. // if not found data will be inserted after tail
  52. if(t->num==key)
  53. break;
  54.  
  55. t = t->next;
  56. }
  57.  
  58. // searched item not found, insert after tail
  59.  
  60. if(t==NULL)
  61. {
  62. tail->next = temp;
  63. tail = tail->next;
  64. }
  65. else
  66. {
  67. // searched item found and it is now pointed to by 't' after the while loop ends,
  68. //we need to insert the new item after that
  69. prev = t->next;
  70. temp->next = prev;
  71. t->next = temp;
  72. }
  73.  
  74. }
  75. cout<<"\n";
  76.  
  77. }
  78.  
  79. void delete_Item()
  80. {
  81. int key;
  82. cout<<"\n\n";
  83. cout<<"Enter the element to delete:";
  84. cin>>key;
  85.  
  86. struct node *t;
  87. t = head;
  88. prev = head; // 'prev' is the pointer to hold the element just before the item we want to delete
  89.  
  90. if(head==NULL)
  91. cout<<"\n List Empty..\n\n";
  92. else
  93. {
  94. while(t!=NULL){
  95. // search for the item to delete
  96. if(t->num==key)
  97. break;
  98.  
  99. prev = t;
  100. t = t->next;
  101. }
  102.  
  103.  
  104. if(t == NULL)
  105. {
  106. cout<<"\n Item Not Found..\n\n";
  107. return;
  108. }
  109. else{
  110.  
  111. // searched item found and it is now pointed to by 't' after the while loop ends
  112. // We need to delete the node pointed to by 't'
  113. // prev no points to the node before 't'
  114.  
  115. prev->next = t->next;
  116. delete t;
  117. }
  118.  
  119. }
  120.  
  121. }
  122.  
  123. void print_List()
  124. {
  125.  
  126. temp = head;
  127. cout<<"\n";
  128.  
  129. if(head==NULL)
  130. cout<<"\n List Empty..\n\n";
  131. else
  132. {
  133. while(temp!=NULL)
  134. {
  135. cout<<temp->num<<" -->";
  136. temp = temp->next;
  137. }
  138. }
  139. cout<<"\n";
  140. cout<<"\n";
  141. }
  142.  
  143.  
  144. int main()
  145. {
  146. int n;
  147. while(true)
  148. {
  149.  
  150. cout<<"Enter operation: (1) Insert (2) Delete (3) Print (0) Exit ():";
  151. cin>>n;
  152.  
  153. if(n==1)
  154. insert_Item();
  155. else if(n==2)
  156. delete_Item();
  157. else if(n==3)
  158. print_List();
  159. else
  160. break;
  161.  
  162.  
  163. }
  164.  
  165. return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement