Professional Documents
Culture Documents
Week5 Linked Stack Queue 27102022 100443am
Week5 Linked Stack Queue 27102022 100443am
Week5 Linked Stack Queue 27102022 100443am
public:
public:
Node *Top;
int data;
Stack();
Node *next; bool isEmpty();
}; void push(int);
int pop();
int Top_val();
};
Dynamic Stack Implementation Using
Linked List – Member Functions
Constructor isEmpty()
{ {
Top=NULL; if(Top==NULL)
return true;
};
else
return false;
};
Dynamic Stack Implementation Using
Linked List – Member Functions
Push Pop
int Stack::Pop()
Void Stack::Push(int v)
{ if(!isEmpty())
{ Node *p=new Node;
{ Node *p=Top;
p->data=v;
int v=Top->data;
p->next=NULL; Top=Top->next;
p->next=Top; }
p=p->next;}
exit(0);}
else{cout<<“Stack is empty”;
return Top->data;
exit(0);}
}; };
Dynamic Queue Implementation Using
Linked List – Class ADT
Class Node{ Class Stack{
public:
public:
Node *front, *rear;
int data;
Queue();
Node *next; bool isEmpty();
}; void enqueue(int);
int dequeue();
int front_val();
};
Dynamic Queue Implementation Using
Linked List – Member Functions
Constructor isEmpty()
return false;
};
Dynamic Queue Implementation Using
Linked List – Member Functions
Enqueue
Void Queue::Enqueue(int v)
p->data=v;
p->next=NULL;
if(isEmpty())
front=p; rear=p;
else{ rear->next=p;
rear=p;}
};
Dynamic Queue Implementation Using
Linked List – Member Functions
Dequeue
int Queue::Dequeue()
p
{if(isEmpty())
if(front==rear){front=rear=NULL;}
else{front=front->next;}
delete p; return v;
p=p->next;}
exit(0);}
else{cout<<“Queue is empty”;
return front->data;
exit(0);}
}; };
Application-Palindrome Check
#include “Stack.h”;
#include “Queue.h”;
#include <iostream.h>
int main()
{
Stack S;
Queue Q;
char ch;
cout << “Enter string to be tested as palindrome: “;
while (cin >> ch)
{
S.push(ch);
Q.addq(ch);
}
1. https://www.geeksforgeeks.org/implement-a-stack-using-singly-linked-list/
2. https://www.geeksforgeeks.org/queue-linked-list-implementation/