Docsity

Stack and Queue Data Structures: Implementation and Examples in C++, Slides of Computer Programming

Partial preview of the text

Download Stack and Queue Data Structures: Implementation and Examples in C++ and more Slides Computer Programming in PDF only on Docsity!

Stack and Queue

Stack

#define maxSize 5 struct Stack{ int top=-1; int data[maxSize]; }s; void push(int a) { if(!overFlow()) { s.data[++s.top]=a; } else cout<<“stack overflow!!!”; } int pop() { if(!underFlow(s)) { return(s.data[s.top--]); } else cout<<“Stack is already empty”; } int overFlow() { if(s.top==maxSize-1) return(1); else return(0); }

Linked List Implementation of Stack

top

2 3 5

Queue

void enqueue(int a){ If(!Full(q)) q.data[++q.rear]=a; else cout<<“Queue is full”; } int dequeue(){ If(!isEmpty()) return(q.data[q.front++]); else { cout<<“Queue is empty”; exit(0); } }

  • #define maxSize 5
  • struct queue{ int front=0; int rear=-1; int data[maxSize]; }q; int Full() { if(rear==maxSize-1) return(1); else return(0); }

Linked List Implementation of Queue

Rear

front

5 2 1

Circular Queue

  • Limitation of Queue:-

front

rear

0 1 2 3 4 5 Queue is Empty still we can not insert data!!!