CC2203-Les09Stack-for-viewing

CC2203-Les09Stack-for-viewing - 9-1/22Lesson 9: Stacks•...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 9-1/22Lesson 9: Stacks• Linear list.• One end is called top.• Other end is called bottom.• Additions to and removals from the top end only. 9-2/22Stack Of Cups• Add a cup to the stack.bottomtopCABDEF• Remove a cup from new stack.• A stack is a LIFO list.bottomtopCABDE9-3/22The Interface Stackpublic interface Stack{ public boolean empty();public Object peek(); // return top element’s valuepublic void push(Object theObject);public Object pop();}demo: javac dataStructures\Stack.javajavac dataStructures\ArrayStack.javajava dataStructures.ArrayStack9-4/22Code From Scratch: Linked Implementation• Use the ChainNode class to create an element in stack. • Use an int variable size to indicate the number of elements in stack.– Stack is empty iff size = 0.• Use a pointer top to point at the top element.– Stack elements are in ChainNode objects.– Top element is in top.element.– Bottom element is in top.next… structure e.g. if there are five elements in stack, size is 5, the bottom element is represented by top.next.next.next.next.9-5/22package dataStructures;import java.util.*;import utilities.*; public class LinkedStack implements Stack{ // data membersint ChainNode top; // pointer to the top elementint size; // number of elements in stack// Stack interface methods come here}Code From Scratch: Linked ImplementationDemo: javac dataStructures\LinkedStack.javajava dataStructures.LinkedStack9-6/22...
View Full Document

This note was uploaded on 08/18/2011 for the course COMP 3868 taught by Professor Keithchan during the Summer '97 term at Hong Kong Polytechnic University.

Page1 / 22

CC2203-Les09Stack-for-viewing - 9-1/22Lesson 9: Stacks•...

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

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