0% found this document useful (0 votes)
12 views4 pages

Stack

A stack is a Last In First Out (LIFO) data structure where all operations are performed at one end, known as the top. Key operations include creating a stack, pushing and popping elements, and checking if the stack is empty or full. Stacks have various applications such as expression evaluation, backtracking, function calls, and syntax parsing.

Uploaded by

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

Stack

A stack is a Last In First Out (LIFO) data structure where all operations are performed at one end, known as the top. Key operations include creating a stack, pushing and popping elements, and checking if the stack is empty or full. Stacks have various applications such as expression evaluation, backtracking, function calls, and syntax parsing.

Uploaded by

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

Stack

1. Define Stack
A Stack is an ordered list in which all insertions (Push operation) and deletion (Pop
operation) are made at one end, called the top. The topmost element is pointed by top. The top is
initialized to -1 when the stack is created that is when the stack is empty. In a stack S = (a1,an), a1
is the bottom most element and element a is on top of element ai-1. Stack is also referred as Last
In First Out (LIFO) list.

2. What are the various Operations performed on the Stack?


The various operations that are performed on the stack are

CREATE(S) – Creates S as an empty stack.

PUSH(S,X) – Adds the element X to the top of the stack.

POP(S) – Deletes the top most elements from the stack.

TOP(S) – returns the value of top element from the stack.

ISEMTPTY(S) – returns true if Stack is empty else false.

ISFULL(S) - returns true if Stack is full else false.


Important Algorithms of Stack Operations

Stack Insertion: push()


The push() is an operation that inserts elements into the stack. The following is an algorithm that
describes the push() operation in a simpler way.
Algorithm
1. Checks if the stack is full.
2. If the stack is full, produces an error and exit.
3. If the stack is not full, increments top to point next
empty space.
4. Adds data element to the stack location, where top
is pointing.
5. Returns success.

Stack Deletion: pop()


The pop() is a data manipulation operation which removes elements from the stack. The following
pseudo code describes the pop() operation in a simpler way.
Algorithm
1. Checks if the stack is empty.
2. If the stack is empty, produces an error and exit.
3. If the stack is not empty, accesses the data element at
which top is pointing.
4. Decreases the value of top by 1.
5. Returns success.

Verifying whether the Stack is full: isFull()


The isFull() operation checks whether the stack is full. This operation is used to check the status of
the stack with the help of top pointer.
Algorithm
1. START
2. If the size of the stack is equal to the top position of the stack,
the stack is full. Return 1.
3. Otherwise, return 0.
4. END

Verifying whether the Stack is empty: isEmpty()


The isEmpty() operation verifies whether the stack is empty. This operation is used to check the status
of the stack with the help of top pointer.
Algorithm
1. START
2. If the top value is -1, the stack is empty. Return 1.
3. Otherwise, return 0.
4. END
Example
3. Write the postfix form for the expression -A+B-C+D?

A-B+C-D+
4. What are the postfix and prefix forms of the expression?

A+B*(C-D)/(P-R)

Postfix form: ABCD-*PR-/+

Prefix form: +A/*B-CD-PR

5. Explain the usage of stack in recursive algorithm implementation?


In recursive algorithms, stack data structures is used to store the return address when a recursive
call is encountered and also to store the values of all the parameters essential to the current state
of the function

Operations Time Complexity

push() O(1)

pop() O(1)

top() or peek() O(1)

isEmpty() O(1)

isFull() O(1)

6. Applications of Stack
 Expression Evaluation and Conversion:
Stacks are used to evaluate and convert expressions between infix, prefix, and postfix notations.
 Backtracking:
Stacks are useful in backtracking algorithms, allowing the program to return to a previous state.
 Function Calls:
Stacks manage function calls by storing return addresses and local variables.
 Parenthesis Checking:
Stacks can efficiently verify the correct matching and nesting of parentheses in expressions.
 String Reversal:
Stacks facilitate reversing strings by pushing characters onto the stack and then popping them.
 Syntax Parsing:
Compilers and interpreters use stacks for parsing the syntax of programming languages.
 Memory Management:
Stacks assist in memory allocation and deallocation in some systems.

You might also like