Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class Node
- {
- public:
- int info;
- Node * next;
- };
- class List: public Node
- {
- public:
- Node *head,*last;
- List()
- {
- head = last = NULL;
- }
- void Insert();
- void Delete();
- void Search(int);
- void Display();
- void Traverse();
- };
- void List::Insert()
- {
- Node *temp = new Node;
- temp->next = NULL;
- cout<<"Enter value for the new Node : "<<endl;
- cin>>temp->info;
- if(head==NULL)
- {
- head = last = temp;
- return;
- }
- last->next = temp;
- last = temp;
- }
- void List::Delete()
- {
- if(head==NULL)
- {
- cout<<"Stack Underflow : "<<endl;
- return;
- }
- cout<<"The item "<<last->info<<" is deleted " <<endl;
- if(head == last)
- {
- delete head;
- head = last = NULL;
- return;
- }
- Node *cur = head;
- Node * pre = NULL;
- while(cur->next!=NULL)
- {
- pre = cur;
- cur = cur->next;
- }
- delete cur;
- last = pre;
- pre->next = NULL;
- }
- void List::Search(int item)
- {
- int position = 0;
- Node *cur = head;
- while(cur!=NULL)
- {
- position++;
- if(cur->info==item)
- {
- cout<<"The item is at "<<position<<" Position : "<<endl;
- return;
- }
- cur = cur->next;
- }
- cout<<"The element doesn't exits: "<<endl;
- }
- void List::Display()
- {
- Node *cur = head;
- if(head == NULL)
- {
- cout<<"Stack is Empty " <<endl;
- return;
- }
- while(cur!=NULL)
- {
- cout<<cur->info<<" -> ";
- cur = cur->next;
- }
- cout<<endl;
- }
- void List::Traverse()
- {
- while(1)
- {
- int chk;
- cout<<"Enter your Choice "<<endl;
- cout<<" 1:Insert \t 2:Delete \t 3:Display \t 4:Search \t 5:Exit "<<endl;
- cin>>chk;
- switch(chk)
- {
- case 1:
- Insert();
- break;
- case 2:
- Delete();
- break;
- case 3:
- Display();
- break;
- case 4:
- int item;
- cout<<"Enter Item to be searched :"<<endl;
- cin>>item;
- Search(item);
- break;
- case 5:
- return;
- default:
- cout<<"Wrong Input : Try Again "<<endl;
- fflush(stdin);
- }
- }
- }
- int main()
- {
- List ob;
- ob.Traverse();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement