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

Stack 1

Uploaded by

Science Spirit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Stack 1

Uploaded by

Science Spirit
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DEFINITION OF STACK

A stack is a linear data structure in which insertions and deletions are allowed only
at the end, called the top of the stack.

Both these examples have two things in common:


1. Any object (either book or a coin) can be accessed only from the top.
2. Any object can be added only at the TOP.

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.

PRIMARY STACK OPERATIONS


1. push (data): Inserts data onto stack.
2. pop(): Deletes the last inserted element from the stack.

SECONDARY STACK OPERATIONS


1. top (): returns the last inserted element without removing it.
2. size(): returns the size or the number of elements in the stack.
3. isEmpty(): returns TRUE if the stack is empty, else returns FALSE.
4. isFull(): returns TRUE if the stack is full, else returns FALSE.

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.

As a stack is a linear data structure, we can implement it using an array or a linked


list.

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.

For the push operation:


top is incremented by 1.
New element is pushed at the position top.

SERIES OF PUSH AND POP OPERATIONS

push (2) Not possible. There is not


push (3)
push (5) enough space in the stack. This
push (10) state is called the over ow state
push (11) and the new
push (12)
element can't be pushed
fl
For the pop operation:
The element at the position of top is
deleted.
top is decremented by 1.

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 () {
.. .
}

Similarly, top variable is used multiple times in di erent functions.


Instead of passing it to every function, the better option is to declare the top variable
globally.
#include <stdio.h>
#de ne MAX 4
int stack_arr [MAX]; top = -1 indicates that
int top = -1; the stack is empty.
int main() {
...
}
fi
fi
fi
ff
ff
Recall: Pushing an element involves two steps:
1. Increment the variable top by 1.
2. Store the value at the index top.

#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.
}

It is important to check whether


the stack is full in the push function.

#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);
}

PROGRAM TO POP AN ELEMENT ONTO STACK

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

You might also like