0% found this document useful (0 votes)
15 views13 pages

Stacks 1

The document provides an overview of stacks, defining them as ordered groups of homogeneous items that follow the Last In, First Out (LIFO) principle. It outlines basic stack operations such as initialization, push, pop, and checking if the stack is empty or full, along with an array implementation of stacks. The document includes code snippets for the push and pop operations, demonstrating how to manage stack elements using an array structure.

Uploaded by

Dr. VASAVI BANDE
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)
15 views13 pages

Stacks 1

The document provides an overview of stacks, defining them as ordered groups of homogeneous items that follow the Last In, First Out (LIFO) principle. It outlines basic stack operations such as initialization, push, pop, and checking if the stack is empty or full, along with an array implementation of stacks. The document includes code snippets for the push and pop operations, demonstrating how to manage stack elements using an array structure.

Uploaded by

Dr. VASAVI BANDE
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/ 13

Stacks

Data Structures

EASTERN MEDITERRANEAN UNIVERSITY


Stacks
 Outline
 Stacks
 Definition
 Basic Stack Operations
 Array Implementation of Stacks
What is a stack?

 It is an ordered group of homogeneous items of elements.


 Elements are added to and removed from the top of the stack
(the most recently added items are at the top of the stack).
 The last element to be added is the first to be removed (LIFO:
Last In, First Out).
BASIC STACK OPERATIONS
 Initialize the Stack.
 Pop an item off the top of the stack (delete an
item)
 Push an item onto the top of the stack (insert
an item)
 Is the Stack empty?
 Is the Stack full?
 Clear the Stack
 Top element
 Determine Stack Size
Array Implementation of the Stacks
 The stacks can be implemented by the use of
arrays and linked lists.
 One way to implement the stack is to have a
data structure where a variable called top
keeps the location of the elements in the stack
(array)
 An array is used to store the elements in the
stack
Stack Definition
struct STACK{
int count; /* keeps the number of elements in
the stack */
int top; /* indicates the location of the top of
the stack*/
int a[STACKSIZE]; /*array to store the
stack elements*/
}s;
Stacks
Stack Initialisation
 initialize the stack by assigning -1 to the top
pointer to indicate that the array based stack
is empty (initialized) as follows:
 You can write following lines in the main
program:
:
STACK s;
s.top = -1;
:
Push Operation
Push an item onto the top of the stack (insert an item)
void push (int newitem)
 Function: Adds newitem to the top of the
stack.
 Preconditions: Stack has been
initialized and is not full.
 Postconditions: newItem is at the top of
the stack.
void push (int newitem)
void push(int newitem) /*pushes ps into stack*/
{
if(s.top == STACKSIZE-1){
printf("Stack is full\n");
return; /*return back to main function*/
}
else {
s.top++;
s.a[top]= newitem;
s.count++;
}
}
Pop operation
 Pop an item off the top of the stack (delete
an item)
int pop ()
 Function: Removes topItem from stack and
returns with topItem
 Preconditions: Stack has been initialized and
is not empty.
 Postconditions: Top element has been
removed from stack and the function returns
with the top element.

You might also like