Advertisement
rootUser

linklist.cpp

May 28th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdio>
  3. using namespace std;
  4.  
  5. void push(int x);
  6. void pop();
  7. void search(int x);
  8.  
  9. typedef struct alvi
  10. {
  11.     int data;
  12.     struct alvi *next;
  13. } node;
  14.  
  15. node *head=NULL;
  16.  
  17. int main()
  18. {
  19.     while(1)
  20.     {
  21.         int choice;
  22.         cout<<"1)push \t 2)pop \t 3)Search \nChoose: ";
  23.         cin>>choice;
  24.         if(choice==1)
  25.         {
  26.             int input;
  27.             cout<<"Enteer n integer to push: ";
  28.             cin>>input;
  29.             push(input);
  30.         }
  31.         if(choice==2)
  32.         {
  33.             pop();
  34.         }
  35.         if(choice==3)
  36.         {
  37.             int input1;
  38.             cout<<"enter element to search : "<<endl;
  39.             cin>>input1;
  40.             search(input1);
  41.         }
  42.     }
  43.     /*
  44.     node a;
  45.     a.data=10;
  46.     cout<<a.data<<":data";
  47.     */
  48.     return 0;
  49. }
  50. void push( int x)
  51. {
  52.     node *newNode=new node();
  53.     newNode->data=x;
  54.     if(head==NULL)
  55.     {
  56.         newNode->next=NULL;
  57.         head=newNode;
  58.         cout<<"First node pushed\n";
  59.     }
  60.     else
  61.     {
  62.         newNode->next=head;
  63.         head=newNode;
  64.         cout<<"value pushesd\n";
  65.     }
  66. }
  67.  
  68.  
  69. void  pop()
  70. {
  71.     node *cursor;
  72.     cursor=head;
  73.     while(cursor!=NULL)
  74.     {
  75.         cout<<cursor->data<<"\t";
  76.         cursor=cursor->next;
  77.     }
  78.     cout<<"\n";
  79. }
  80. void  search(int x)
  81. {
  82.     node *cursor1;
  83.     cursor1=head;
  84.     int counter=1;
  85.     int flag=0;
  86.     while(cursor1!=NULL)
  87.     {
  88.         if(cursor1->data==x)
  89.         {
  90.             flag=1;
  91.             cout<<"element found"<<endl;
  92.             cout<<"the "<<counter<<" th element is : "<<cursor1->data<<endl;
  93.             break;
  94.         }
  95.         //cout<<cursor1->data<<"\t";
  96.         cursor1=cursor1->next;
  97.         counter++;
  98.     }
  99.     if(flag==0)
  100.     {
  101.         cout<<"the element "<<x<<" is not found"<<endl;
  102.     }
  103.     cout<<"\n";
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement