Stack and Queue Implementation using Arrays in C++, Lecture notes of Data Structures and Algorithms

A practical guide to implementing stack and queue data structures using arrays in c++. It includes c++ code examples for both stack and queue, demonstrating essential operations such as push, pop, enqueue, and dequeue. The document also covers applications of stacks, such as backtracking, function calls, and string reversal, and applications of queues, such as cpu scheduling and synchronization. It is designed to help students understand and implement these fundamental data structures effectively. The document also includes assignment questions to test the understanding of the concepts.

Typology: Lecture notes

2022/2023

Available from 11/17/2025

marwan-ahmed-10
marwan-ahmed-10 🇪🇬

6 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Data Structures | Level 2 | Lab 3
1 | P a g e
Stack & Queue using Arrays
1- What is the Stack?
Stack is an abstract data type and a data structure that follows LIFO (last in first out) strategy.
It means the element added last will be removed first. Stack allows two operations push and
pop. Push adds an element at the top of the stack and pop removes an element from top of
the stack.
2- Applications of Stack:
Stack is used in different applications, here is some of them:
1. Backtracking
Suppose we are finding a path for solving maze problem. We choose a path and after
following it we realize that it is wrong. Now we need to go back to the beginning of the path
to start with new path. This can be done with the help of stack.
2. Function Call
Stack is used to keep information about the active functions.
3. String Reversal
Stack is used to reverse a string. We push the characters of string one by one into stack and
then pop character from stack.
pf3
pf4
pf5

Partial preview of the text

Download Stack and Queue Implementation using Arrays in C++ and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Stack & Queue using Arrays

1- What is the Stack?

Stack is an abstract data type and a data structure that follows LIFO (last in first out) strategy.

It means the element added last will be removed first. Stack allows two operations push and

pop. Push adds an element at the top of the stack and pop removes an element from top of

the stack.

2 - Applications of Stack :

Stack is used in different applications, here is some of them:

1. Backtracking

Suppose we are finding a path for solving maze problem. We choose a path and after

following it we realize that it is wrong. Now we need to go back to the beginning of the path

to start with new path. This can be done with the help of stack.

2. Function Call

Stack is used to keep information about the active functions.

3. String Reversal

Stack is used to reverse a string. We push the characters of string one by one into stack and

then pop character from stack.

Stack Using Dynamic Array Code:

#include using namespace std; int top = - 1; int size; int *stack_arr; bool isEmpty() { if (top==-1) return true; else return false; } bool isFull() { if (top==size-1) return true; else return false; } void push(int pushed_item) { if(isFull()) cout<<"Stack Overflow\n"; else { top=top+1; stack_arr[top] = pushed_item; } } int pop() { if(isEmpty()) { cout<<"Stack Underflow\n"; return - 1; } else { int poped_item =stack_arr[top]; top=top-1; return poped_item; } } void traverse() { if(!isEmpty()) { cout<<"Elements in the stack \n"; for (int i=top; i>=0;i--) cout<<stack_arr[i]<<endl; } else cout<<"Stack is Empty \n"; }

Queue Using Array

1- What is the Queue?

Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is

open at both its ends. One end is always used to insert data (enqueue) and the other is used

to remove data (dequeue). Queue follows First-In-First-Out methodology (FIFO), i.e., the data

item stored first will be accessed first.

3 - Applications of Queue data Structure :

  • Queue is useful in CPU scheduling, Disk Scheduling.
  • Queue is used for synchronization. Examples: IO Buffers, pipes, file IO, etc.

Queue Using Dynamic Array Code:

#include using namespace std; int front,rear; int size; int *Queue; bool isFull() { if (rear==size-1) return true; else return false; } bool isEmpty() { if (front==-1 || front > rear) return true; else return false; } void enqueue(int item) { if (isFull()) cout<<"Queue Overflow \n"; else

if (front==-1) front=0; rear++; Queue[rear]=item; } } int dequeue() { if(isEmpty()) { cout<<"Queue Underflow \n"; return - 1; } else { int item=Queue[front]; front++; return item; } } void traverse() { if(isEmpty()) cout<<"Queue is Empty \n"; else { for(int i=front;i<=rear;i++) cout<<Queue[i]<<" "; } cout<<endl; } void main() { int ch,item; front=rear=-1; cout<<"Enter Queue Size"; cin>>size; Queue=new int [size]; while(true) { cout<<"***************************************\n"; cout<<"1) Insert element to Queue \n"; cout<<"2) Delete element from Queue \n"; cout<<"3) Display all the elements of queue \n"; cout<<"4) Exit \n"; cout<<"***************************************\n"; cin>>ch; switch (ch) { case 1 : cout<<"Enter the item:"; cin>>item; enqueue(item); break; case 2 : cout<<dequeue()<<" Deleted from the Queue \n"; break;