1 / 6

STACK (Linear Stack) Traverse i.e. Printing all the elements of an Stack

STACK (Linear Stack) Traverse i.e. Printing all the elements of an Stack. A[4]. Traverse means printing all elements of stack. A[3]. To Traverse , we have to take temporary Stack. A[2]. A[1]. Firstly , we will print the element. A[0]. top= -1. Then Take out the element.

santos
Download Presentation

STACK (Linear Stack) Traverse i.e. Printing all the elements of an Stack

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. STACK (Linear Stack) Traverse i.e. Printing all the elements of an Stack A[4] Traverse means printing all elements of stack A[3] To Traverse , we have to take temporary Stack A[2] A[1] Firstly , we will print the element A[0] top= -1 Then Take out the element Now we’ve two Stacks. And Insert into another Stack. Pop the element from Temporary Stack and push into Original Stack This process Continues till “Stack” becomes empty

  2. STACK (Linear Stack) Traverse Take Temporary Stack Suppose 3 elements are available. A[4] B[4] A[3] B[3] A[2] 30 top= 2 10 B[2] So , value of top is 2. top1= 2 20 A[1] 20 top= 1 B[1] top1= 1 10 A[0] 30 top= 0 B[0] top1= 0 top= -1 Now Original “Stack “ is Empty. Bcoz top=-1 top1= -1 Now , Reverse the process. Temporary “Stack” contains all the elements Pop the elements from Temporary Stack and push into Original Stack

  3. STACK (Linear Stack) Traverse Temporary Stack Original Stack B[4] A[4] B[3] A[3] B[2] 30 top1= 2 10 A[2] top= 2 20 B[1] 20 top1= 1 A[1] top= 1 10 B[0] 30 top1= 0 A[0] top= 0 top1= -1 Temporary “Stack “ is Empty. Bcoz top=-1 top= -1 Hence , we get original stack without violating the principle of Stack i.e. LIFO Original “Stack” contains all the elements

  4. Coding of Traversing an element in stack Traverse Operation is called as :Traverse() Important Point: Before traversing stack , we have to check whether the stack is empty or not Stack is empty: if top = -1 If Stack is not empty, cout<<A[top]; • top1++;; • B[top1]=A[top]; top---; This process continues until top becomes -1. Back from Temporary to Original Stack. Top++; A[top]=B[top1]; Top1-- ; This process Continues until top1 become -1.

  5. Coding of Traverse() void Traverse() { if(top==-1) cout<<“Underflow or empty”; else { int B[20], top1=-1; cout<<“Elements of stack are: “<<endl; while(top>=0) { cout<<A[top]; top1++; B[top1]=A[top]; top---; } while(top1>=0) { top++; B[top1]=A[top]; top1---; } } }

  6. In most Schools , Colleges • Important Information: Concept of Traversal is explained incorrectly. Without Temporary Stack In some books , it’s also written incorrectly. But If you refer Foreign authors ,This concept is explained with Temporary Stack. I hope , you guys will not do this mistake.

More Related