DATA STRUCTURE STACK Stack A stack is a

  • Slides: 9
Download presentation
DATA STRUCTURE STACK

DATA STRUCTURE STACK

Stack �A stack is a list of element in which an element may be

Stack �A stack is a list of element in which an element may be inserted or deleted only at one end, called the top of the stack. This means in particular , that elements are removed from a stack in the reverse order of thet in which they were inserted into the stack. �Stack is also called last-in-first –out (LIFO)type of list.

Example of stack � Stack of book � Stack of pennies

Example of stack � Stack of book � Stack of pennies

Two basic operation �PUSH is the term used to insert an element into a

Two basic operation �PUSH is the term used to insert an element into a stack �POP is the term used to delete an element from a stack

Diagram of push operation top top A B C D E A B C

Diagram of push operation top top A B C D E A B C D Push (E) F E A Btop C D Push( F)

Diagram of POP operation A B C D top A B C POP (D)

Diagram of POP operation A B C D top A B C POP (D) top A B POP( C)

Implementation of stack �Static implementation of stack �Dynamic implementation of stack

Implementation of stack �Static implementation of stack �Dynamic implementation of stack

Static implementation of stack �Static implementation of stack uses of array to create stack.

Static implementation of stack �Static implementation of stack uses of array to create stack. �Therefore a stack can be declared as a structure containing two fields. Ex: #define MAXSIZE 100 Strcut stack{ int top; int element[MAXSIZE] }; Struct stack s;

Dynamic implementation of stack �Dynamic implementation is also called linked list representation and uses

Dynamic implementation of stack �Dynamic implementation is also called linked list representation and uses pointer to implement the stack type of data structure �Ex: Strcut node{ int info; int node *next; }; Struct node *s;