Professional Documents
Culture Documents
Stack: Presented By: Maritess Tolentino Maricris Z.Alemania Charmaine Delos Trios
Stack: Presented By: Maritess Tolentino Maricris Z.Alemania Charmaine Delos Trios
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".
1. Using Array
2. Using Linked List
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.
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