CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked...

26
CS2006 - Data Structures I Chapter 7 Stacks II

Transcript of CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked...

Page 1: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

CS2006 - Data Structures I

Chapter 7Stacks II

Page 2: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

2

Topics

Implementation of ADT Stack Using Linked lists Using ADT List

Page 3: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

3

Linked List Stack Implementation

Stacks can grow and shrink dynamically No particular linked list variations are required:

The head pointer can also serve as the top of the stack.

Pushing becomes inserting the first node in a linked list.

Popping becomes removing the first node.

Page 4: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

4

Linked List Stack Implementation

The head of List is the top of the stackhead 1 3 7 9

1

3

7

9

top

Page 5: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

5

LL Stack Implementation// Assumes that the classes StackInterface and Node are available public class StackRefereenceBased implements StackInterface { private Node top; // class Node is the same used in the LinkedList class except it stores // Objects rather than Comparables

public StackRefereenceBased (){ top = null; } // end default constructor public boolean isEmpty() { return (top == null); } // end isEmpty

Page 6: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

6

LL Stack Implementation// Assumes that the classes StackInterface and Node are available public class StackRefereenceBased implements StackInterface { private Node top; // class Node is the same used in the LinkedList class except it stores // Objects rather than Comparables

public StackRefereenceBased (){ top = null; } // end default constructor public boolean isEmpty() { return (top == null); } // end isEmpty

Page 7: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

7

LL Stack Implementation (2) public void push(Object newItem) {

top = new Node(newItem, top);

} // end push

public Object pop() throws StackException {

if (!isEmpty()) {

Object temp = top.getItem();

top = top.getNext();

return temp;

}

else {

throw new StackException("StackException on " + "pop: stack empty");

} // end if

} // end pop

Page 8: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

8

LL Stack Implementation (2) public void push(Object newItem) {

top = new Node(newItem, top);

} // end push

public Object pop() throws StackException {

if (!isEmpty()) {

Object temp = top.getItem();

top = top.getNext();

return temp;

}

else {

throw new StackException("StackException on " + "pop: stack empty");

} // end if

} // end pop

Page 9: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

9

LL Stack Implementation (3)

public void popAll() {top=null;

} // end popAll public Object peek() throws StackException { if (!isEmpty()) { return top.getItem(); } else { throw new StackException("StackException on " + "peek: stack empty"); } // end else } // end peek} // end StackReferencedBased

Page 10: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

10

LL Stack Implementation (3)

public void popAll() {top=null;

} // end popAll public Object peek() throws StackException { if (!isEmpty()) { return top.getItem(); } else { throw new StackException("StackException on " + "peek: stack empty"); } // end else } // end peek} // end StackReferencedBased

Page 11: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

11

ADT Stack ADT List Implementation

All stack operations can be implemented using the ADT List's operations

The implementation is simpler to write than the array-based and makes used of previously-written code (code reuse)

Position 1 of the list represents the top position of the stack

Page 12: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

12

ADT Stack ADT List Implementation

10

80

60

.

.5

topList position

List site()

1

2

3

Page 13: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

13

ADT List Stack Implementation// Assumes that the classes ListInterface and Node are available public class StackListBased implements StackInterface { private ListInterface list; public StackListBased() { list = new ListReferenceBased(); } // end default constructor public boolean isEmpty() { return list.isEmpty (); } // end isEmpty

Page 14: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

14

ADT List Stack Implementation// Assumes that the classes ListInterface and Node are available public class StackListBased implements StackInterface { private ListInterface list; public StackListBased() { list = new ListReferenceBased(); } // end default constructor public boolean isEmpty() { return list.isEmpty (); } // end isEmpty

Page 15: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

15

List Stack Implementation (2) public void push(Object newItem) {

list.add (1, newItem);

} // end push

public Object pop() throws StackException {

if (!list.isEmpty()) {

Object temp = list.get(1);

list.remove(1);

return temp;

}

else {

throw new StackException("StackException on " + "pop: stack empty");

} // end if

} // end pop

Page 16: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

16

List Stack Implementation (2) public void push(Object newItem) {

list.add (1, newItem);

} // end push

public Object pop() throws StackException {

if (!list.isEmpty()) {

Object temp = list.get(1);

list.remove(1);

return temp;

}

else {

throw new StackException("StackException on " + "pop: stack empty");

} // end if

} // end pop

Page 17: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

17

List Stack Implementation (3)

public void popAll() { list.removeAll(); } // end popAll public Object peek() throws StackException { if (!list.isEmpty()) { return list.get(1); } else { throw new StackException("StackException on " + "peek: stack empty"); } // end else } // end peek} // end StackListBased

Page 18: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

18

ADT Stack Implementation Comparison

Array-Based   No overhead of reference manipulation   Prevents adding elements if the array is full

LL-Based More complicated than ADT List Most flexible, No size restrictions

ADT List-Based Simpler to write Not as efficient as using a linked list directly

Page 19: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

19

Review The ______ method of the ADT stack

retrieves and then removes the top of the stack. createStack push pop peek

Page 20: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

20

Review The ______ method of the ADT stack

retrieves the top of the stack, but does not change the stack. createStack push pop peek

Page 21: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

21

Review Which of the following methods of the ADT

stack accepts a parameter? push pop createStack peek

Page 22: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

22

Review If a stack is used by an algorithm to check

for balanced braces, which of the following is true once the end of the string is reached? the stack is empty the stack has one “{” the stack has one “}” the stack has one “{” and one “}”

Page 23: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

23

Review Which of the following operations of the

ADT stack does not throw a StackException? push pop popAll peek

Page 24: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

24

Review The pop operation throws a

StackException when it tries to ______. add an item to an empty stack add an item to an array-based implementation

of a stack that is already full delete an item from an array-based

implementation of a stack that is already full delete an item from an empty stack

Page 25: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

25

Review The push operation throws a

StackException when it tries to ______. add an item to an empty stack add an item to an array-based implementation

of a stack that is already full delete an item from an array-based

implementation of a stack that is already full delete an item from an empty stack

Page 26: CS2006 - Data Structures I Chapter 7 Stacks II. 2 Topics Implementation of ADT Stack Using Linked lists Using ADT List.

26

Review In the StackInterface class, the pop

method returns an item that is an instance of ______. Integer Double String Object