0% found this document useful (0 votes)
21 views

Stacks

Stacks are a linear data structure that follow the LIFO (last-in, first-out) principle. Elements are inserted and removed from only one end called the top. Common stack operations include push to insert, pop to remove, peek to inspect the top element, and functions to check if the stack is empty or return the size. Stacks have applications in reversing polish notation, function call handling, expression evaluation, syntax parsing, undo operations, and backtracking algorithms.

Uploaded by

calvinakiweley54
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)
21 views

Stacks

Stacks are a linear data structure that follow the LIFO (last-in, first-out) principle. Elements are inserted and removed from only one end called the top. Common stack operations include push to insert, pop to remove, peek to inspect the top element, and functions to check if the stack is empty or return the size. Stacks have applications in reversing polish notation, function call handling, expression evaluation, syntax parsing, undo operations, and backtracking algorithms.

Uploaded by

calvinakiweley54
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/ 22

STACKS

DATA STRUCTURES AND ALGORITHM


WEEK 6
DEFINITION

DIAGRAMATIC VIEW OF Stacks in Data Structures is a


linear type of data structure that
STACKS follows the LIFO (Last-In-First-
Out) principle.
 LIFO implies that the
element that is inserted last,
comes out first.
 FILO implies that the
element that is inserted first,
comes out last.
Stacks have only one end, that is
top.
 Adding and removing
elements is done at one end .
The top of the stack is always
the element that is currently
accessible for viewing or
manipulation.
OPERATION ON STACKS
push() to insert an element into the stack
pop() to remove an element from the stack
Peek() retrieving the topmost element in the stack
without removing.
top() Returns the top element of the stack.
isEmpty() returns true if stack is empty else false.
size() returns the size of stack.
OPERATION ON STACKS
PUSH
Check if the stack is full.
If full, it is overflow condition.
Else, increment the top by 1.
 Insert a new element where the
top is pointing
Return success.
OPERATION ON STACKS
POP
Check if the stack is empty.
If empty, it is underflow
condition.
If not, access the topmost data
element
Decrement the top by one.
Return success.
ARRAY IMPLEMENTATION OF
STACKS
ARRAY IMPLEMENTATION OF
STACKS
 OPERATION ON STACKS
