Haval1

Basic operation in Linked List 1

Mar 23rd, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct node
  5. {
  6.     int data;
  7.     node *next;
  8. };
  9. void createList(node *&head, node*& tail, int item)
  10. {
  11.     node* newNode = new node;
  12.     newNode->next = NULL;
  13.     newNode->data = item;
  14.     if(head == NULL)
  15.     {
  16.         head = tail = newNode;
  17.     }
  18.     else
  19.     {
  20.         tail->next = newNode;
  21.         tail = newNode;
  22.     }
  23. }
  24. int deleteList(node *&head)    //Start Function ( Delete all item in list )
  25. {
  26.     node *temp;
  27.     node *curr = new node;
  28.     while(curr != NULL)
  29.     {
  30.         if(curr->data %2==1)
  31.         {
  32.             while(head != NULL)
  33.             {
  34.                 temp = head;
  35.                 head = head->next;
  36.                 delete temp;
  37.                 break;
  38.             }
  39.         }
  40.         curr = curr->next;
  41.         cout<<"delete sec\n";
  42.     }
  43. }
  44.  
  45. int display(node *&head)
  46. {
  47.     node *temp = head;
  48.     node *prev = new node;
  49.     node *curr = new node;
  50.     curr = head;
  51.     int temppos = 0,pos1 = 3,pos2 = 5;
  52.     if(temp == NULL)
  53.     {
  54.         cout<<"Please Enter some data to increment it \n";
  55.         return 0;
  56.     }
  57.     while(curr->next != NULL && temppos != pos1)
  58.     {
  59.         prev = curr;
  60.         curr = curr->next;
  61.         temppos++;
  62.     }
  63.     prev->data+=20;
  64.     cout<<"3rd position is increment it : "<<prev->data<<endl;
  65.     while(curr->next != NULL && temppos != pos2)
  66.     {
  67.         prev = curr;
  68.         curr = curr->next;
  69.         temppos++;
  70.     }
  71.     prev->data+=20;
  72.     cout<<"5d position is increment it : "<<prev->data<<endl;
  73.     while(temp != NULL)
  74.     {
  75.         cout<<"data is : "<<temp->data<<endl;
  76.         temp = temp->next;
  77.     }
  78. }
  79. int main()
  80. {
  81.     node *tail,*head;
  82.     head = tail = NULL;
  83.     int choice,item;
  84.     do
  85.     {
  86.         cout<<"1 - create list \n";
  87.         cout<<"2 - inc by 20 the 3th and 5th\n";
  88.         cout<<"4 - exit\n";
  89.         cout<<"Enter your choice : ";cin>>choice;
  90.         switch(choice)
  91.         {
  92.         case 1:
  93.             for(int i=0;i<7;i++)
  94.             {
  95.                 cout<<"Enter item : ";
  96.                 cin>>item;
  97.                 createList(head,tail,item);
  98.             }
  99.             break;
  100.         case 2:
  101.             display(head);
  102.             break;
  103.         default:
  104.             cout<<"\n\n\n\t\t\tBYE BYE\n\n";
  105.         }
  106.     }
  107.     while(choice != 4);
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment