Posts

Showing posts from November 10, 2019

Queue

Image
Program #include<iostream> using namespace std; struct node{ int data; node *next; }; class ListQueue{ node *head,*tail; public: ListQueue() { head=NULL; tail=NULL; } void EnQueue(int info) { node *NewNode=new node;     NewNode->data=info;     NewNode->next=NULL;     if(head==NULL)     {     head=NewNode; } else { tail->next=NewNode; } tail=NewNode; } int DeQueue(){ if(IsEmpty()) { cout<<"Underflow occurs "; return 0; } int info; node *cur=head; info=head->data; head=head->next; delete cur;             return info; } int IsEmpty() { if(head==NULL) return 1; return 0; } }; int main() { ListQueue Q; Q.EnQueue(20); cout<<Q.DeQueue(); }

Stack

Image
Program #include<iostream> using namespace std; struct node { int data; node *next; }; class ListStack{ node *head; public: ListStack() { head=NULL; } void push(int info) { node *NewNode=new node; NewNode->data=info; NewNode->next=head; head=NewNode; } int pop() { if(IsEmpty()) { cout<<"Underflow occurs "; return NULL; } int info; node *cur=head; info=head->data; head=head->next; delete cur;             return info; } int IsEmpty() { if(head==NULL) return 1; return 0; } }; int main() { ListStack s; s.push(20); cout<<s.pop(); cout<<s.pop(); }