Advertisement
samu14792

Insert, Delete in Linked List

Feb 22nd, 2018
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<string>
  4. using namespace std;
  5.  
  6. struct node{
  7.     int x;
  8.     struct node* next;
  9. };
  10.  
  11. struct node* create_new_node(){
  12.     struct node* tmp;
  13.     tmp = new node;
  14.     return tmp;
  15. };
  16.  
  17. void print_list(struct node* h){
  18.     struct node* it; //iterator for iterating the whole list;
  19.     it=h; // start from head
  20.     cout<<"List: ";
  21.     while(it != NULL){
  22.         cout<<it->x<<" "; // why it->x is used here instead of it.x?
  23.         it = it->next;
  24.     }
  25.     cout<<endl; //new line
  26. }
  27.  
  28. struct node* list_insert_first(struct node* h, int value){
  29.     struct node* temp;
  30.     temp = create_new_node();
  31.     temp->x = value; // why temp->x is used here instead of temp.x?
  32.     temp->next = h; // because insert-first here
  33.     h = temp; //why this should be done? check this!!
  34.     return h;
  35. }
  36.  
  37. struct node* list_insert_last(struct node* h, int value){
  38.     struct node* temp;
  39.     struct node* it;
  40.     temp = create_new_node();
  41.     temp->x = value; // why temp->x is used here instead of temp.x?
  42.     temp->next = NULL;
  43.     if(h==NULL) {
  44.         return temp; //why?
  45.     }
  46.     it = h; //starting to iterate from head
  47.     while(it->next!=NULL){ //iterating till the last node
  48.         it = it->next;
  49.     }
  50.     it->next = temp; //inserting last node
  51.     return h;
  52. }
  53.  
  54. struct node* list_delete_last(struct node* h){
  55.     if(h==NULL) return h; // check why I did this?
  56.     else if(h->next == NULL) return NULL; // check why I did this? ***
  57.     struct node* it = h->next; // interesting!! instead of initializing it by head, I initialized it by head->next. Why? ***
  58.     struct node* it_1 = h; // What is the role of this iterator?
  59.     while(it->next!=NULL){
  60.         it_1 = it;
  61.         it = it->next;
  62.     }
  63.     it_1->next = NULL; // Why I initialized it_1->next by NULL? ***
  64.     return h;
  65. }
  66.  
  67. struct node* list_delete_first(struct node* h){
  68.     if(h==NULL) return h;
  69.     else{
  70.         return h->next; // interesting!! why? ***
  71.     }
  72. }
  73.  
  74. int main(){
  75.     struct node* head; //starting node of the list
  76.     head = NULL; // at first no value, so nothing in that node(you must initialize head before comparing it!!)
  77.  
  78.     string s;
  79.     int value;
  80.     while(1){
  81.         cin>>s;
  82.         if(s=="insert-first"){
  83.             cin>>value;
  84.             head = list_insert_first(head, value);
  85.         }
  86.         else if(s=="insert-last"){
  87.             cin>>value;
  88.             head = list_insert_last(head, value);
  89.         }
  90.         else if(s=="delete-last"){
  91.             head = list_delete_last(head);
  92.         }
  93.         else if(s=="delete-first"){
  94.             head = list_delete_first(head);
  95.         }
  96.         else if(s=="exit"){
  97.             break;
  98.         }
  99.         else{
  100.             cout<<"Operation is not identified!"<<endl;
  101.         }
  102.         print_list(head);
  103.     }
  104.  
  105. }
  106.  
  107. /*
  108. input:
  109. insert-first 1
  110. insert-first 3
  111. insert-last 2
  112. insert-last 4
  113. insert-first 5
  114. insert-last 6
  115. insert-first 8
  116. insert-last 7
  117. insert-first 1
  118. insert-last 9
  119. insert-last 10
  120. insert-first 11
  121. delete-last
  122. delete-last
  123. delete-first
  124. delete-first
  125. delete-first
  126. delete-last
  127. delete-first
  128. delete-last
  129. delete-first
  130. delete-first
  131. delete-last
  132. delete-last
  133. delete-first
  134. delete-last
  135. delete-first
  136. insert-first 1
  137. exit
  138. */
  139.  
  140. /*
  141. output:
  142. List: 1
  143. List: 3 1
  144. List: 3 1 2
  145. List: 3 1 2 4
  146. List: 5 3 1 2 4
  147. List: 5 3 1 2 4 6
  148. List: 8 5 3 1 2 4 6
  149. List: 8 5 3 1 2 4 6 7
  150. List: 1 8 5 3 1 2 4 6 7
  151. List: 1 8 5 3 1 2 4 6 7 9
  152. List: 1 8 5 3 1 2 4 6 7 9 10
  153. List: 11 1 8 5 3 1 2 4 6 7 9 10
  154. List: 11 1 8 5 3 1 2 4 6 7 9
  155. List: 11 1 8 5 3 1 2 4 6 7
  156. List: 1 8 5 3 1 2 4 6 7
  157. List: 8 5 3 1 2 4 6 7
  158. List: 5 3 1 2 4 6 7
  159. List: 5 3 1 2 4 6
  160. List: 3 1 2 4 6
  161. List: 3 1 2 4
  162. List: 1 2 4
  163. List: 2 4
  164. List: 2
  165. List:
  166. List:
  167. List:
  168. List:
  169. List: 1
  170. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement