What is Stack, Its Operations, Queue, Circular Queue, Priority QueueBalwant Gorad
Explain Stack and its Concepts, Its Operations, Queue, Circular Queue, Priority Queue. Explain Queue and It's Operations
Data Structures, Abstract Data Types
Data Structure- Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations −
PUSH, POP, PEEP
The document discusses linear data structures and specifically stacks and queues. It defines a stack as a linear data structure that follows the LIFO principle with one end and stack operations like push and pop. A queue has two ends (front and rear) and follows the FIFO principle. Common stack applications include undo/redo operations and recursion. Common queue applications include waiting lists and media playlists. The document also covers array implementations of stacks and queues as well as circular queue implementations to improve memory utilization.
The document discusses stacks in C++. It defines a stack as a data structure that follows LIFO (Last In First Out) principle where the last element added is the first to be removed. Stacks can be implemented using arrays or linked lists. The key operations on a stack are push which adds an element and pop which removes an element. Example applications of stacks include function call stacks, converting infix to postfix notation, and reversing arrays.
The document discusses stacks and stack operations in C. It defines a stack as a linear data structure that follows the LIFO principle, where elements are added and removed from one end called the top. The key stack operations are PUSH to add an element, POP to remove an element, and DISPLAY to output the stack. It provides algorithms for implementing PUSH and POP and handling exceptions like stack overflow. The document also covers postfix notation, where operators follow operands, and the postfix evaluation algorithm using a stack.
The document discusses stacks and queues. It begins by defining a stack as a collection of homogeneous data elements where insertion and deletion occurs at one end, known as the top. The stack follows the LIFO (last in, first out) principle. Common stack operations like push and pop are introduced. Implementing stacks using both arrays and linked lists is covered. Applications of stacks like balancing symbols, recursion, undo operations are mentioned. The document then moves to discussing queues, their applications and implementations. Priority queues and their applications are also briefly covered.
Stack and Queue.pptx university exam preparationRAtna29
Queues and stacks are dynamic while arrays are static. So when we require dynamic memory we use queue or stack over arrays. Stacks and queues are used over arrays when sequential access is required. To efficiently remove any data from the start (queue) or the end (stack) of a data structure
Stack and queue are non-primitive data structures that differ in their accessing and adding methods. A stack uses LIFO (last in first out), accessing the last added element first, while a queue uses FIFO (first in first out), accessing the first added element first. A key difference is that a stack has one open end for pushing and popping, while a queue has two open ends for enqueuing and dequeuing. Both data structures are based on real-world equivalents like stacks of CDs and queues for movie tickets.
A stack is a linear data structure that follows the LIFO (last in, first out) principle. Elements are inserted and removed from the top of the stack. Common stack operations include push to add an element and pop to remove the top element. Stacks can be implemented using arrays or linked lists. Stacks are useful for operations like converting infix expressions to postfix and evaluating postfix expressions using a stack to hold operands. Queues follow the FIFO (first in, first out) principle with elements added to the rear and removed from the front. Common queue operations are enqueue to add and dequeue to remove elements. Queues can also be implemented using arrays or linked lists. Linked lists store elements in nodes with each node
The document discusses data structures and algorithms including stacks, queues, and their implementations using arrays and linked lists. Key points:
1. Stacks follow LIFO principle and allow insertion/removal at one end only. Queues follow FIFO principle. Both can be implemented using arrays or linked lists.
2. Common stack operations like push, pop, and peek have O(1) time complexity. Queue operations like enqueue and dequeue also have O(1) time complexity.
3. Linked list implementations of stacks and queues allocate memory dynamically and don't have size limits like arrays.
4. A circular queue treats the last node as connected to the first, forming a ring. This allows insertion
STACK ( LIFO STRUCTURE) - Data StructureYaksh Jethva
Stack which is known as LIFO structure.Which is type of the Linear data structure and it is Non-Primitive data structure.
Definition:Non primitive data structure are not a basic data structure and depends on other primitive data structure (Integer,float etc).
Non primitive data structure can't be operated by machine level instruction directly.
This document discusses stacks and their use as a data structure. It begins by defining what a stack is, namely a data structure that follows the last-in, first-out (LIFO) principle. Common stack operations like push, pop, and peek are introduced. Examples are given of how stacks are used in applications like undo mechanisms, expression evaluation, and validating parentheses in expressions. The key operations and algorithms for implementing a stack are described, including pseudocode for push, pop, and display functions. Finally, examples are provided of how stacks can be used to reverse strings, validate parentheses, and convert infix expressions to postfix form.
This document discusses different types of data structures, including linear and non-linear structures. It focuses on linear structures like arrays, stacks, and queues. Stacks follow LIFO principles with push and pop operations. Queues follow FIFO principles with enqueue and dequeue operations. Real-world examples and algorithms for common stack and queue operations are provided.
The document discusses the stack data structure. A stack is a collection of elements that follow the LIFO (last-in, first-out) principle. Elements can be inserted and removed from the top of the stack only. Stacks have common applications like undo functions in text editors and web browser history. Formally, a stack is an abstract data type that supports push, pop, top, is_empty and length operations. The document provides examples and explanations of stack operations and applications like infix to postfix conversion, expression evaluation, balancing symbols, function calls and reversing a string.
The document discusses stacks, which are linear data structures that follow the LIFO (last-in, first-out) principle. A stack has two main operations - push, which adds an element to the top of the stack, and pop, which removes the top element. Some key applications of stacks include implementing undo/redo features, converting infix expressions to postfix notation, and solving recursive backtracking problems. The document also describes how to evaluate expressions in postfix notation using a stack. Elements are pushed onto the stack as they are encountered, and when an operator is reached, elements are popped off to perform the operation before pushing the result back on.
The document describes implementing a queue using an array. It provides algorithms for enQueue() and deQueue() operations. EnQueue() inserts elements at the rear by incrementing rear and checking for full. DeQueue() deletes elements from the front by incrementing front and checking for empty. The queue uses front and rear pointers to manage insertion and deletion of elements based on FIFO principle using an underlying fixed-size array.
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxKALPANAC20
This document discusses stacks as a data structure. It defines a stack as a Last In First Out (LIFO) structure where the last item inserted is the first out. Key stack operations like push, pop, peek and isEmpty are described. Examples of stack applications include checking for balanced brackets, converting infix to postfix notation, implementing an undo/redo feature and recursion. The document also provides pseudocode for converting an infix expression to postfix using a stack and describes how a postfix calculator evaluates expressions.
The document discusses stacks and queues as data structures. It defines a stack as a linear data structure that follows the LIFO principle with insertion and deletion occurring at one end. Key stack operations are described as push, pop, peek, isEmpty and isFull. Queue is defined as a linear structure that follows the FIFO principle with insertion at the rear and deletion at the front. Common queue operations are enqueue, dequeue, peek, isEmpty and isFull. Array and linked list implementations of stacks and queues are also covered.
The section provides brief introduction to stack and its operations (PUSH and POP) along with the implementation of stack in real scenario to convert infix expression to postfix expression.
This document provides information about stacks as a data structure. It defines a stack as a linear data structure that only allows additions or deletions at one end, following the last-in, first-out (LIFO) principle. The key operations on a stack are push, which adds an element to the top, and pop, which removes an element from the top. The document also discusses stack terminology, different types of notations for expressions (infix, prefix, postfix), algorithms for converting between notations using a stack, an example C program for implementing a stack using an array, and summarizes the main characteristics of stacks.
This document provides an overview of data structures and algorithms. It defines data structures as organized storage for data that allows for efficient access and updating. There are two main categories of data structures - linear and non-linear. Common linear data structures include arrays and linked lists, while trees and graphs are examples of non-linear data structures. The document then focuses on array data structures, providing details on one-dimensional and two-dimensional arrays including representation, insertion, deletion, and traversal algorithms. It also covers common abstract data types like stacks and queues.
This document provides an overview of data structures and algorithms. It defines data structures as a way to store and organize data for efficient access and updating. There are two main categories of data structures - linear and non-linear. Common linear data structures include arrays and linked lists, while trees and graphs are examples of non-linear data structures. The document also describes common operations for stacks and queues like push, pop, enqueue and dequeue. It concludes by discussing different notations for writing arithmetic expressions like infix, prefix and postfix notations.
The document discusses stacks and queues. It begins by defining a stack as a collection of homogeneous data elements where insertion and deletion occurs at one end, known as the top. The stack follows the LIFO (last in, first out) principle. Common stack operations like push and pop are introduced. Implementing stacks using both arrays and linked lists is covered. Applications of stacks like balancing symbols, recursion, undo operations are mentioned. The document then moves to discussing queues, their applications and implementations. Priority queues and their applications are also briefly covered.
Stack and Queue.pptx university exam preparationRAtna29
Queues and stacks are dynamic while arrays are static. So when we require dynamic memory we use queue or stack over arrays. Stacks and queues are used over arrays when sequential access is required. To efficiently remove any data from the start (queue) or the end (stack) of a data structure
Stack and queue are non-primitive data structures that differ in their accessing and adding methods. A stack uses LIFO (last in first out), accessing the last added element first, while a queue uses FIFO (first in first out), accessing the first added element first. A key difference is that a stack has one open end for pushing and popping, while a queue has two open ends for enqueuing and dequeuing. Both data structures are based on real-world equivalents like stacks of CDs and queues for movie tickets.
A stack is a linear data structure that follows the LIFO (last in, first out) principle. Elements are inserted and removed from the top of the stack. Common stack operations include push to add an element and pop to remove the top element. Stacks can be implemented using arrays or linked lists. Stacks are useful for operations like converting infix expressions to postfix and evaluating postfix expressions using a stack to hold operands. Queues follow the FIFO (first in, first out) principle with elements added to the rear and removed from the front. Common queue operations are enqueue to add and dequeue to remove elements. Queues can also be implemented using arrays or linked lists. Linked lists store elements in nodes with each node
The document discusses data structures and algorithms including stacks, queues, and their implementations using arrays and linked lists. Key points:
1. Stacks follow LIFO principle and allow insertion/removal at one end only. Queues follow FIFO principle. Both can be implemented using arrays or linked lists.
2. Common stack operations like push, pop, and peek have O(1) time complexity. Queue operations like enqueue and dequeue also have O(1) time complexity.
3. Linked list implementations of stacks and queues allocate memory dynamically and don't have size limits like arrays.
4. A circular queue treats the last node as connected to the first, forming a ring. This allows insertion
STACK ( LIFO STRUCTURE) - Data StructureYaksh Jethva
Stack which is known as LIFO structure.Which is type of the Linear data structure and it is Non-Primitive data structure.
Definition:Non primitive data structure are not a basic data structure and depends on other primitive data structure (Integer,float etc).
Non primitive data structure can't be operated by machine level instruction directly.
This document discusses stacks and their use as a data structure. It begins by defining what a stack is, namely a data structure that follows the last-in, first-out (LIFO) principle. Common stack operations like push, pop, and peek are introduced. Examples are given of how stacks are used in applications like undo mechanisms, expression evaluation, and validating parentheses in expressions. The key operations and algorithms for implementing a stack are described, including pseudocode for push, pop, and display functions. Finally, examples are provided of how stacks can be used to reverse strings, validate parentheses, and convert infix expressions to postfix form.
This document discusses different types of data structures, including linear and non-linear structures. It focuses on linear structures like arrays, stacks, and queues. Stacks follow LIFO principles with push and pop operations. Queues follow FIFO principles with enqueue and dequeue operations. Real-world examples and algorithms for common stack and queue operations are provided.
The document discusses the stack data structure. A stack is a collection of elements that follow the LIFO (last-in, first-out) principle. Elements can be inserted and removed from the top of the stack only. Stacks have common applications like undo functions in text editors and web browser history. Formally, a stack is an abstract data type that supports push, pop, top, is_empty and length operations. The document provides examples and explanations of stack operations and applications like infix to postfix conversion, expression evaluation, balancing symbols, function calls and reversing a string.
The document discusses stacks, which are linear data structures that follow the LIFO (last-in, first-out) principle. A stack has two main operations - push, which adds an element to the top of the stack, and pop, which removes the top element. Some key applications of stacks include implementing undo/redo features, converting infix expressions to postfix notation, and solving recursive backtracking problems. The document also describes how to evaluate expressions in postfix notation using a stack. Elements are pushed onto the stack as they are encountered, and when an operator is reached, elements are popped off to perform the operation before pushing the result back on.
The document describes implementing a queue using an array. It provides algorithms for enQueue() and deQueue() operations. EnQueue() inserts elements at the rear by incrementing rear and checking for full. DeQueue() deletes elements from the front by incrementing front and checking for empty. The queue uses front and rear pointers to manage insertion and deletion of elements based on FIFO principle using an underlying fixed-size array.
STACK AND ITS OPERATIONS IN DATA STRUCTURES.pptxKALPANAC20
This document discusses stacks as a data structure. It defines a stack as a Last In First Out (LIFO) structure where the last item inserted is the first out. Key stack operations like push, pop, peek and isEmpty are described. Examples of stack applications include checking for balanced brackets, converting infix to postfix notation, implementing an undo/redo feature and recursion. The document also provides pseudocode for converting an infix expression to postfix using a stack and describes how a postfix calculator evaluates expressions.
The document discusses stacks and queues as data structures. It defines a stack as a linear data structure that follows the LIFO principle with insertion and deletion occurring at one end. Key stack operations are described as push, pop, peek, isEmpty and isFull. Queue is defined as a linear structure that follows the FIFO principle with insertion at the rear and deletion at the front. Common queue operations are enqueue, dequeue, peek, isEmpty and isFull. Array and linked list implementations of stacks and queues are also covered.
The section provides brief introduction to stack and its operations (PUSH and POP) along with the implementation of stack in real scenario to convert infix expression to postfix expression.
This document provides information about stacks as a data structure. It defines a stack as a linear data structure that only allows additions or deletions at one end, following the last-in, first-out (LIFO) principle. The key operations on a stack are push, which adds an element to the top, and pop, which removes an element from the top. The document also discusses stack terminology, different types of notations for expressions (infix, prefix, postfix), algorithms for converting between notations using a stack, an example C program for implementing a stack using an array, and summarizes the main characteristics of stacks.
This document provides an overview of data structures and algorithms. It defines data structures as organized storage for data that allows for efficient access and updating. There are two main categories of data structures - linear and non-linear. Common linear data structures include arrays and linked lists, while trees and graphs are examples of non-linear data structures. The document then focuses on array data structures, providing details on one-dimensional and two-dimensional arrays including representation, insertion, deletion, and traversal algorithms. It also covers common abstract data types like stacks and queues.
This document provides an overview of data structures and algorithms. It defines data structures as a way to store and organize data for efficient access and updating. There are two main categories of data structures - linear and non-linear. Common linear data structures include arrays and linked lists, while trees and graphs are examples of non-linear data structures. The document also describes common operations for stacks and queues like push, pop, enqueue and dequeue. It concludes by discussing different notations for writing arithmetic expressions like infix, prefix and postfix notations.
Examining Visual Attention in Gaze-Driven VR Learning: An Eye-Tracking Study ...Yasasi Abeysinghe
This study presents an eye-tracking user study for analyzing visual attention in a gaze-driven VR learning environment using a consumer-grade Meta Quest Pro VR headset. Eye tracking data were captured through the headset's built-in eye tracker. We then generated basic and advanced eye-tracking measures—such as fixation duration, saccade amplitude, and the ambient/focal attention coefficient K—as indicators of visual attention within the VR setting. The generated gaze data are visualized in an advanced gaze analytics dashboard, enabling us to assess users' gaze behaviors and attention during interactive VR learning tasks. This study contributes by proposing a novel approach for integrating advanced eye-tracking technology into VR learning environments, specifically utilizing consumer-grade head-mounted displays.
Preclinical Advances in Nuclear Neurology.pptxMahitaLaveti
This presentation explores the latest preclinical advancements in nuclear neurology, emphasizing how molecular imaging techniques are transforming our understanding of neurological diseases at the earliest stages. It highlights the use of radiotracers, such as technetium-99m and fluorine-18, in imaging neuroinflammation, amyloid deposition, and blood-brain barrier (BBB) integrity using modalities like SPECT and PET in small animal models. The talk delves into the development of novel biomarkers, advances in radiopharmaceutical chemistry, and the integration of imaging with therapeutic evaluation in models of Alzheimer’s disease, Parkinson’s disease, stroke, and brain tumors. The session aims to bridge the gap between bench and bedside by showcasing how preclinical nuclear imaging is driving innovation in diagnosis, disease monitoring, and targeted therapy in neurology.
Animal Models for Biological and Clinical Research ppt 2.pptxMahitaLaveti
This presentation provides an in-depth overview of the pivotal role animal models play in advancing both basic biological understanding and clinical research. It covers the selection and classification of animal models—ranging from invertebrates to rodents and higher mammals—and their applications in studying human physiology, disease mechanisms, drug development, and toxicology. Special emphasis is placed on the use of genetically modified models, patient-derived xenografts (PDX), and disease-specific models in cancer, neuroscience, infectious diseases, and metabolic disorders. The talk also addresses ethical considerations, regulatory guidelines, and the principles of the 3Rs (Replacement, Reduction, and Refinement) in animal research
Structure formation with primordial black holes: collisional dynamics, binari...Sérgio Sacani
Primordial black holes (PBHs) could compose the dark matter content of the Universe. We present the first simulations of cosmological structure formation with PBH dark matter that consistently include collisional few-body effects, post-Newtonian orbit corrections, orbital decay due to gravitational wave emission, and black-hole mergers. We carefully construct initial conditions by considering the evolution during radiation domination as well as early-forming binary systems. We identify numerous dynamical effects due to the collisional nature of PBH dark matter, including evolution of the internal structures of PBH halos and the formation of a hot component of PBHs. We also study the properties of the emergent population of PBH binary systems, distinguishing those that form at primordial times from those that form during the nonlinear structure formation process. These results will be crucial to sharpen constraints on the PBH scenario derived from observational constraints on the gravitational wave background. Even under conservative assumptions, the gravitational radiation emitted over the course of the simulation appears to exceed current limits from ground-based experiments, but this depends on the evolution of the gravitational wave spectrum and PBH merger rate toward lower redshifts.
Skin_structure_dermis_epidermis_layers of skinmuralinath2
Cells of skin have a brown pigment, melanin that is responsible for the colour of the skin. It is synthesised by melanocytes, which are observed primarily in the stratum germinativum & stratum spinosum of epidermis
They envisioned a brave new world, and what they got was fascism. As vibrant as its counterparts in Paris, Munich, and Milan, the avant-garde of Florence rose on a wave of artistic, political, and social idealism that swept the world with the arrival of the twentieth century. How the movement flourished in its first heady years, only to flounder in the bloody wake of World War I, is a fascinating story, told here for the first time. It is the history of a whole generation's extraordinary promise--and equally extraordinary failure.
Astrobiological implications of the stability andreactivity of peptide nuclei...Sérgio Sacani
Recent renewed interest regarding the possibility of life in the Venusian clouds has led to new studies on organicchemistry in concentrated sulfuric acid. However, life requires complex genetic polymers for biological function.Therefore, finding suitable candidates for genetic polymers stable in concentrated sulfuric acid is a necessary firststep to establish that biologically functional macromolecules can exist in this environment. We explore peptidenucleic acid (PNA) as a candidate for a genetic-like polymer in a hypothetical sulfuric acid biochemistry. PNA hex-amers undergo between 0.4 and 28.6% degradation in 98% (w/w) sulfuric acid at ~25°C, over the span of 14 days,depending on the sequence, but undergo complete solvolysis above 80°C. Our work is the first key step towardthe identification of a genetic-like polymer that is stable in this unique solvent and further establishes that con-centrated sulfuric acid can sustain a diverse range of organic chemistry that might be the basis of a form of lifedifferent from Earth’s
Keynote presentation at DeepTest Workshop 2025Shiva Nejati
Ad
Data Structures Stack and Queue Data Structures
1. Department of Computer Science (UG)
Data structures
(24BCA2T421)
Stack and Queue
Dr.Poongothai P
Assistant Professor
Department of Computer Science (UG)
Kristu Jayanti College (Autonomous)
Bengaluru.
2. UNIT – III
STACK and QUEUE
STACK:
Stack is a linear data structure that follows a particular order in which the
operations are performed.
The order may be LIFO(Last In First Out) or FILO(First In Last Out).
LIFO implies that the element that is inserted last, comes out first
and FILO implies that the element that is inserted first, comes out last.
Example:
Real-world stack, piles of books, etc.
A Stack is an abstract data type with a pre-defined capacity, which means
that it can store the elements of a limited size.
It is a data structure that follows some order to insert and delete the
elements, and that order can be LIFO or FILO.
Consider a plate stacked over one another in the canteen. The plate which is
at the top is the first one to be removed, i.e. the plate which has been
placed at the bottommost position remains in the stack for the longest
period of time.
3. Working of Stack:
Stack works on the LIFO pattern.
Example there are five memory blocks in the stack; therefore, the size of
the stack is 5.
Suppose we want to store the elements in a stack and let's assume
that stack is empty.
We have taken the stack of size 5 as shown below in which we are pushing
the elements one by one until the stack becomes full.
Stack Operations:
The following are some common operations implemented on the stack:
push(): When we insert an element in a stack then the operation is known
as a push. If the stack is full then the overflow condition occurs.
4. pop(): When we delete an element from the stack, the operation is known as
a pop. If the stack is empty means that no element exists in the stack, this
state is known as an underflow state.
isEmpty(): It determines whether the stack is empty or not.
isFull(): It determines whether the stack is full or not.'
peek(): It returns the element at the given position.
count(): It returns the total number of elements available in a stack.
change(): It changes the element at the given position.
display(): It prints all the elements available in the stack.
Stack Implementation:
PUSH operation:
The steps involved in the PUSH operation are given below:
Before inserting an element in a stack, we check whether the stack is full.
If we try to insert the element in a stack, and the stack is full, then
the overflow condition occurs.
When we initialize a stack, we set the value of top as -1 to check that
the stack is empty.
When the new element is pushed in a stack, first, the value of the top gets
incremented, i.e., top=top+1, and the element will be placed at the new
position of the top.
The elements will be inserted until we reach the max size of the stack.
5. POP operation:
The steps involved in the POP operation are given below:
Before deleting the element from the stack, we check whether the stack is
empty.
If we try to delete the element from the empty stack,
then
the underflow condition occurs.
If the stack is not empty, we first access the element which is pointed by
the top
Once the pop operation is performed, the top is decremented by 1,
i.e., top=top-1.
6. Stack Notations
Following is the various Applications of Stack in Data Structure:
Evaluation of Arithmetic Expressions
Backtracking
Delimiter Checking
Reverse a Data
Processing Function Calls
1. Evaluation of Arithmetic Expressions
An arithmetic expression consists of operands and operators.
In addition to operands and operators, the arithmetic expression may
also include parenthesis like "left parenthesis" and "right parenthesis".
7. Example: A + (B - C)
To evaluate the expressions, the precedence rules for the five basic arithmetic
operators are:
Operators Associativity Precedence
^ exponentiation Right to left
Highest followed by *Multiplication
and /division
*Multiplication, /division Left to right
Highest followed by + addition and
– subtraction
+ addition, - subtraction Left to right Lowest
Evaluation of Arithmetic Expression requires two steps:
First, convert the given expression into special notation.
Evaluate the expression in this new notation.
Notations for Arithmetic Expression
There are three notations to represent an arithmetic expression:
Infix Notation
Prefix Notation
Postfix Notation
Infix Notation
The infix notation is an expression in which each operator is placed between
the operands.
Infix expressions can be parenthesized or unparenthesized depending upon
the problem requirement.
8. Example: A + B, (C - D) etc.
All these expressions are in infix notation because the operator comes
between the operands.
Prefix Notation
The prefix notation places the operator before the operands and hence often
referred to as polish notation.
Example: + A B, -CD etc.
All these expressions are in prefix notation because the operator comes
before the operands.
Postfix Notation
The postfix notation places the operator after the operands.
This notation is just the reverse of Polish notation and also known as
Reverse Polish notation.
Example: AB +, CD+, etc.
All these expressions are in postfix notation because the operator comes
after the operands.
Conversion of Arithmetic Expression into various Notations:
Infix Notation Prefix Notation Postfix Notation
A * B * A B AB*
(A+B)/C /+ ABC AB+C/
(A*B) + (D-C) +*AB - DC AB*DC-+
9. Evaluating Postfix expression:
Before evaluating the postfix expression, the following conditions must be
checked. If any one of the conditions fails, the postfix expression is invalid.
When an operator encounters the scanning process, the Stack must contain a
pair of operands or intermediate results previously calculated.
When an expression has been completely evaluated, the Stack must contain
exactly one value.
10. Algorithm for STACK:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
// Stack structure
typedef struct {
int items[MAX_SIZE];
int top;
} Stack;
// Function to initialize
the stack
void init(Stack *stack) {
stack->top = -1;
}
// Function to check if
the stack is full
int isFull(Stack *stack)
{
return stack->top ==
MAX_SIZE - 1;
}
// Function to check if
the stack is empty
int isEmpty(Stack *stack) {
return stack->top == -1;
}
// Function to push an
element onto the stack
void push(Stack *stack, int value) {
if (isFull(stack)) {
11. return;
}
stack->top++;
stack->items[stack->top] = value;
printf("Pushed %dn", value);
}
// Function to pop an element
from the stack
int pop(Stack *stack) {
int item;
if (isEmpty(stack))
{ printf("Stack is empty
n");
return -1;
}
item = stack->items[stack->top];
stack->top--;
return item;
}
// Function to peek at the top
element of the stack
int peek(Stack *stack) {
if (isEmpty(stack)) {
printf("Stack is emptyn");
return -1;
}
return stack->items[stack-
>top];
}
13. Queue Data Structure
A Queue Data Structure is used for storing and managing data in a
specific order.
It follows the principle of “First in, First out” (FIFO), where the first
element added to the queue is the first one to be removed.
Queues are commonly used in various algorithms and applications for
their simplicity and efficiency in managing data flow.
FIFO Principle of Queue:
A Queue is like a line waiting to purchase tickets, where the first person in
line is the first person served. (i.e. First come first serve).
Position of the entry in a queue ready to be served, that is, the first entry that
will be removed from the queue, is called the front of the
queue(sometimes, head of the queue), similarly, the position of the last entry
in the queue, that is, the one most recently added, is called the rear (or
the tail) of the queue.
14. Characteristics of Queue:
Queue can handle multiple data.
We can access both ends.
They are fast and flexible.
Basic Operations on Queue:
Some of the basic operations for Queue in Data Structure are:
enqueue() – Insertion of elements to the queue.
dequeue() – Removal of elements from the queue.
peek() or front()- Acquires the data element available at the front node of
the queue without deleting it.
rear() – This operation returns the element at the rear end without removing
it.
isFull() – Validates if the queue is full.
isEmpty() – Checks if the queue is empty.
size(): This operation returns the size of the queue i.e. the total number of
elements it contains.
15. Operation 1: enqueue()
Inserts an element at the end of the queue i.e. at the rear end.
The following steps should be taken to enqueue (insert) data into a queue:
o Check if the queue is full.
o If the queue is full, return overflow error and exit.
o If the queue is not full, increment the rear pointer to point to the
next empty space.
o Add the data element to the queue location, where the rear is pointing.
o return success.
16. // Function to add an item to the queue.
// It changes rear and size
void enqueue(struct Queue* queue, int item)
{
if (isFull(queue))
return;
queue->rear = (queue->rear + 1) % queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
printf("%d enqueued to queuen", item);
}
17. Complexity Analysis:
Time Complexity: O(1)
Space Complexity: O(N)
Operation 2: dequeue()
This operation removes and returns an element that is at the front end of
the queue.
The following steps are taken to perform the dequeue operation:
Check if the queue is empty.
If the queue is empty, return the underflow error and exit.
If the queue is not empty, access the data where the front is pointing.
Increment the front pointer to point to the next available data element.
The Return success.
18. // Function to remove an item from queue.
// It changes front and size
int dequeue(struct Queue* queue)
{
if (isEmpty(queue)) { printf("
nQueue is emptyn");
return;
}
int item = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->capacity;
queue->size = queue->size - 1;
return item;
}
Complexity Analysis:
Time Complexity: O(1)
Space Complexity: O(N)
Operation 3: front()
This operation returns the element at the front end without removing
it. The following steps are taken to perform the front operation:
If the queue is empty return the most minimum value.
otherwise, return the front value.
// Function to get front of queue
19. int front(struct Queue* queue)
{
if (isempty(queue))
return INT_MIN;
return queue->arr[queue->front];
}
Complexity Analysis:
Time Complexity: O(1)
Space Complexity: O(N)
Operation 4 : rear()
This operation returns the element at the rear end without removing
it. The following steps are taken to perform the rear operation:
If the queue is empty return the most minimum value.
otherwise, return the rear value.
// Function to get rear of queue
int front(struct Queue* queue)
{
if (isempty(queue))
return INT_MIN;
return queue->arr[queue->rear];
}
Complexity Analysis:
20. Time Complexity: O(1)
Space Complexity: O(N)
Operation 5: isEmpty():
This operation returns a boolean value that indicates whether the queue is empty or
not.
The following steps are taken to perform the Empty operation:
check if front value is equal to -1 or not, if yes then return true means queue
is empty.
Otherwise return false, means queue is not empty
// Queue is empty when size is 0
bool isEmpty(struct Queue* queue)
{
return (queue->size == 0);
}
Complexity Analysis:
Time Complexity: O(1)
Space Complexity: O(N)
Operation 6 : isFull()
This operation returns a boolean value that indicates whether the queue is full or
not.
The following steps are taken to perform the isFull() operation:
Check if front value is equal to zero and rear is equal to the capacity of
queue if yes then return true.
otherwise return false
21. // Queue is full when size becomes equal to the capacity
bool isFull(struct Queue* queue)
{
return (queue->size == queue->capacity);
}
Complexity Analysis:
Time Complexity: O(1)
Space Complexity: O(N)
Operation 7: size()
This operation returns the size of the queue i.e. the total number of elements it
contains.
queuename.size()
Parameters : No parameters are passed
Returns : Number of elements in the container
Complexity Analysis:
Time Complexity: O(1)
Space Complexity: O(N)
22. Types of Queue:
There are four different types of queue that are listed as follows -
o Simple Queue or Linear Queue
o Circular Queue
o Priority Queue
o Double Ended Queue (or Deque)
Simple Queue or Linear Queue:
In Linear Queue, an insertion takes place from one end while the deletion
occurs from another end.
The end at which the insertion takes place is known as the rear end, and the
end at which the deletion takes place is known as front end.
It follows the FIFO rule.
23. The major drawback of using a linear Queue is that insertion is done only
from the rear end.
If the first three elements are deleted from the Queue, we cannot insert more
elements even though the space is available in a Linear Queue.
In this case, the linear Queue shows the overflow condition as the rear is
pointing to the last element of the Queue.
Algorithm for Linear Queue:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
// Linear Queue
structure
typedef struct {
int items[MAX_SIZE];
int front, rear;
} LinearQueue;
// Function to initialize the linear queue
void init(LinearQueue *queue) {
queue->front = -1;
queue->rear = -1;
}
// Function to check if the queue is full
int isFull(LinearQueue *queue) {
24. return queue->rear == MAX_SIZE - 1;
}
// Function to check if the queue is empty
int isEmpty(LinearQueue *queue) {
return queue->front == -1;
}
// Function to add an element to the
queue
void enqueue(LinearQueue *queue, int value) {
if (isFull(queue)) {
printf("Queue is fulln");
return;
}
if (isEmpty(queue)) {
queue->front = 0;
}
queue->rear++;
queue->items[queue->rear] = value;
printf("Inserted %dn", value);
}
// Function to remove an element from the queue
int dequeue(LinearQueue *queue) {
25. int item;
if (isEmpty(queue))
{ printf("Queue is empty
n"); return -1;
}
item = queue->items[queue->front];
if (queue->front == queue->rear) {
queue->front = -1;
queue->rear = -1;
} else {
queue->front++;
}
return item;
}
// Function to display the elements of the queue
void display(LinearQueue *queue) {
if (isEmpty(queue)) {
printf("Queue is emptyn");
return;
}
printf("Front -> ");
27. Circular Queue:
In Circular Queue, all the nodes are represented as circular.
It is similar to the linear Queue except that the last element of the queue is
connected to the first element.
It is also known as Ring Buffer, as all the ends are connected to another
end.
The drawback that occurs in a linear queue is overcome by using the
circular queue.
If the empty space is available in a circular queue, the new element can
be added in an empty space by simply incrementing the value of rear.
The main advantage of using the circular queue is better memory
utilization.
Example of Circular Queue:
31. Algorithm of Circular Queue:
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 5
// Circular Queue structure
typedef struct {
int items[MAX_SIZE];
int front, rear;
} CircularQueue;
// Function to initialize the circular queue
void init(CircularQueue *queue) {
queue->front = -1;
queue->rear = -1;
}
// Function to check if the queue is full
int isFull(CircularQueue *queue) {
return (queue->front == 0 &&
queue->rear == MAX_SIZE - 1) ||
(queue->rear
== (queue->front - 1) % (MAX_SIZE
- 1));
}
// Function to check if the queue is empty
int isEmpty(CircularQueue *queue) {
return queue->front == -1;
}
// Function to add an element to the
queue
void enqueue(CircularQueue *queue, int value) {
if (isFull(queue)) {
32. printf("Queue is full
n"); return;
}
if (isEmpty(queue)) {
queue->front = 0;
queue->rear = 0;
} else {
queue->rear =
(queue->rear + 1)
% MAX_SIZE;
}
queue->items[queue->rear] = value;
printf("Inserted %dn", value);
}
// Function to remove an element from the queue
int dequeue(CircularQueue *queue) {
int item;
if (isEmpty(queue))
{ printf("Queue is empty
n"); return -1;
}
if (queue->front == queue-
>rear) {
item = queue->items[queue->front];
queue->front = -1;
queue->rear = -1;
} else {
item = queue->items[queue->front];
33. queue->front = (queue->front + 1) % MAX_SIZE;
}
return item;
}
// Function to display the elements of the queue
void display(CircularQueue *queue) {
if (isEmpty(queue))
{ printf("Queue is empty
n"); return;
}
printf("Front -> ");
for (int i = queue->front; i != queue->rear; i = (i + 1) % MAX_SIZE) {
printf("%d -> ", queue->items[i]);
}
printf("%d -> Rearn", queue->items[queue->rear]);
}
int main() {
CircularQueue queue;
init(&queue);
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
enqueue(&queue, 4);
enqueue(&queue, 5);
enqueue(&queue, 6); // Queue is full