1 / 5

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]. If we have to print more than one element , than we have to use Loop. A[2]. A[1]. If stack is empty , then no element is to be printed out. A[0].

hans
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] If we have to print more than one element , than we have to use Loop A[2] A[1] If stack is empty , then no element is to be printed out A[0] if top = -1 then stack is empty top= -1

  2. STACK (Linear Stack) Traverse Suppose 3 elements are available A[4] So , value of top is 2 A[3] Coding of Traversing 30 top= 2 A[2] cout<<A[TOP]; 20 A[1] top= 1 TOP--; 10 top= 0 A[0] cout<<A[TOP]; top= -1 TOP--; Now, we cannot print an element Stack is empty cout<<A[TOP]; TOP--; Value of top =-1

  3. 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]; Top---; This process continues until top becomes -1

  4. Coding of Traverse() void Traverse() { if(top==-1) cout<<“Underflow or empty”; else { cout<<“Elements of stack are: “<<endl; while(top>=0) { cout<<a[top]; top---; } } }

  5. Wow !!!! Traverse • Easy…very Easy Foolish thing , we did. Question was : To traverse an array. But Not Delete Top -- means “Deleting an element”

More Related