0% found this document useful (0 votes)
0 views6 pages

Module 2

The document provides an overview of stacks and queues, including their definitions, operations, and implementations using arrays. It covers stack operations like push and pop, and queue operations like enqueue and dequeue, along with their respective terminologies. Additionally, it discusses types of queues such as circular queues, double-ended queues, and priority queues, highlighting their unique features and applications.

Uploaded by

yyashas008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views6 pages

Module 2

The document provides an overview of stacks and queues, including their definitions, operations, and implementations using arrays. It covers stack operations like push and pop, and queue operations like enqueue and dequeue, along with their respective terminologies. Additionally, it discusses types of queues such as circular queues, double-ended queues, and priority queues, highlighting their unique features and applications.

Uploaded by

yyashas008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Module 2: Stacks and Queues

1. Stacks

1.1 Introduction

A stack is a linear data structure that follows the Last In First Out (LIFO) principle. The element
added last is removed first. It has two primary operations:

• push: Insert an element onto the stack.

• pop: Remove the top element from the stack.

1.2 Stack Terminology

• Top: Index of the last inserted element.

• Overflow: Attempting to push when the stack is full.

• Underflow: Attempting to pop when the stack is empty.

1.3 Stack Implementation using Arrays

#define MAX 100

int stack[MAX];

int top = -1;

void push(int item) {

if (top == MAX - 1) {

printf("Stack Overflow\n");

return;

stack[++top] = item;

int pop() {

if (top == -1) {

printf("Stack Underflow\n");

return -1;

}
return stack[top--];

1.4 Applications of Stacks

• Reversing strings

• Expression evaluation (postfix, prefix)

• Backtracking (e.g., maze, recursive calls)

• Function call stack in recursion

2. Queues

2.1 Introduction

A queue is a linear data structure that follows the First In First Out (FIFO) principle. The element
inserted first is removed first. It has two main operations:

• enqueue: Insert element at the rear.

• dequeue: Remove element from the front.

2.2 Queue Terminology

• Front: Index of the first element.

• Rear: Index of the last inserted element.

• Overflow: Queue is full.

• Underflow: Queue is empty.

2.3 Queue Implementation using Arrays

#define SIZE 50

int queue[SIZE];

int front = -1, rear = -1;

void enqueue(int item) {

if (rear == SIZE - 1) {

printf("Queue Overflow\n");

return;

if (front == -1)

front = 0;
queue[++rear] = item;

int dequeue() {

if (front == -1 || front > rear) {

printf("Queue Underflow\n");

return -1;

return queue[front++];

3. Types of Queues

3.1 Circular Queue

In circular queues, the last position is connected back to the first position to make a circle. This avoids
the wastage of space in linear queues.

Circular Queue Operations:

#define N 5

int cq[N];

int front = -1, rear = -1;

void enqueue(int val) {

if ((rear + 1) % N == front) {

printf("Queue Overflow\n");

return;

if (front == -1)

front = 0;

rear = (rear + 1) % N;

cq[rear] = val;

}
int dequeue() {

if (front == -1) {

printf("Queue Underflow\n");

return -1;

int val = cq[front];

if (front == rear)

front = rear = -1;

else

front = (front + 1) % N;

return val;

3.2 Double-Ended Queue (Deque)

In a deque, insertion and deletion can take place from both ends.

• Input Restricted Deque: Insertion at one end only.

• Output Restricted Deque: Deletion at one end only.

#define SIZE 5

int deque[SIZE];

int front = -1, rear = -1;

void insertFront(int item) {

if ((front == 0 && rear == SIZE - 1) || (front == rear + 1)) {

printf("Deque Overflow\n");

return;

if (front == -1) {

front = rear = 0;

} else if (front == 0) {

front = SIZE - 1;

} else {

front--;
}

deque[front] = item;

void insertRear(int item) {

if ((front == 0 && rear == SIZE - 1) || (front == rear + 1)) {

printf("Deque Overflow\n");

return;

if (front == -1) {

front = rear = 0;

} else if (rear == SIZE - 1) {

rear = 0;

} else {

rear++;

deque[rear] = item;

int deleteFront() {

if (front == -1) {

printf("Deque Underflow\n");

return -1;

int item = deque[front];

if (front == rear) {

front = rear = -1;

} else if (front == SIZE - 1) {

front = 0;

} else {

front++;
}

return item;

int deleteRear() {

if (rear == -1) {

printf("Deque Underflow\n");

return -1;

int item = deque[rear];

if (front == rear) {

front = rear = -1;

} else if (rear == 0) {

rear = SIZE - 1;

} else {

rear--;

return item;

3.3 Priority Queue

A priority queue is an abstract data type where each element has a priority. Elements with higher
priority are dequeued before those with lower priority.

Example Applications:

• Job scheduling in operating systems

• Dijkstra’s shortest path algorithm

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy