Download presentation
Presentation is loading. Please wait.
Published byJoseph Short Modified over 9 years ago
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) or LCFS(Last Come First Served)
3
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.
4
Implementation of Stack Array 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. 123456
5
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 50 34 20 34 20 top
6
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
7
Pseudocode Void 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; }
8
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 50 34 20 34 20 top
9
Algorithm StackPop(stack) Step 1:[check for stack underflow] If Top<0 Then print “stack is empty” and exit else remove the item from top Step 2:[decrement Top] set Top=Top-1 Step 3:[delete an element in Top position] set item=stack[Top] END StackPop
10
Psuedocode int Pop() { int lement; if(Top==-1) { printf(“The stack is empty”); getch(); exit(0); } else { element=stack[Top]; Top=Top-1; } return(element); }
11
Linked List Implementation of stack Stacks 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 * CNULL
12
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 1000 2050 2050 Stack empty Push element 20 Push element 13 NULL 20NULL 2050 201000 13NULL 1000
13
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 stack Step 2:[set node data] newnode->element=element
14
Step 3:[insert node into as top of stack] current Top=Top current Top->Link=newnode Top=newnode END linkedstackpush;
15
Pseudocode void 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; }
16
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 1000 2050 2050 Pop element 13 Pop element 20 stack empty 1000 13NULL 20100020NULL 2050 NULL
17
Algorithm linkedstackpop(element) Step 1:[check whether stack is empty] if(Top==NULL) Then print ”stack is empty” stop else Step 2:[retrieve node value] data=node->element Step 3:[delete node] Top=node->link deallocate memory END stackpush;
18
Pseudocode void 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); }
19
Thank u
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.