Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 41

STACK Data Structures

S.Venkatesan
Classification of Data Structures
Simple Data Structure
Simple data structure can be constructed with
the help of primitive data structure.

A primitive data structure used to represent the
standard data types of any one of the computer
languages.

Variables, arrays, pointers, structures, unions,
etc. are examples of primitive data structures.

Compound Data structure
Compound data structure can be constructed with the
help of any one of the primitive data structure and it
is having a specific functionality.

It can be designed by user.

It can be classified as

1) Linear data structure

2) Non-linear data structure
Linear data structure
Collection of nodes which are logically adjacent in which
logical adjacency is maintained by pointers.

(or)

Linear data structures can be constructed as a continuous
arrangement of data elements in the memory.

It can be constructed by using array data type.

In the linear Data Structures the relation ship of adjacency
is maintained between the Data elements.
Operations applied on linear data
structure
The following list of operations applied on linear data structures

1. Add an element

2. Delete an element

3. Traverse

4. Sort the list of elements

5. Search for a data element

By applying one or more functionalities to create different types of
data structures

For example Stack, Queue, Tables, List, and Linked Lists.
Non-linear data structure
Non-linear data structure can be constructed as a
collection of randomly distributed set of data
item joined together by using a special pointer
(tag).

In non-linear Data structure the relationship of
adjacency is not maintained between the Data
items.


Operations applied on non-linear
data structures
The following list of operations applied on non-linear data structures.

1. Add elements

2. Delete elements

3. Display the elements

4. Sort the list of elements

5. Search for a data element

By applying one or more functionalities and different ways of joining
randomly distributed data items to create different types of data
structures.

For example Tree, Decision tree, Graph and Forest
What is a stack?
Stores a set of elements in a particular order
Stack principle: LAST IN FIRST OUT = LIFO
It means: the last element inserted is the first one to be
removed
Example




Which is the first element to pick up?


Stacks Operations
initializeStackInitializes the stack to an empty state.

isEmptyStackDetermines whether the stack is empty. If the stack is
empty, it returns the value true; otherwise, it returns the value false.

isFullStackDetermines whether the stack is full. If the stack is full, it
returns the value true; otherwise, it returns the value false.

pushAdds a new element to the top of the stack. The input to this
operation consists of the stack and the new element. Prior to this
operation, the stack must exist and must not be full.

topReturns the top element of the stack. Prior to this operation, the
stack must exist and must not be empty.

popRemoves the top element of the stack. Prior to this operation,
the stack must exist and must not be empty.
objects: a finite ordered list with zero or more elements.
methods:
for all stack Stack, item element, max_stack_size
positive integer
Stack createS(max_stack_size) ::=
create an empty stack whose maximum size is
max_stack_size
Boolean isFull(stack, max_stack_size) ::=
if (number of elements in stack == max_stack_size)
return TRUE
else return FALSE
Stack push(stack, item) ::=
if (IsFull(stack)) stack_full
else insert item into top of stack and return
Stack ADT
Boolean isEmpty(stack) ::=
if(stack == CreateS(max_stack_size))
return TRUE
else return FALSE
Element pop(stack) ::=
if(IsEmpty(stack)) return
else remove and return the item on the top
of the stack.
Stack ADT (contd)
Stack createS(max_stack_size) ::=
#define MAX_STACK_SIZE 100 /* maximum stack size */
typedef struct {
int key;
/* other fields */
} element;
element stack[MAX_STACK_SIZE];
int top = -1;

Boolean isEmpty(Stack) ::= top< 0;

Boolean isFull(Stack) ::= top >= MAX_STACK_SIZE-1;

Stack Implementation:
CreateS, isEmpty, isFull
void push(int *top, element item)
{
/* add an item to the global stack */
if (*top >= MAX_STACK_SIZE-1) {
stack_full( );
return;
}
stack[++*top] = item;
}

Push
element pop(int *top)
{
/* return the top element from the stack */
if (*top == -1)
return stack_empty( ); /* returns and error key
*/
return stack[(*top)--];
}

Pop
PUSH
void push(int i)
{
p1++;
if(p1 == (tos+SIZE)) {
printf("Stack Overflow.\n");
exit(1);
}
*p1 = i;
}

POP
int pop(void)
{
if(p1 == tos) {
printf("Stack Underflow.\n");
exit(1);
}
p1--;
return *(p1+1);
}

#define SIZE 50

void push(int i);
int pop(void);
int *tos, *p1, stack[SIZE];
int main(void)
{
int value;
tos = stack; /* tos points to the top of stack */
p1 = stack; /* initialize p1 */
do {
printf("Enter value: ");
scanf("%d", &value);
if(value != 0) push(value);
else printf("value on top is %d\n", pop());
} while(value != -1);
return 0;
}

Exercise: Converting
1. Convert these INFIX to PREFIX and POSTFIX :
a) A / B C / D
b) (A + B) ^ 3 C * D
c) A ^ (B + C)
2. Convert these PREFIX to INFIX and POSTFIX :
a) + / A B C ^ D E
b) + D E / X Y
c) ^ + 2 3 C D
3. Convert these POSTFIX to INFIX and PREFIX :
a) A B C +
b) G H + I J / *
c) A B ^ C D +
FPE Infix to Postfix
( ( A + B ) * ( C - E ) ) / ( F + G ) )

stack: (
output: []
FPE Infix to Postfix
( A + B ) * ( C - E ) ) / ( F + G ) )

stack: ( (
output: []
FPE Infix to Postfix
A + B ) * ( C - E ) ) / ( F + G ) )

stack: ( ( (
output: []
FPE Infix to Postfix
+ B ) * ( C - E ) ) / ( F + G ) )

stack: ( ( (
output: [A]
FPE Infix to Postfix
B ) * ( C - E ) ) / ( F + G ) )

stack: ( ( ( +
output: [A]
FPE Infix to Postfix
) * ( C - E ) ) / ( F + G ) )

stack: ( ( ( +
output: [A B]
FPE Infix to Postfix
* ( C - E ) ) / ( F + G ) )

stack: ( (
output: [A B + ]
FPE Infix to Postfix
( C - E ) ) / ( F + G ) )

stack: ( ( *
output: [A B + ]
FPE Infix to Postfix
C - E ) ) / ( F + G ) )

stack: ( ( * (
output: [A B + ]
FPE Infix to Postfix
- E ) ) / ( F + G ) )

stack: ( ( * (
output: [A B + C ]
FPE Infix to Postfix
E ) ) / ( F + G ) )

stack: ( ( * ( -
output: [A B + C ]
FPE Infix to Postfix
) ) / ( F + G ) )

stack: ( ( * ( -
output: [A B + C E ]
FPE Infix to Postfix
) / ( F + G ) )

stack: ( ( *
output: [A B + C E - ]
FPE Infix to Postfix
/ ( F + G ) )

stack: (
output: [A B + C E - * ]
FPE Infix to Postfix
( F + G ) )

stack: ( /
output: [A B + C E - * ]
FPE Infix to Postfix
F + G ) )

stack: ( / (
output: [A B + C E - * ]
FPE Infix to Postfix
+ G ) )

stack: ( / (
output: [A B + C E - * F ]
FPE Infix to Postfix
G ) )

stack: ( / ( +
output: [A B + C E - * F ]
FPE Infix to Postfix
) )

stack: ( / ( +
output: [A B + C E - * F G ]
FPE Infix to Postfix
)

stack: ( /
output: [A B + C E - * F G + ]
FPE Infix to Postfix


stack: <empty>
output: [A B + C E - * F G + / ]
Thank You

You might also like