Stack 1
Stack 1
A stack is a linear data structure in which insertions and deletions are allowed only
at the end, called the top of the stack.
STACK AS AN ADT
When we de ne a stack as an ADT (or Abstract Data Type), then we are only
interested in knowing the stack operations from the user point of view.
Means we are not interested in knowing the implementation details at this moment.
We are only interested in knowing what type of operations we can perform on stack.
LIST OF COMMANDS
push (1)
push (2)
push (3)
pop ( )
isFull()
fi
push operation in C & pop operation in C
Only basics.
A stack is a linear data structure in which insertions and deletions are allowed only
at the end, called the top of the stack.
We know that stack-arr[] is an array and insertion and deletion operations can be
performed at any place but we want the stack-arr [] to behave like a stack.
Hence, the insertions and deletions must be performed at the end.
For this, we will keep a variable top which will keep track of the last inserted
element or the topmost element in an array.
To indicate that the stack is empty, we will put -1 in the variable top.
Note: top = 0 indicates that the topmost element is at index 0 and this
means there is only one element in the stack.
In future, we will have functions for di erent operations including push and pop.
Almost every function requires the stack_arr[] array.
Instead of passing it to very function, the better option is to declare the stack-arr globally.
#include ‹stdio.h>
Global declaration of
int stack_arr[4];
stack_arr[];
int main() {
…… We can also use #de ne
} preprocessor directive to
declare this
constant;
#include ‹stdio.h›
#de ne MAX 4
int stack_arr[MAX];
int main () {
.. .
}
#include ‹stdio.h>
#de ne MAX 4
int stack_arr[MAX];
int top = -1;
int main() {
push (1) ;
push (2);
push (3);
push (4);
push (5);
}
void push(int data)
{
top = top + 1;
stack_arr[top] = data; Wait. The stack is full.
}
#include ‹stdio.h›
#de ne MAX 4
void push(int data)
{
int stack_arr[MAX];
if(top == MAX - 1) {
int top = -1;
printf("Stack over ow");
int main() {
return;
push (1);
}
push (2);
top = top + 1;
push (3);
stack_arr[top] = data;
push (4);
push (5);
}
NEXT PAGE
fi
fi
fl
Q. How to delete the element at index 3?
We cannot simply remove the array element. Still, we can give the user an illusion that the
array element is deleted by decrementing the top
variable.
top variable always keeps track of the topmost element of the stack. If the top is
decremented by l then the user will perceive that the
topmost element is deleted.
Now, we will write a pop function which is capable of deleting the topmost element of the
stack (stack-arr[ ] and returns the deleted
element.
#include <stdio.h>
#de ne MAX 4
int stack_arr[MAX];
int top = -1;
int main() {
push (1);
push (2);
push (3); since pop has also to
push (4); return a value about which
push (5); data get popped so
pop () ; it must return a integer value and
} for that returned value, we will
store the value it in another variable
#include <stdio.h>
#de ne MAX 4
int stack_arr[MAX];
int top = -1; int pop()
int main() { {
int data; int value;
push (1); value = stack_arr[top];
push (2); top = top - 1;
push (3); return value;
push (4); }
push (5);
data = pop () ;
}
fi
fi
CHECK THE STACK UNDERFLOW CONDITION
#include <stdio.h>
#de ne MAX 4
int pop ( )
int stack_arr[MAX]; {
int top = -1; int value;
int main() { if(top == - 1) {
int data; printf("Stack under ow");
push (1); exit (1); }
push (2); value = stack_arr[top];
push (3); top = top - 1;
push (4); return value;
push (5); }
data = pop () ;
}
We don't want to return an integer.
exit (1) means abnormal termination of the
program.
fi
fl