Vous êtes sur la page 1sur 31

# SI VAGAMI ANNAMALAI

DATA
STRUCTURES
Stack
N
Stack
2
Stack
1
Single
Array
Q U E S T I O N
Stack structure is used for parameter
tracking and passing with procedure
calls, compilers, and recursion. Design a
program that implements Multiple Stack
Using Array .

C O N C E P T S U S E D
In this program I used the concepts
STACK BASIC PRINCIPLE
NORMAL STACK vs MULTIPLE STACK
STACK:
Linear Data Structure
LIFO Last In First Out
Basic Operations :
Push Process Of Inserting An Element
At The Topmost Position
Pop Process Of Deleting An Element At
The Topmost Position

CONCEPT USED IN THE PROGRAM
PART-
1
BASIC PRINCIPLES OF STACK
CONCEPT OF THE PROGRAM
PART-
2
SINGLE STACK
NORMAL STACK
ATTRIBUTES:
Top of the Stack
Overflow Limit
Underflow Limit

Top of the
Stack
Underflow
Limit
Overflow
Limit
Single
Stack in an
array
CONCEPT OF THE PROGRAM
PART-
3
MULTIPLE STACK
MULTIPLE STACK
USING ARRAY:
Process of
implementing
more than one
stack with in a
single array

Stack
N
Stack
2
Stack
1
CONCEPT OF THE PROGRAM
PART-
4
IMPLEMENT MULTIPLE STACK
ATTRIBUTES NEEDED TO IMPLEMENT MUTIPLE
STACK:
Lets Consider
Number of Stacks N
Then, We Need
N number of Tops
N number of overflow Limit
N number of underflow Limit

CONCEPT OF THE PROGRAM
PART-
5
IMPLEMENT MULTIPLE STACK
Stack
N
Stack
2
Overflow Limit of N
Underflow Limit of N
Overflow Limit of 2
Stack
1
Underflow Limit of 1
Overflow Limit of 1
& underflow limit of
2
OPTIMIZATION
PART-
1
Values To Be Read From The User To Split The Array:

* Array Size (array_size)
* Number of stacks to be implemented in an
array
(no_of_stack)
OPTIMIZATION
PART-
2
Cases Of Invalid Inputs

1) array_size<=0 || no_of_stack<=0
2) array_size<no_of_stack

to View
These
Cases
Stack Fragmentation Warning
Condition array_size%no_of_stack!=0

EXCEPTIONS
OPTIMIZATION
PART-
3
array=(int*)malloc(array_size * sizeof(int));
top=(int*)malloc(no_of_stack*sizeof(int));
overflowlmt=(int*)malloc(no_of_stack*sizeof(int
));
underflowlmt=(int*)malloc(no_of_stack*sizeof(i
nt));
DYNAMIC MEMORY ALLOCATION
OPTIMIZATION
PART-
4
for(lpVar=0;lpVar<no_of_stack;lpVar++)
{

overflowlmt[lpVar]=(array_size/no_of_stack)*(lpVar+1)-1;

underflowlmt[lpVar]=(array_size/no_of_stack)*lpVar-1;
top[lpVar]=(array_size/no_of_stack)*lpVar-1;
}

SPLITTING OF AN ARRAY INTO MULTIPLE STACK
to see how
it works
OPTIMIZATION
PART-
5
Void push() To Insert an element on to the particular Stack
Void pop() To Delete the top element from the Particular Stack
Void display() To Display the elements of the Particular Stack
IMPLEMENTATION
LIST OF FUNCTIONS SUPPLIED
Program Code (main routine)
#include<stdio.h>
#include<conio.h>

int *array; // variable declaration
int *top, array_size, no_of_stack, lpVar, *overflowlmt, *underflowlmt, stack_choice;

void push(); //list of functions used
void pop();
void display();

