Download presentation
Presentation is loading. Please wait.
1
1 Lecture 26 Abstract Data Types – IV Overview The List ADT Implementing Stacks as Linked List Linked List Implementation of Queues . Preview: Abstract Data Types V
2
2 Lecture 26 The Stack ADT l A stack is a list that limits insertions and removals to the front (top) of the list. l Operations »Push: insert an object onto the top of the stack. »Pop: remove the top object from the stack. »Empty: returns true if the stack is empty. »Peek: retrieve the top object without removing it. Top In a Stack, insertions and deletions occur at the top New Top
3
3 Lecture 26 Implementing Stacks as Linked List The original array implementations of Stacks and Queues suffered from inefficiency in that the array was fixed in size. Too small and it was useless, too big and it wasted resources. Both stacks and queues can be implemented more efficiently using linked lists to maintain the stack and queue data. n Stack - push and pop from front of list (Header) (push and pop from here) Click here for link Linked List Implementation of Stack Linked List Implementation of Stack
4
4 Lecture 26 The Stack Class l A Stack is very easy to implement as an extension of our generic List structure. public class Stack extends List { public Stack() { super(); } public void push( Object obj ) { insertAtFront(obj); } public Object pop() { return removeFirst(); } } // Stack Invoke the List() constructor. Abstract Data Type: We limit access to the data to push() and pop().
5
5 Lecture 26 Testing the Stack Class l Stack behavior: Last In First Out (LIFO) l Use a stack to reverse a string. public static void main( String argv[] ) { Stack stack = new Stack(); String string = "Hello this is a test string"; System.out.println("String: " + string); for (int k = 0; k < string.length(); k++) stack.push(new Character( string.charAt(k))); Object o = null; String reversed = ""; while (!stack.isEmpty()) { o = stack.pop(); reversed = reversed + o.toString(); } System.out.println("Reversed String: " + reversed); } // main() Push each character. Then pop each character.
6
6 Lecture 26 The Queue ADT l A queue is a list that limits insertions to the rear and removals to the front of a list. l Operations »Enqueue: insert an object onto the rear of the list. »Dequeue: remove the object at the front of the list. »Empty: return true if the queue is empty. Head In a queue, insertions take place at the rear, and removals occur at the front New Head
7
7 Lecture 26 Linked List Implementation of Queues The original array implementations of Stacks and Queues suffered from inefficiency in that the array was fixed in size. Too small and it was useless, too big and it wasted resources. Both stacks and queues can be implemented more efficiently using linked lists to maintain the stack and queue data. n Queue - enQueue at end of list and deQueue from head of list List (Header) (dequeue from here) (enqueue here) Click here for link Linked List Implementation of Queue Linked List Implementation of Queue
8
8 Lecture 26 The Queue Class l A Queue is very easy to implement as an extension of our generic List structure. public class Queue extends List { public Queue() { super(); } public void enqueue(Object obj) { insertAtRear( obj ); } public Object dequeue() { return removeFirst(); } } // Queue Invoke the List() constructor. Abstract Data Type: We limit access to the data to enqueue() and dequeue().
9
9 Lecture 26 Java Library: java.util.LinkedList Java provides a LinkedList class public class LinkedList extends AbstractSequentialList implements List { public void add(int index, Object o); // Inserts o at index position public void addFirst(Object o); // Inserts o at beginning of list public void addLast(Object o); // Appends o to end of list public Object get(int index); // Returns list element at index public Object getFirst(); // Returns first element in the list public Object getLast(); // Returns last element in list. public Object removeFirst(); // Removes and returns first element public Object removeLast(); // Removes and returns last element } Implements the List interface. l Advantage: The methods can be attached to a wide range of sequential structures.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.