Week5 Linked Stack Queue 27102022 100443am

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

(CSC-221)

Data Structures &


Algorithms
(Fall 2022)
COURSE INSTRUCTOR :
MOMINA MOETESUM
Week 5
STACK AND QUEUE
IMPLEMENTATION USING LINKED
LIST
Two Important Data Structures
STACK QUEUE

LIFO Architecture FIFO Architecture

Initializing the stack Initializing the queue

Push an item onto the Enqueue an item from the


top of the stack rear of the queue

Pop an item from the Dequeue an item from the


top of the stack front of the queue

Is the Stack empty? Is the queue empty?


Dynamic Stack Implementation Using
Linked List – Class ADT
Class Node{ Class Stack{

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()

Stack::Stack() Bool Stack::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;

if(!isEmpty()) delete p; return v;

p->next=Top; }

else{cout<<“Stack underflow”; exit(0);}


Top=p;
};
};
Dynamic Stack Implementation Using
Linked List – Member Functions
Top_Value Display_Content
void Stack::display()
int Stack::Top_val()
{ if(!isEmpty())
{
{ Node *p=Top;
if(!isEmpty()) while(p!=NULL)

{cout<<“Stack is Empty; { cout<<p->data<<endl;

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()

Queue::Queue() Bool Queue::isEmpty()


{
{
front=NULL;
if(front==NULL && rear==NULL)
rear=NULL;
return true;
};
else

return false;

};
Dynamic Queue Implementation Using
Linked List – Member Functions
Enqueue

Void Queue::Enqueue(int v)

{ Node *p=new Node;

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())

{cout<<“Queue underflow”; exit(0);}


front rear front rear
Node *p=front; int v=front->data;

if(front==rear){front=rear=NULL;}

else{front=front->next;}

delete p; return v;

}; front p front rear


Dynamic Queue Implementation Using
Linked List – Member Functions
front_Value Display_Content
void Queue::display()
int Stack::front_val()
{ if(!isEmpty())
{
{ Node *p=front;
if(!isEmpty()) while(p!=NULL)

{cout<<“Queue is Empty; { cout<<p->data<<endl;

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);
}

bool pal = true;


while (!Q.empty()){
pal = Q.removeq() = = S.pop();
if(pal==false) break;
}
if (pal)
cout << “Palindrome!!” << endl;
return 0;
}
Credits
Some of the content in this presentation is adapted from:

1. https://www.geeksforgeeks.org/implement-a-stack-using-singly-linked-list/

2. https://www.geeksforgeeks.org/queue-linked-list-implementation/

You might also like