PUSH
Push operation has the following two steps:
Increment the top variable of the stack so that it can
refer to the next memory location.
Add a data element at the increment top position.
Stack data structure states an overflow condition when
you try to insert an element into the stack when full.
CONT’D
POP
The pop operation has two following steps:
The value of the top variable will be incremented by one
whenever you delete an item from the stack.
The topmost variable of the stack is stored in another
variable, and then the value of the top variable will be
decremented by one.
The pop operation returns the deleted element that was
stored in another variable as a result.
ARRAY IMPLEMENTATION OF
STACKS
ARRAY IMPLEMENTATION OF STACKS IN C++
#include <bits/stdc++.h> bool Stack::push(int x {
using namespace std; if (top >= (MAX - 1)) {
#define MAX 1000 cout << "Stack Overflow";
class Stack { int top; return false; }

public: else { a[++top] = x;

int a[MAX]; cout << x << " pushed into


stack\n"; return true; } int
// Maximum size of Stack
Stack::pop()
Stack() { top = -1; }
{ if (top < 0) {cout << "Stack
bool push(int x); Underflow";
int pop(); return 0; } else {
int peek(); int x = a[top--];
bool isEmpty();}; return x;}}
ARRAY IMPLEMENTATION OF STACKS
IN C++
int Stack::peek(){ //print top element of stack
if (top < 0) { after popping
cout << "Stack is Empty"; cout << "Top element is : " <<
return 0; } s.peek() << endl;
else { int x = a[top]; //print all elements in stack:
return x;}} cout <<"Elements present in
bool Stack::isEmpty(){ stack : ";
return (top < 0);} while(!s.isEmpty()){ //
print top element in stack
// Driver program to test above
functions cout << s.peek() <<" ";
int main(){class Stack s; // remove top element in stack
s.push(10);s.push(20);s.push(30); s.pop();}
cout << s.pop() << " Popped from return 0;}
LINKEDLIST IMPLEMENTATION OF STACKS
LINKEDLIST IMPLEMENTATION OF
STACKS
OPERATION ON STACKS
 PUSH
 Pushing a node in the linked list is quite different from inserting an
element in the array.
Push operation on stack implementation using linked-list involves several steps:
 Create a node first and allocate memory to it.
 If the list is empty, then the node is pushed as the first node of the linked
list. This operation assigns a value to the data part of the node and gives
NULL to the address part of the node.
CONT’D
If some nodes are already in the linked list, then we
have to add a new node at the beginning to the list
not to violate the Stack's property. For this, assign
the element to the address field of the new node and
make a new node which will be starting node of the
list.
CONT’D
 An overflow condition occurs when we try to push an operation if
the Stack is already full.
CONT’D
Pop
The popping node from the linked list is different from the
popping element from the array.
To perform the pop operation involves the following steps:
In Stack, the node is removed from the end of the linked
list.
Therefore, must delete the value stored in the head pointer,
and the node must get free. The following link node will
become the head node now.
CONT’D
An underflow condition will occur when we try to
pop an operation when the Stack is already empty.
The Stack will be meaningless if the head pointer of
the list points to NULL.
CONT’D
CODE IMPLEMENTATION OF STACKS IN
LINKEDLIST
#include <iostream> void pop() {if (
template<typename T> !isEmpty()) {
class Node { public: T data; Node<T>* temptop;top = top-
Node* next; >next;delete temp;

Node(T value){data = value;next }} T peek() {


nullptr; if (!isEmpty()) {
}}; template<typename T> return top->data;}
class Stack { private:Node<T>* top; // Handle empty stack case
public: Stack() { top = nullptr;} appropriately
~Stack() {while (!isEmpty()) throw
{ pop();}} void push(T value) std::runtime_error("Stack is
{ Node<T>* newNode = new empty");
Node<T>(value); newNode->next = top; } bool isEmpty() { return top ==
top = newNode; } nullptr;}};
COND’T
int main() {
Stack<int> myStack;
myStack.push(10);
myStack.push(20);
myStack.push(30);
std::cout << "Top element: " << myStack.peek() <<
std::endl;
myStack.pop();
std::cout << "Top element after pop: " << myStack.peek()
<< std::endl;
return 0;
}
APPLICATION OF STACKS
 Polish Notation Conversion: Stacks are commonly used to convert infix
expressions to postfix expressions (Reverse Polish Notation) or prefix
expressions (Polish Notation). This conversion involves using a stack to
keep operators until they can be placed in the correct order.
 Function Calls: Stacks are used in programming languages to manage
function calls. When a function is called, the address of the next instruction
after the call is pushed onto the stack. When the function returns, the address
is popped from the stack, and execution resumes at that point.
 Expression Evaluation: Stacks can be used to evaluate postfix expressions
efficiently. As operands are encountered, they are pushed onto the stack.
When an operator is encountered, the required number of operands are
popped from the stack, the operation is performed, and the result is pushed
back onto the stack.
APPLICATION OF STACKS
Syntax Parsing: Stacks are utilized in syntax parsing
algorithms like recursive descent parsing and shift-reduce
parsing. They help in tracking the structure of the syntax being
parsed, ensuring correct ordering of tokens.
 Undo Functionality: Stacks can be used to implement undo
functionality in text editors or other applications. Each operation that
modifies the state is pushed onto the stack, allowing users to undo
changes by popping operations from the stack.
 Backtracking: Stacks are useful in backtracking algorithms, such as
depth-first search (DFS). During exploration of a search space, the stack
keeps track of the current path. If a dead-end is reached, the algorithm
can backtrack by popping elements from the stack.

You might also like