Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
1K views10 pages

Multiple Stacks and Queues

This document discusses using a one-dimensional array to implement multiple stacks. It explains how boundary and top indexes are used to track the boundaries and tops of each stack within the single array. PUSH and POP operations are presented to add/remove elements from a specified stack. The document also describes a recovery process for making space for a new stack when the array is full by shifting existing stacks to create space between stacks.

Uploaded by

Sanskar Seth
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)
1K views10 pages

Multiple Stacks and Queues

This document discusses using a one-dimensional array to implement multiple stacks. It explains how boundary and top indexes are used to track the boundaries and tops of each stack within the single array. PUSH and POP operations are presented to add/remove elements from a specified stack. The document also describes a recovery process for making space for a new stack when the array is full by shifting existing stacks to create space between stacks.

Uploaded by

Sanskar Seth
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/ 10

BBM 201

DATA STRUCTURES
Lecture 7:
Multiple Stacks and Queues

2015-2016 Fall
Multiple Stacks and Queues
Multiple Stacks and Queues
•  A one dimensional array can be used for building n
stacks.

…….. .……..

boundary[0] boundary[1] boundary[2] boundary[n]


top[0] top[1] top[2]
Multiple Stacks and Queues

#define STACK_SIZE 100


#define MAX_STACK 10 //stack number+1

element stack[STACK_SIZE];
int top[MAX_STACK];
int boundary[MAX_STACK];
int n; //n<MAX_STACK (number of stacks)
Multiple Stacks and Queues

top[0]=boundary[0]=-1;
for(i=1; i<n; i++)
top[i]=boundary[i]=(STACK_SIZE/n)*i;
boundary[n]=STACK_SIZE-1;
Multiple Stacks and Queues

0 11 22 33
…….. .……..

boundary[0] boundary[1] boundary[2] boundary[n]


top[0] top[1] top[2]
if top[i]==boundary[i+1], then the
top -1 11 22 33 44 stack i is completely full.

if top[i]==boundary[i], then the


boundary -1 11 22 33 44 stack i is empty.
PUSH

void push(int i, element item)


{
if(top[i]==boundary[i+1]) //is there any space in the ith stack?
stack_full();
stack[++top[i]]=item;
}
POP

element pop(int i)
{
if(top[i]==boundary[i] //is the stack empty?
return stack_empty();
return stack[top[i]--];
}
Is the stack completely full?
--Recovery
In order to create some space for the a new stack:

1.  stack_no < j < n, such that there is free space between stacks j and j+1

move stacks stack_no+1, stack_no+2, ... j one position right.


this creates a space between stack_no and stack_no+1.

2.  0 <= j < stack_no, such that there is free space between stacks j and j
+1
move stacks j+1, j+2, ... stack_no one position left.
this also creates a space between stack_no and stack_no+1.

3.  No j satisfying either condition 1 or condition 2, then there is no free


space.
References
•  Data Structures Notes, Mustafa Ege.
•  Fundamentals of Data Structures in C. Ellis Horowitz,
Sartaj Sahni, and Susan Anderson-Freed, 1993.

You might also like