Haval1

homework1

Mar 29th, 2019
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 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 display(node *&head)
  25. {
  26.     node *temp = head;
  27.     node *prev = new node;
  28.     node *curr = new node;
  29.     curr = head;
  30.     int temppos = 0,pos1 = 3,pos2 = 5;
  31.     if(temp == NULL)
  32.     {
  33.         cout<<"Please Enter some data to increment it \n";
  34.         return 0;
  35.     }
  36.     while(curr->next != NULL && temppos != pos1)
  37.     {
  38.         prev = curr;
  39.         curr = curr->next;
  40.         temppos++;
  41.     }
  42.     prev->data+=20;
  43.     cout<<"3rd position is increment it : "<<prev->data<<endl;
  44.     while(curr->next != NULL && temppos != pos2)
  45.     {
  46.         prev = curr;
  47.         curr = curr->next;
  48.         temppos++;
  49.     }
  50.     prev->data+=20;
  51.     cout<<"5d position is increment it : "<<prev->data<<endl;
  52.     while(temp != NULL)
  53.     {
  54.         cout<<"data is : "<<temp->data<<endl;
  55.         temp = temp->next;
  56.     }
  57. }
  58. int main()
  59. {
  60.     node *tail,*head;
  61.     head = tail = NULL;
  62.     int choice,item;
  63.     do
  64.     {
  65.         cout<<"1 - create list \n";
  66.         cout<<"2 - inc by 20 the 3th and 5th\n";
  67.         cout<<"3 - check odd\n";
  68.         cout<<"4 - exit\n";
  69.         cout<<"Enter your choice : ";cin>>choice;
  70.         switch(choice)
  71.         {
  72.         case 1:
  73.             for(int i=0;i<7;i++)
  74.             {
  75.                 cout<<"Enter item : ";
  76.                 cin>>item;
  77.                 createList(head,tail,item);
  78.             }
  79.             break;
  80.         case 2:
  81.             display(head);
  82.             break;
  83.         case 3:
  84.             delete_odd(head);
  85.             break;
  86.         default:
  87.             cout<<"\n\n\n\t\t\tBYE BYE\n\n";
  88.         }
  89.     }
  90.     while(choice != 4);
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment