Advertisement
_Kripaka001_

linkedList 7

Aug 21st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  class List{
  6.     public:
  7.  
  8.         List(){ next = NULL; data = 0;}
  9.         int data;
  10.         List* next;
  11.  };
  12.  
  13. void output (List* x){
  14.    if(x->next != NULL) {
  15.         cout << x->data << " ";
  16.         x = x->next;
  17.         output(x);
  18.     }
  19.     cout << endl;
  20. }
  21.  
  22. int shift(List*& root, List* head){ // delete one from the end and type data ||-||-/  *5*
  23.     if(root == NULL){
  24.         cout << "I can't do shift. Root is NULL.";
  25.     }else{
  26.         int number = root->data;
  27.         List* node = new List();
  28.         List* x = new List();
  29.         while(head->next->next != NULL){
  30.             head = head->next;
  31.             cout << "test -> " << head->data << " ";
  32.         }
  33.         node = root;
  34.         delete node;
  35.         root = head;
  36.         root->next = NULL;
  37.  
  38.         return number;
  39.     }
  40.  }
  41.  
  42. int pop(List*& root, List*& head){
  43.     cout <<  head;
  44.     if(root == NULL){
  45.         cout << "I can't do pop. Root is NULL.";
  46.     }else{
  47.         List* node = new List();
  48.         node = head;
  49.  
  50.         List* x = new List();
  51.         x = head->next;
  52.         delete node;
  53.  
  54.         head = x;
  55.         delete x;
  56.     }
  57.     cout <<  head;
  58.     output(head);
  59. }
  60.  
  61. void unshift (List*& root, int value, List*& head){ // add one list from the beginning |new
  62.     if(root == NULL){
  63.         List* node = new List();
  64.         node->data = value;
  65.         node->next = root;
  66.         root = node;
  67.         cout << root->data << endl;
  68.  
  69.         head = root;
  70.         //cout << "->";
  71.         //output(head);
  72.         delete node;
  73.     }else{
  74.         List* node = new List();
  75.         node->next = head;
  76.         node->data = value;
  77.         root = node;
  78.         head = node;
  79.         //output(head);
  80.         delete node;
  81.     }
  82.     cout <<  head->data;
  83. }
  84.  
  85.  void push (List*& root, int value, List*& head){     // insert line to the end ||-||-|new|
  86.     if(root == NULL){
  87.         List *node = new List();
  88.         node->data = value;
  89.         node->next = root;
  90.         root = node;
  91.         //cout << root->data << endl;
  92.  
  93.         head = root;
  94.         //cout << "->" << " head - data" ;
  95.         output(head);
  96.  
  97.         delete node;
  98.     }else{
  99.         List *node = new List();
  100.         node->data = value;
  101.         node->next = root;
  102.         root->next = node;
  103.         root = root->next;
  104.  
  105.         //cout << root->data;
  106.         delete node;
  107.     }
  108.  
  109. }
  110.  
  111.  int main(){
  112.     List* head = new List();
  113.     head = NULL;
  114.     List* root = new List();
  115.     root = NULL;
  116.     push(root, 10, head);
  117.     push(root, 20, head);
  118.     //cout << pop(root, head);
  119.     unshift(root, 11, head);
  120.    // output(head);
  121.  
  122.  
  123.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement