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.
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 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 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.
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
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.
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.
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.
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 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 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.
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
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.
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.
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.
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.
Data structures like stacks, queues, and priority queues can be implemented in Python using built-in data types and modules. Stacks follow LIFO order, with new items added to the top and removed from the top. Queues follow FIFO order, with new items added to the back and removed from the front. Priority queues order items by priority, so the highest or lowest priority item is removed first depending on the implementation.
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.
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.
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
A stack is a data structure where items can only be inserted and removed from one end. The last item inserted is the first item removed (LIFO). Common examples include stacks of books, plates, or bank transactions. Key stack operations are push to insert, pop to remove, and functions to check if the stack is empty or full. Stacks can be used to implement operations like reversing a string, converting infix to postfix notation, and evaluating arithmetic expressions.
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.
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.
This document discusses stacks, including what they are, their operations, implementations, and applications. Stacks follow LIFO order and can be implemented using arrays or linked lists. Common stack operations are push, pop, peek, isEmpty, and size. Stacks have various applications, including converting expressions between infix, postfix, and prefix notation.
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
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 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
A stack is a linear data structure that follows the LIFO (last-in, first-out) principle. Elements are inserted and removed from only one end called the top of the stack. Common stack operations are push, which adds an element to the top, pop, which removes an element from the top, and peek, which returns the top element without removing it. A stack can be implemented using arrays, linked lists, or other data structures. Push involves adding an element to the top of the stack, while pop involves removing and returning the top element.
This document discusses stacks and queues as abstract data types. It defines stacks as LIFO (last-in first-out) data structures where elements can only be added or removed from one end. Queues are defined as FIFO (first-in first-out) structures where elements can be added to one end and removed from the other. Common operations for each like push, pop, enqueue, dequeue are described along with pseudocode algorithms. Implementation of stacks and queues using arrays is provided as an example.
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.
The document discusses stacks and queues as abstract data types (ADTs). It describes stacks as LIFO (last-in first-out) data structures and queues as FIFO (first-in first-out) data structures. It provides implementations of stacks using arrays and discusses various stack operations like push, pop, peek, etc. It also discusses applications of stacks like expression evaluation, conversion between infix, postfix and prefix notations. Similarly, it describes the basic representation and operations of queues.
The document discusses stacks and their implementation and operations. It defines a stack as a linear data structure that follows the LIFO principle. Stacks can be implemented using arrays or linked lists. The two main operations on a stack are push and pop. Push adds an element to the top of the stack, while pop removes an element from the top. The document provides examples of these operations and discusses applications of stacks such as converting infix to postfix notation.
The document discusses stacks and their implementation and applications. Stacks are LIFO data structures that can be implemented using arrays. Key operations on stacks include push, pop, peek, isEmpty and isFull. Stacks have various applications including expression evaluation, recursion, function calls and memory management. Stacks can grow either upwards or downwards in the array.
A Guide to a Winning Interview June 2024Bruce Bennett
This webinar is an in-depth review of the interview process. Preparation is a key element to acing an interview. Learn the best approaches from the initial phone screen to the face-to-face meeting with the hiring manager. You will hear great answers to several standard questions, including the dreaded “Tell Me About Yourself”.
More Related Content
Similar to Stack and its operations, Queue and its operations
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.
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.
Data structures like stacks, queues, and priority queues can be implemented in Python using built-in data types and modules. Stacks follow LIFO order, with new items added to the top and removed from the top. Queues follow FIFO order, with new items added to the back and removed from the front. Priority queues order items by priority, so the highest or lowest priority item is removed first depending on the implementation.
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.
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.
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
A stack is a data structure where items can only be inserted and removed from one end. The last item inserted is the first item removed (LIFO). Common examples include stacks of books, plates, or bank transactions. Key stack operations are push to insert, pop to remove, and functions to check if the stack is empty or full. Stacks can be used to implement operations like reversing a string, converting infix to postfix notation, and evaluating arithmetic expressions.
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.
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.
This document discusses stacks, including what they are, their operations, implementations, and applications. Stacks follow LIFO order and can be implemented using arrays or linked lists. Common stack operations are push, pop, peek, isEmpty, and size. Stacks have various applications, including converting expressions between infix, postfix, and prefix notation.
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
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 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
A stack is a linear data structure that follows the LIFO (last-in, first-out) principle. Elements are inserted and removed from only one end called the top of the stack. Common stack operations are push, which adds an element to the top, pop, which removes an element from the top, and peek, which returns the top element without removing it. A stack can be implemented using arrays, linked lists, or other data structures. Push involves adding an element to the top of the stack, while pop involves removing and returning the top element.
This document discusses stacks and queues as abstract data types. It defines stacks as LIFO (last-in first-out) data structures where elements can only be added or removed from one end. Queues are defined as FIFO (first-in first-out) structures where elements can be added to one end and removed from the other. Common operations for each like push, pop, enqueue, dequeue are described along with pseudocode algorithms. Implementation of stacks and queues using arrays is provided as an example.
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.
The document discusses stacks and queues as abstract data types (ADTs). It describes stacks as LIFO (last-in first-out) data structures and queues as FIFO (first-in first-out) data structures. It provides implementations of stacks using arrays and discusses various stack operations like push, pop, peek, etc. It also discusses applications of stacks like expression evaluation, conversion between infix, postfix and prefix notations. Similarly, it describes the basic representation and operations of queues.
The document discusses stacks and their implementation and operations. It defines a stack as a linear data structure that follows the LIFO principle. Stacks can be implemented using arrays or linked lists. The two main operations on a stack are push and pop. Push adds an element to the top of the stack, while pop removes an element from the top. The document provides examples of these operations and discusses applications of stacks such as converting infix to postfix notation.
The document discusses stacks and their implementation and applications. Stacks are LIFO data structures that can be implemented using arrays. Key operations on stacks include push, pop, peek, isEmpty and isFull. Stacks have various applications including expression evaluation, recursion, function calls and memory management. Stacks can grow either upwards or downwards in the array.
Similar to Stack and its operations, Queue and its operations (20)
A Guide to a Winning Interview June 2024Bruce Bennett
This webinar is an in-depth review of the interview process. Preparation is a key element to acing an interview. Learn the best approaches from the initial phone screen to the face-to-face meeting with the hiring manager. You will hear great answers to several standard questions, including the dreaded “Tell Me About Yourself”.
How to Prepare for Fortinet FCP_FAC_AD-6.5 Certification?NWEXAM
Begin Your Preparation Here: https://bit.ly/3VfYStG — Access comprehensive details on the FCP_FAC_AD-6.5 exam guide and excel in the Fortinet Certified Professional - Network Security certification. Gather all essential information including tutorials, practice tests, books, study materials, exam questions, and the syllabus. Solidify your knowledge of Fortinet FCP_FAC_AD-6.5 certification. Discover everything about the FCP_FAC_AD-6.5 exam, including the number of questions, passing percentage, and the time allotted to complete the test.
IT Career Hacks Navigate the Tech Jungle with a RoadmapBase Camp
Feeling overwhelmed by IT options? This presentation unlocks your personalized roadmap! Learn key skills, explore career paths & build your IT dream job strategy. Visit now & navigate the tech world with confidence! Visit https://www.basecamp.com.sg for more details.
Learnings from Successful Jobs SearchersBruce Bennett
Are you interested to know what actions help in a job search? This webinar is the summary of several individuals who discussed their job search journey for others to follow. You will learn there are common actions that helped them succeed in their quest for gainful employment.
Leadership Ambassador club Adventist modulekakomaeric00
Aims to equip people who aspire to become leaders with good qualities,and with Christian values and morals as per Biblical teachings.The you who aspire to be leaders should first read and understand what the ambassador module for leadership says about leadership and marry that to what the bible says.Christians sh
Joyce M Sullivan, Founder & CEO of SocMediaFin, Inc. shares her "Five Questions - The Story of You", "Reflections - What Matters to You?" and "The Three Circle Exercise" to guide those evaluating what their next move may be in their careers.
Jill Pizzola's Tenure as Senior Talent Acquisition Partner at THOMSON REUTERS...dsnow9802
Jill Pizzola's tenure as Senior Talent Acquisition Partner at THOMSON REUTERS in Marlton, New Jersey, from 2018 to 2023, was marked by innovation and excellence.
In the intricate tapestry of life, connections serve as the vibrant threads that weave together opportunities, experiences, and growth. Whether in personal or professional spheres, the ability to forge meaningful connections opens doors to a multitude of possibilities, propelling individuals toward success and fulfillment.
Eirini is an HR professional with strong passion for technology and semiconductors industry in particular. She started her career as a software recruiter in 2012, and developed an interest for business development, talent enablement and innovation which later got her setting up the concept of Software Community Management in ASML, and to Developer Relations today. She holds a bachelor degree in Lifelong Learning and an MBA specialised in Strategic Human Resources Management. She is a world citizen, having grown up in Greece, she studied and kickstarted her career in The Netherlands and can currently be found in Santa Clara, CA.
Stack and its operations, Queue and its operations
1. 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.
2. 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.
3. 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.
4. POPoperation:
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.
5. 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".
6. 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.
7. 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-+
8. 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.
9. 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)) {
printf("Stack is fulln");
10. 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 emptyn");
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];
}
12. 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.
13. 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.
14. 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.
15. // 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);
}
16. 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.
17. // 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
18. 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:
19. 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
20. // 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)
21. 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.
22. 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) {
23. 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) {
24. int item;
if (isEmpty(queue)) {
printf("Queue is emptyn");
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 -> ");
26. 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:
27.
28.
29.
30. 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)) {
31. printf("Queue is fulln");
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 emptyn");
return -1;
}
if (queue->front == queue->rear) {
item = queue->items[queue->front];
queue->front = -1;
queue->rear = -1;
} else {
item = queue->items[queue->front];
32. 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 emptyn");
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