+ All Categories
Home > Documents > STACK – Data Structures

STACK – Data Structures

Date post: 15-Oct-2015
Category:
Upload: rajan-jaiprakash
View: 26 times
Download: 1 times
Share this document with a friend
Description:
Data structures and algorithm Lecture series
Popular Tags:

of 41

Transcript

STACK Data Structures

STACK Data StructuresS.VenkatesanClassification of Data Structures

Simple Data StructureSimple 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 structureCompound 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 structureLinear 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 structureThe 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 structureNon-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 structuresThe 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 ForestWhat is a stack?Stores a set of elements in a particular orderStack principle: LAST IN FIRST OUT = LIFOIt means: the last element inserted is the first one to be removedExample

Which is the first element to pick up?

Stacks OperationsinitializeStackInitializes 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 ADTBoolean isEmpty(stack) ::= if(stack == CreateS(max_stack_size)) return TRUE else return FALSEElement 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, isFullvoid push(int *top, element item){ /* add an item to the global stack */ if (*top >= MAX_STACK_SIZE-1) { stack_full( ); return; } stack[++*top] = item;}

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

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

POPint 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: ConvertingConvert these INFIX to PREFIX and POSTFIX :A / B C / D(A + B) ^ 3 C * DA ^ (B + C)Convert these PREFIX to INFIX and POSTFIX :+ / A B C ^ D E + D E / X Y^ + 2 3 C DConvert these POSTFIX to INFIX and PREFIX :A B C + G H + I J / *A B ^ C D + FPE Infix to Postfix( ( A + B ) * ( C - E ) ) / ( F + G ) )

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

stack: ( /output: [A B + C E - * F G + ]39FPE Infix to Postfixstack: output: [A B + C E - * F G + / ]40Thank You


Recommended