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

Stack

Stacks are a linear data structure that operates on the LIFO principle, allowing insertion and deletion only from the top. They can be implemented using arrays or linked lists, and common operations include push, pop, isEmpty, isFull, peek, count, and display. While stacks offer efficient data management and memory control, they have limitations such as limited memory size and the risk of stack overflow.
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)
7 views6 pages

Stack

Stacks are a linear data structure that operates on the LIFO principle, allowing insertion and deletion only from the top. They can be implemented using arrays or linked lists, and common operations include push, pop, isEmpty, isFull, peek, count, and display. While stacks offer efficient data management and memory control, they have limitations such as limited memory size and the risk of stack overflow.
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/ 6

Data Structures

Unit 2: Stacks
Stack
Stacks in Data Structures is a linear type of data structure that follows the LIFO (Last-In-First-Out)
principle and allows insertion and deletion operations from one end of the stack data structure, that is
top. Implementation of the stack can be done by contiguous memory which is an array, and non-
contiguous memory which is a linked list.

In other words, a stack can be defined as a container in which insertion and deletion can be done from the
one end known as the top of the stack.

Real-life examples of a stack are a deck of cards, piles of books, piles of money, and many more.
This example allows you to perform operations from one end only, like when you insert and remove
new books from the top of the stack. It means insertion and deletion in the stack data structure can be
done only from the top of the stack. You can access only the top of the stack at any given point in time.

• Inserting a new element in the stack is termed a push operation.


• Removing or deleting elements from the stack is termed pop operation.

Stack Representation in Data Structures

Advantages and disadvantages stack

Advantages

1. Efficient data management: Stack helps you manage the data in a LIFO (last in, first out)
method, which is not possible with a Linked list and array.
2. Efficient management of functions: When a function is called, the local variables are stored
in a stack, and it is automatically destroyed once returned.
3. Control over memory: Stack allows you to control how memory is allocated and deallocated.
4. Smart memory management: Stack automatically cleans up the object.
5. Not easily corrupted: Stack does not get corrupted easily; hence it is more secure and
reliable.
6. Does not allow resizing of variables: Variables cannot be resized.

Page | 1
Data Structures

Disadvantages

1. Limited memory size: Stack memory is very limited.


2. Chances of stack overflow: Creating too many objects on the stack can increase the risk of
stack overflow.
3. Random access is not possible: In a stack, random accessing the data is not possible.
4. Unreliable: When variable storage will get overwritten, it will sometimes leads to undefined
behaviour of the function or program.
5. Undesired termination: The stack will fall outside of the memory area, which might lead to
an abnormal termination.

Working of Stack

Stack works on the LIFO pattern. As we can observe in the below figure 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.

Since our stack is full as the size of the stack is 5. In the above cases, we can observe that it goes from
the top to the bottom when we were entering the new element in the stack. The stack gets filled up
from the bottom to the top.

When we perform the delete operation on the stack, there is only one way for entry and exit as the other
end is closed. It follows the LIFO pattern, which means that the value entered first will be removed
last. In the above case, the value 5 is entered first, so it will be removed only after the deletion of all
the other elements.

Standard Stack Operations

The following are some common operations implemented on the stack:

o 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.

Page | 2
Data Structures

o 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.
o isEmpty(): It determines whether the stack is empty or not.
o isFull(): It determines whether the stack is full or not.'
o peek(): It returns the element at the given position.
o count(): It returns the total number of elements available in a stack.
o display(): It prints all the elements available in the stack.

Implementation of Stack in Data Structures


You can perform the implementation of stacks in data structures using two data structures that are an
array and a linked list.
• Array: In array implementation, the stack is formed using an array. All the operations are
performed using arrays. You will see how all operations can be implemented on the stack in
data structures using an array data structure.

Implementation of stack using array

#include <stdio.h>
#include <stdlib.h>
#define SIZE 4

int top = -1, stack[SIZE];

void push();
void pop();
void show();
void peek();
int main()
{
int choice;

while (1)
{
printf("\nPerform operations on the stack:");
printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.Top element\n5.End");
printf("\n\nEnter the choice: ");

Page | 3
Data Structures

scanf("%d", &choice);

switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
peek();
break;
case 5:
exit(0);

default:
printf("\nInvalid choice!!");
}
}
}

void push()
{
int x;

if (top == SIZE - 1)
{
printf("\nOverflow!!");
}
else
{
printf("\nEnter the element to be added onto the stack: ");
scanf("%d", &x);
top = top + 1;
stack[top] = x;
}
}

void pop()
{
if (top == -1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nPopped element: %d", stack[top]);
top = top - 1;

Page | 4
Data Structures

}
}

void show()
{
if (top == -1)
{
printf("\nUnderflow!!");
}
else
{
printf("\nElements present in the stack: \n");
for (int i = top; i >= 0; --i)
printf("%d\n", stack[i]);
}
}

void peek()
{
if (top == -1)
{
printf("\nUnderflow!!");
}
else
{
printf("\n peek element is %d",stack[top]);
}
}

Page | 5
Data Structures

• Linked-List: Every new element is inserted as a top element in the linked list implementation
of stacks in data structures. That means every newly inserted element is pointed to the top.
Whenever you want to remove an element from the stack, remove the node indicated by the
top, by moving the top to its previous node in the list.

Page | 6

You might also like