day19 - COP 3503 Computer Science II Spring 2000 CLASS...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 3503 – Computer Science II – Spring 2000 - CLASS NOTES - DAY #19 Stacks and Queues Recall from our earlier discussions of stacks and queues that the operations defined on these data structures are expected to take O(1) time (constant time). For both of these data structures there are two basic methods for implementation that will yield constant time operations. The first technique is to store the data items contiguously in an array. The second technique is to store the items non- contiguously in a linked list. Array Implementations Stacks Focusing on stacks exclusively for the moment, we have previously seen that the static implementation of a stack is sufficient, if the size of the stack will never exceed the predetermined maximum size of the array which is used to implement the stack. If this is the case, the push( ) and pop( ) operations will require O(1) time. If the stack should ever reach the capacity of the array, then the next push( ) operation will require a time which is not constant since the array will now need to be expanded, which is not a constant time operation. The time required to expand the underlying array can be amortized over each push( ) operation which will have the effect of ensuring each operation can be performed in O(1) time but with a slight increase in the actual time for each operation. Recall that an array-based implementation of a stack requires an array stack and a single integer variable. The integer tos ( top_of_stack ) is the index of the topmost element in the stack at any point in time. When tos == - 1, the stack is empty. To push a new element new onto the stack the value of tos is incremented and the new stack element is placed onto the top of the stack: stack [++ tos ] = new . The pop operation is performed by retrieving the top element of the stack and decrementing the value of tos : element = stack [ tos - ]. Day 19 - 1 Part IV - Implementations Chapter 15 – Stacks and Queues
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Array-based Stack Implementation //Implements a generic stack class public class AStack { final static int SIZE = 100; //size of all instantiated stacks Object[ ] elements; //the underlying array of the stack elements int tos; // the top of stack “pointer” //instantiate a new stack public AStack( ) { elements = new Object[SIZE]; tos = -1; } //return the top element of the stack – if one exists public Object Top( ) { if (!Empty( ) ) return elements[tos]; else
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 7

day19 - COP 3503 Computer Science II Spring 2000 CLASS...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online