Definition: A stack is an ordered collection of elements in which insertions(Push) and...

19

description

A stack is referenced via a pointer to the top element of the stack referred as top pointer. The top pointer keeps track of the top element in the stack. Initially, when the stack is empty, the top pointer has a value zero and when the stack contains a singly element, the top pointer has a value one and so on.

Transcript of Definition: A stack is an ordered collection of elements in which insertions(Push) and...

Page 1: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)
Page 2: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Definition:A stack is an ordered collection of elements in

which insertions(Push) and deletions(Pop) are restricted to one end.

LIFO(Last In First Out) or LCFS(Last Come First Served)

Page 3: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

A stack is referenced via a pointer to the top element of the stack referred as top pointer.

The top pointer keeps track of the top element in the stack.

Initially, when the stack is empty, the top pointer has a value zero and when the stack contains a singly element, the top pointer has a value one and so on.

Page 4: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Implementation of StackArray implementation of stack:• Stacks can be represented using arrays when the size is nominal and elements are predefined.

0 1 2 3 4 Top Max stack

• A Pointer called Top which contains the location of top element . • The variable Max stack gives the maximum number of elements held by the stack.

12 34 56

Page 5: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Array Implementation of PUSH Operation

• Process of adding a new element.• Each time top pointer is incremented.

Top=-1 Top=0 Top= 1 Top=2 Stack is empty Insert element 20 Insert element 34 Insert element 50

20

503420

3420top top

toptop

Page 6: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Algorithm StackPUSH(stack , maxsize) Step 1:[check for stack overflow] If Top>maxsize-1 Then print ”stack is full: and else Step 2:[Increment Top] set Top=Top+1 Step 3:[Insert new element in Top position] set stack[Top]=element END StackPUSH

Page 7: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

PseudocodeVoid push(){int element;if(Top==maxsize-1){printf(“the stack is full”);getch();exit(0);}else{printf(“Enter the element to be inserted”);scanf(“%d”,&element);Top=Top+1;stack[Top]=element;}}

Page 8: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Array Implementation of POP Operation

Process of deleting an existing element from the top of the stack.

Each time top pointer is decremented.

Top=2 Top=1 Top=0 Top=-1 Initial stack After deletion of 50 After deletion of 34 After deletion of 20 Stack empty

503420

3420 20

toptop

top top

Page 9: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Algorithm StackPop(stack)Step 1:[check for stack underflow] If Top<0 Then print “stack is empty” and exit else remove the item from topStep 2:[decrement Top] set Top=Top-1Step 3:[delete an element in Top position] set item=stack[Top]END StackPop

Page 10: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Psuedocodeint Pop(){int lement;if(Top==-1){printf(“The stack is empty”);getch();exit(0);}else{element=stack[Top];Top=Top-1;}return(element);}

Page 11: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Linked List Implementation of stackStacks can be represented using linked list to

overcome the drawbacks of array that are memory wastage and shortage.

Top

• Linked List implementation of stack provides us convenient and flexibility to increase or decrease stack size as necessary during execution.

• Only Drawback is extra memory to store the address of links.

A * B

* C NULL

Page 12: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Linked List implementation of Push Operation

While inserting a new element into the linked stack, first we need to create a new stack node and then store

new element into the data field of the new node.

Top

Top

1000

2050 2050 Stack empty Push element 20 Push element 13

NULL

20 NULL

2050

20 1000

13 NULL

1000

Page 13: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Algorithm linkedstackpush(element)Step 1:[allocate memory for new stack node] node=(stack*)malloc(sizeof(stack) if(Top==NULL) stack empty and insert newnode as only

node Top==newnode else insert node at top of stackStep 2:[set node data] newnode->element=element

Page 14: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Step 3:[insert node into as top of stack] current Top=Top current Top->Link=newnode Top=newnodeEND linkedstackpush;

Page 15: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Pseudocodevoid pushstacklinked(){stack *node;int data;node=(stack*)malloc(sizeof(stack))printf(“enter the elements”);Scanf(“%d”,&data);Newnode->element=data;currentTop=Top;currentTop->Link=newnode;Top=newnode;}

Page 16: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Linked list implementation of Pop Operation

For deleting an element from linked stack, first we need to make sure that the stack is not empty.

For this first Verify the Top pointer. Top

Top

1000

2050 2050

Pop element 13 Pop element 20 stack empty

1000

13 NULL

20 1000 20

NULL

2050

NULL

Page 17: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Algorithm linkedstackpop(element)Step 1:[check whether stack is empty] if(Top==NULL) Then print ”stack is empty” stop elseStep 2:[retrieve node value] data=node->elementStep 3:[delete node] Top=node->link deallocate memoryEND stackpush;

Page 18: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Pseudocodevoid popstacklinked(){stack *node;int data;if(top==NULL){printf(“stack is empty and deletion cannot be done”);getch();exit();}Else{node=Top;data=node->element;Top=node->link;free(node);}retrun(data);}

Page 19: Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Thank u