Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Stack

Presented by:
Maritess Tolentino
Maricris Z.Alemania
Charmaine Delos Trios
STACK
A stack is an Abstract
Data Type (ADT),
commonly used in most
programming languages.
It is named stack as it
behaves like a real-world
stack, for example – a
deck of cards or a pile of
plates, etc.
A real-world stack allows operations at one end only.
For example, we can place or remove a card or plate
from the top of the stack only. Likewise, Stack ADT
allows all data operations at one end only. At any given
time, we can only access the top element of a stack.
What is a Stack?
Stack is a linear data structure in
which the insertion and deletion
operations are performed at only
one end. In a stack, adding and
removing of elements are
performed at single position
which is known as "top". That
means, new element is added at
top of the stack and an element
is removed from the top of the
stack. In stack, the insertion and
deletion operations are
performed based on LIFO (Last
In First Out) principle.
In a stack, the insertion operation is performed using a
function called "push" and deletion operation is
performed using a function called "pop".

In the figure, PUSH and POP operations are


performed at top position in the stack. That means,
both the insertion and deletion operations are
performed at one end (i.e., at Top
A stack data structure can be defined as follows...

Stack is a linear data structure in which


the operations are performed based on
LIFO principle.
Stack can also be defined as

"A Collection of similar data items in


which both insertion and deletion
operations are performed based on
LIFO principle".
Operations on a Stack
The following operations are performed on the stack...

1. Push (To insert an element on to the stack)


2. Pop (To delete an element from the stack)
3. Display (To display elements of the stack)
Stack data structure can be implement in two ways. They
are as follows...

1. Using Array
2. Using Linked List

When stack is implemented using array, that stack can


organize only limited number of elements. When stack
is implemented using linked list, that stack can organize
unlimited number of elements.
HOW IT WORKS
Stack as Abstract Data Type
He stack of elements of any particular
type is a finite sequence of elements of
that type together with the following
operations:

Initialize the stack to be empty


Determine whether stack is empty or not
Check whether stack is full or not
If stack is not full, add or insert a new
node at the top of the stack. This
operation is termed as Push Operation
If the stack is not empty, then retrieve the
node at its top
If the stack is not empty, the delete the
node at its top. This operation is called as
Pop operation
Stack Representation

The following diagram depicts a stack and its operations



Basic Operations
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() − Pushing (storing) an element on the stack.

pop() − Removing (accessing) an element from the


stack.
When data is PUSHed onto stack.

To use a stack efficiently, we need to check the status of stack as well. For the
same purpose, the following functionality is added to stacks −

peek() − get the top data element of the stack, without removing it.

isFull() − check if stack is full.

isEmpty() − check if stack is empty.

At all times, we maintain a pointer to the last PUSHed data on the stack. As this
pointer always represents the top of the stack, hence named top. The top pointer
provides top value of the stack without actually removing it
HOW IT WORKS

http://www.cs.armstrong.edu/liang/animation/web/S
tack.html
SAMPLE OF CODES
Representation of Stack using Arrays
Stack can be represented in memory with the use of
arrays. To do this job you need to maintain a linear
array STACK, a pointer variable top which contains the
top element
#include <iostream>
#include<stdlib.h>
using namespace std;

class stack {
 int stk[5];
 int top;

public:
 stack()
 {
 top = -1;
 }
 void push(int x)
 {
 if (top > 4) {
 cout << "stack overflow";
 return;
 }
 stk[++top] = x;
 cout << "inserted " << x;
 }
 void pop()
 {
 if (top < 0) {
 cout << "stack underflow";
 return;
 }
 cout << "deleted " << stk[top--];
 }
 void display()
 {
 if (top < 0) {
 cout << " stack empty"; return; } for (int i = top; i >= 0; i--)
 cout << stk[i] << " ";
 }
};

int main()
{
 int ch;
 stack st;
 while (1) {
 cout << "\n1.push 2.pop 3.display 4.exit\nEnter ur choice: "; cin >> ch;
 switch (ch) {
 case 1:
 cout << "enter the element: "; cin >> ch;
 st.push(ch);
 break;
 case 2:
 st.pop();
 break;
 case 3:
 st.display();
 break;
 case 4:
 exit(0);
 }
 }
}
Reference:
https://
www.tutorialspoint.com/data_structures_algorithms/
stack_algorithm.htm
https://
www.w3schools.in/data-structures-tutorial/stack/
http://btechsmartclass.com/DS/U2_T1.html

You might also like