40 likes | 207 Views
STACK (Linear Stack ) Deletion. A[4]. Delete an element. A[3]. It means , print the element Available at the top. A[2]. Then decrease the value of top by 1. A[1]. A[0]. Stack is empty so we cannot delete an element. top= -1. STACK (Linear Stack ) Deletion.
E N D
STACK (Linear Stack) Deletion A[4] Deletean element A[3] It means , print the element Available at the top A[2] Then decrease the value of top by 1 A[1] A[0] Stack is empty so we cannot delete an element top= -1
STACK (Linear Stack) Deletion Suppose 3 elements are available A[4] So , value of top is 2 A[3] Coding of deletion: 30 top= 2 A[2] cout<<A[TOP]; 20 A[1] top= 1 TOP--; 10 top= 0 A[0] Delete one more element top= -1 cout<<A[TOP]; TOP--; Delete one more element Now, we cannot delete an element Stack is empty Delete one more element cout<<A[TOP]; TOP--; Value of top =-1
Coding of Deleting an element in stack Delete Operation is called as :Pop() Important Point: Before Deleting an element , we have to check whether the stack is emptyor not Stack is empty: if top = -1 If Stack is not empty, Cout<<A[top]; Top---;
Coding of push() void pop() { if(top==-1) cout<<“Underflow or empty”; else { cout<<“Deleted element is:”<<a[top]; top---; } }