0% found this document useful (0 votes)
7 views35 pages

Stack in Data Structure in C (1)

A stack is a linear data structure that operates on a Last In First Out (LIFO) principle, allowing operations like Push, Pop, and Peek. It can be implemented using arrays or linked lists, with each method having its own advantages regarding memory management and performance. Stacks are widely used in applications such as function call management, expression evaluation, and implementing undo mechanisms.

Uploaded by

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

Stack in Data Structure in C (1)

A stack is a linear data structure that operates on a Last In First Out (LIFO) principle, allowing operations like Push, Pop, and Peek. It can be implemented using arrays or linked lists, with each method having its own advantages regarding memory management and performance. Stacks are widely used in applications such as function call management, expression evaluation, and implementing undo mechanisms.

Uploaded by

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

Stack in Data Structure in C

By Your Name
What is a Stack?
Definition of Stack
A stack is a linear data structure that follows the Last In First Out (LIFO)
principle. Elements are added and removed from one end known as the
top. It only allows access to the top element without affecting other
elements.

Characteristics of a Stack
Stack operations involve Push (adding an element to the top), Pop
(removing the top element), and Peek (viewing the top element
without removal). The size of the stack changes dynamically
during operations.

Key Concepts
Stacks are commonly implemented using arrays or linked lists. They are
essential in function calls, expression evaluation, and undo mechanisms in
applications.

Photos provided by Unsplash


Operations on a Stack

Peek Operation Push Operation Pop Operation


Peek allows viewing the top Push adds an element to the top of Pop removes the top element from
element without removing it from the stack. It increments the stack the stack. It decrements the stack
the stack. It helps in checking the pointer before inserting the pointer after removing the top
topmost element for various element. It is fundamental for element. It is used to retrieve and
operations without affecting the inserting new data. remove data.
stack's content.
Implementation of Stack in C
Array Implementation
Stacks can be implemented using arrays in C. The stack top is managed
using an index. Stack overflow and underflow conditions should be
handled to prevent data loss.

Linked List Implementation


Stacks can also be implemented using linked lists in C. Each node in the
linked list represents an element, and pointers are used for traversal.
This method allows dynamic memory allocation.

Performance Considerations
The choice between array and linked list implementations depends on
the application requirements. Arrays offer faster access while linked
lists provide flexibility in memory usage.

Photos provided by Unsplash


Advantages of Using Stacks

Efficient Memory Management Simplicity in Implementation


Stacks use memory efficiently as it operates on a Last Stack operations are straightforward to implement,
In First Out basis. It simplifies memory cleanup after making them ideal for applications requiring temporary
function execution, preventing memory leaks. data storage. They offer a simple interface for
managing data.

Function Call Management


Stacks are crucial for managing function calls, storing
parameters and return addresses. This mechanism
ensures proper execution flow and memory utilization.
Stack Visualization

40% 30% 20% 10%

Push Operation Pop Operation Peek Operation Empty Stack


Push operation adds Pop operation removes Peek operation views Represents the
elements to the stack. It elements from the the top element without remaining 10% of the
represents 40% of the stack. It accounts for removing it. It makes up stack operations when
stack operations. 30% of the stack 20% of the stack the stack is empty.
operations. operations.
Stack Applications

Application Description

Expression Evaluation Used in compilers for evaluating arithmetic expressions efficiently.

Undo Mechanism Enables the 'undo' functionality in text editors or graphic software.

Function Call Stack Manages function calls and parameter passing in programming languages.

Backtracking Algorithms Supports backtracking algorithms in solving problems like the N-Queens puzzle.
Linked List Implementation of Stacks
Expression Evaluation

• Evaluate an expression represented by a String. The


expression can contain parentheses, you can assume
parentheses are well-matched. For simplicity, you can
assume only binary operations allowed are +, -, *, and /.
Arithmetic Expressions can be written in one of three
forms:
• Infix Notation: Operators are written between the
operands they operate on, e.g. 3 + 4.
• Prefix Notation: Operators are written before the
operands, e.g + 3 4
• Postfix Notation: Operators are written after
operands.
Infix expression evalution
• An infix expression is evaluated using two stacks, one for operator and another for operands. The infix sting is read in an array, from which symbols/characters are
fetched one by one and the following checks are performed:
• If symbol is an operand, push it in operand’s stack.
• Initialize operator stack with a dummy operator with the least precedence (say #).
• If the symbol is an operator, then do the following:
• Pop an operator from the operator stack.
• Check its precedence with the symbol.
• If the precedence of the symbol is higher, then push the popped operator and then the symbol.
• Else if the precedence of symbol is lower (or equal to) than the popped operator, pop two operands, (opnd2 and opnd1) and perform operation specified by
popped operator.
• Push the result in operand’s stack.
• Repeat above steps until the symbol becomes less than the popped operator.
• After the string ends, start popping an operator from the operator stack and two operands from operand stack.
• Repeat step (d) until dummy operator (#) is found.
• Pop the value from the operand stack and return it.
• Scan the infix expression from left to right.
• 2. a)If the scanned symbol is left parenthesis, push it onto the
stack.
• b) If the scanned symbol is an operand, then place directly in the
postfix
• expression (output).
• c) If the symbol scanned is a right parenthesis, then go on popping
all

Procedur • the items from the stack and place them in the postfix expression
till
• we get the matching left parenthesis.

e • d) If the scanned symbol is an operator, then go on removing all


the
• operators from the stack and place them in the postfix expression,
if
• and only if the precedence of the operator which is on the top of
the
• stack is greater than (or greater than or equal) to the precedence
of
• the scanned operator and push the scanned operator onto the
stack
• otherwise, push the scanned operator onto the stack.
Procedure for
infix to prefix

• The precedence rules for


converting an expression from
infix to prefix are identical. The
• only change from postfix
conversion is that traverse the
expression from right to left
• and the operator is placed before
the operands rather than after
them. The prefix form
• of a complex expression is not
the mirror image of the postfix
form.

You might also like