Program Code
int main()
{
int choice,decider;
main_label:
system("cls");
printf("\nEnter total memory size:(greater than 0) : ");
scanf("%d",&array_size);
printf("\nEnter number of stacks:(greater than 0) : ");
scanf("%d",&no_of_stack);

Program Code
if(array_size<=0 || no_of_stack<=0)
{
printf("\nInvalid input.. Try again.." );
getch();
goto main_label;
}

if(array_size<no_of_stack)
{
printf("\nInvalid input..\nNumber of Stacks is greater than Array size..\nTry again.." );
getch();
goto main_label;
}
if(array_size%no_of_stack!=0)
{
printf("\nWarning!! There is stack fragmentation.. \nMemory space that will not be utilized : %d",array_size%no_of_stack);
printf("\nPress any key to continue..");
getch();
}

Program Code
array=(int*)malloc(array_size * sizeof(int));
top=(int*)malloc(no_of_stack*sizeof(int));
overflowlmt=(int*)malloc(no_of_stack*sizeof(int));
underflowlmt=(int*)malloc(no_of_stack*sizeof(int));
for(lpVar=0;lpVar<no_of_stack;lpVar++)
{
overflowlmt[lpVar]=(array_size/no_of_stack)*(lpVar+1)-1;
underflowlmt[lpVar]=(array_size/no_of_stack)*lpVar-1;
top[lpVar]=(array_size/no_of_stack)*lpVar-1;
}
getch();

Program Code
label:
system("cls");
fflush(stdin);
puts("\nWhich stack do you want to access? ");
for(lpVar=1;lpVar<= no_of_stack;lpVar++)
printf("%d. Stack %c ",lpVar,64+lpVar);
printf("\nPress 0 to exit ");
scanf("%d",&stack_choice);
if(stack_choice==0)
exit(0);
else if(!(stack_choice <= no_of_stack && stack_choice>0))
{
printf("\nInvalid Stack!! Press any key to continue..");
getch();
goto label;
}
Program Code
puts("\n1. Push \n2. Pop\n3. Display\n4. Exit");
fflush(stdin);
scanf("%d",&choice);
while(1)
{
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;

Program Code
case 4:
exit(0);
break;
default:
puts("\nYou have Entered the
wrong choice...\nTry again.. :)");
getch();
goto label;
}
printf("\nPress any key to terminate\nWant to continue press 1..");
scanf("%d",&decider);
if(decider==1)
goto label;
else
exit(0);
}
}

Push Routine
void push()
{
if(top[stack_choice-1]>=overflowlmt[stack_choice-1])
printf("\nSorry.. The Element can't be inserted.. \nSTACK
OVERFLOW.. ");
else
{
printf("%d",top[stack_choice-1]++);
printf("\nEnter the element to be inserted: ");
scanf("%d",&array[top[stack_choice-1]]);
}

}

If Part(executed when
overflow)
Top[1-1]>=overflowlmt[1-1]
Top>=overflowlmt
-1>=1
false

Else Part
Top[1-1]++
top++
(-1)++ 0

Pop Routine
void pop()
{
int temp;
if(top[stack_choice-1]==underflowlmt[stack_choice-1])
printf("\n\nSorry.. The Element can't be deleted..\nSTACK
UNDERFLOW.. ");
else
{
temp=array[top[stack_choice-1]];
printf("\nThe Popped element is %d ",temp);
top[stack_choice-1]--;
}
}

If Part(initially executed)
Top[1-1]==underflowlmt[1-
1]
Top>=underflowlmt
-1==-1
true

Else Part(after inserting
element)
Temp = array[top] =
array
print temp
top--

Display Routine
void display()
{
int j;
if(top[stack_choice-1]==underflowlmt[stack_choice-1])
printf("\nThe stack is empty");
else
{
printf("\nThe Elements of stack are:\n");
for(j=underflowlmt[stack_choice-1]+1;j<=top[stack_choice-1];j++)
printf("%d ",array[j]);
}
}

If Part
Top[1-1]==overflowlmt[1-1]
Top==overflowlmt
No elements in stack

Else Part
Eg: Stack 1
Underflowlmt+1 to top

{
printf("\n\n\t\t\tMULTIPLE STACK IMPLEMENTATION\n");
printf("\t\t\t=============================\n");
}
Invalid Inputs
Invalid Inputs
Stack Fragmentation Warning
Go
Back
Overflowlmt Determination
array_size 6
no_of_stacks 3
So, top [0:1]
overflowlmt[0:1]
Underflowlmt[0:1]
Formula:
overflowlmt[lpVar]=(array_size/no_of_stack)*(lpVar+1)-1;

First run: lpVar =0
Overflowlmt=(6/3)*(0+1)-1=2(1)-1=1

Second run: lpVar=1
Overflowlmt=(6/3)*(1+1)-1=2(2)-1=3

Third Run:
Overflowlmt=(6/3)*(2+1)-1=2(3)-1=5

Underflowlmt and Top Determination
array_size 6
no_of_stacks 3
So, top [0:1]
overflowlmt[0:1]
Underflowlmt[0:1]
Formulae: underflowlmt[lpVar]=(array_size/no_of_stack)*lpVar-
1;
top[lpVar]=(array_size/no_of_stack)*lpVar-1;

First run: lpVar =0
Underflowlmt=top=(6/3)*0-1=0-1=-1

Second run: lpVar=1
Underflowlmt=top=(6/3)*1-1=2-1=1

Third Run:
Underflowlmt=top=(6/3)*2-1=4-1=3

Screen Shot
Array Index Top Overflowlmt Underflowlmt
5 Overflowlmt
4
3 Top Overflowlmt Underflowlmt[2
]
2
1 Top Overflowlmt Underflowlmt[1
]
0
-1 Top Underflowlmt[0
]
Positio
n
Fixed
Positio
n
Fixed
Position
Changes as the element
is pushed and popped
Go
Back
Stac
k C
Stac
k B
Stac
k A