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

Stack: Prepared By: Eng. Ahmed & Mohamed Taha

The document discusses stacks and their implementation. It defines a stack as a collection of items that can have items added or removed from only one end, called the top. Items are added using push and removed using pop. It then discusses stack operations like push, pop, and describes implementing a stack using both arrays with indexes and pointers. The implementation includes functions for initialization, checking if empty/full, and performing push/pop operations on the stack.

Uploaded by

Ahmed M. Hegazy
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)
69 views

Stack: Prepared By: Eng. Ahmed & Mohamed Taha

The document discusses stacks and their implementation. It defines a stack as a collection of items that can have items added or removed from only one end, called the top. Items are added using push and removed using pop. It then discusses stack operations like push, pop, and describes implementing a stack using both arrays with indexes and pointers. The implementation includes functions for initialization, checking if empty/full, and performing push/pop operations on the stack.

Uploaded by

Ahmed M. Hegazy
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/ 20

Stack

Prepared by:

Eng. Ahmed & Mohamed Taha

Agenda

Introduction to Stack.
How Stack works.
Operations performed on Stack.

Stack Implementation.

Introduction to stack

Stack is an ordered collection of items in which new data


items may be added to or deleted from only one end, called
the top of the stack.

All the addition and deletion in a stack is done from the top
of the stack, the last added element will be first removed
from the stack. That is why the stack is also called Last-inFirst-out (LIFO).
The insertion (or addition) operation is referred to as push.
The deletion (or remove) operation as pop.
A stack is said to be empty or underflow, if the stack
contains no elements. At this point the top of the stack is
present at the bottom of the stack.
A stack is overflow when it becomes full, i.e., no other
elements can be pushed onto the stack. At this point the
top pointer is at the highest location of the stack.

Introduction to Stack

Company Logo

Stack Working

Company Logo

OPERATIONS PERFORMED ON STACK

PUSH: The process of adding (or inserting) a new element


to the top of the stack is called PUSH operation.
Pushing an element to a stack will add the new element at
the top. After every push operation the top is incremented
by one.

If the array is full and no new element can be


accommodated, then the stack overflow condition occurs.
POP: The process of deleting (or removing) an element
from the top of stack is called POP operation.
After every pop operation the stack is decremented by one.

If there is no element in the stack and the pop operation is


performed then the stack underflow condition occurs.

Stack Implementation

Using
Address

Using
Pointers

Stack Implementation

Using
Address

Using
Pointers

Stack Implementation

#include <iostream.h>
#include <conio.h>

#define max_size 100


#define TRUE 1
#define FALSE 0
typedef int bolean;
/************************************/
struct stack
{
int items[max_size];
int top;
};

using address

Stack Implementation
/////////////////////////////funcions
// Initialize Function
void initialize(stack &s)
{
s.top=-1;
}

// Is_full Function
bolean is_full(stack &s)
{
if (s.top==max_size-1)
return TRUE;
return FALSE;
}

// Is_empty Function
bolean is_empty(stack &s)
{
if (s.top==-1)
return TRUE;
return FALSE;
}

using address

Stack Implementation

using address

// Push Function
void push(stack &s,int item)
{
if (is_full(s))
{
cout<<"Stack is overflow";
return;
}
// Pop Function
s.top++;
int pop(stack &s)
s.items[s.top]=item;
{
}
if (is_empty(s))
{
cout<<"Stack is underflow";
return -1;
}
int item=s.items[s.top];
s.top--;
return item;
}

Stack Implementation
// Print all elements Funcion
void print_all_elements(stack &s)
{
cout<<"the current items of the stack from top to bottom are:"<<endl;
for(int i=s.top;i>=0;i--)
cout<<s.items[i]<<endl;

// Stacktop Funcion
int stacktop(stack &s)
{
if (is_empty(s))
{
cout<<"Stack is underflow";
return -1;
}
return s.items[s.top];
}

using address

Stack Implementation
void main()
{
clrscr();
stack s;
initialize(s);
push(s,1);
push(s,2);
push(s,3);
push(s,4);
print_all_elements(s);
int val=pop(s);
print_all_elements(s);
//cout<<val;
getch();
}

using address

Stack Implementation

Using
Address

Using
Pointers

Stack Implementation

#include <iostream.h>
#include <conio.h>

#define max_size 100


#define TRUE 1
#define FALSE 0
typedef int bolean;
/************************************/
struct stack
{
int items[max_size];
int top;
};

using pointers

Stack Implementation
/////////////////////////////funcions
// Initialize Function
void initialize(stack *s)
{
s->top=-1;
}

// Is_empty Function
bolean is_empty(stack *s)
{
if (s->top==-1)
return TRUE;
return FALSE;
}

// Is_full Function
bolean is_full(stack *s)
{
if (s->top==max_size-1)
return TRUE;
return FALSE;
}

using pointers

Stack Implementation

using pointers

// Push Function
void push(stack *s,int item)
{
if (is_full(s))
{
cout<<"Stack is overflow";
return;
}
// Pop Function
s->top++;
int pop(stack *s)
s->items[s->top]=item;
{
}
if (is_empty(s))
{
cout<<"Stack is
underflow";
return -1;
}
int item=s->items[s->top];
s->top--;
return item;
}

Stack Implementation

using pointers

// Print all elements Funcion


void print_all_elements(stack *s)
{
cout<<"the current items of the stack from top to bottom are:"<<endl;
for(int i=s->top;i>=0;i--)
cout<<s->items[i]<<endl;

// Stacktop Funcion
int stacktop(stack *s)
{
if (is_empty(s))
{
cout<<"Stack is
underflow";
return -1;
}
return s->items[s->top];
}

Stack Implementation
void main()
{
clrscr();
stack s;
initialize(s);

stack *ps;
ps=&s;
push(ps,1);
push(ps,2);
push(ps,3);
push(ps,4);
print_all_elements(ps);
int val=pop(ps);
print_all_elements(ps);
//cout<<val;
getch();
}

using pointers

Thank you !

You might also like