Linked structures. Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists.

Slides:



Advertisements
Similar presentations
So what's next? Introduction to data structures Two Perspectives: Abstract description (capabilities) Implementation(s) For structures: Stack Queue Deque.
Advertisements

6-1 6 Stack ADTs Stack concepts. Stack applications. A stack ADT: requirements, contract. Implementations of stacks: using arrays, linked lists. Stacks.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
COMP 103 Linked Stack and Linked Queue.
Searching and Sorting. Overview Search Analysis of search algorithms Sorting Analysis of sort algorithms Recursion p. 2 of 26.
Priority Queues and Heaps. Overview Our last ADT: PriorityQueueADT A new data structure: heaps One more sorting algorithm: heapsort Priority Queues and.
And now for something completely different: recursion.
DictionaryADT and Trees. Overview What is the DictionaryADT? What are trees? Implementing DictionaryADT with binary trees Balanced trees DictionaryADT.
OrderedListADT. Overview OrderedListADT: not the same as linked lists The OrderedListADT: operations and sample applications An ArrayList implementation.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Stack Implementations Chapter Chapter Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Stack Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (
Chapter 13 Linked Structures - Stacks. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked implementation.
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Lecture Objectives To understand how Java implements a stack To learn how to implement a stack using an underlying array or linked list Implement a simple.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Stacks and Queues Introduction to Computing Science and Programming I.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Stacks and Queues Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming.
6 Stack ADTs  Stack concepts  Stack applications  Stack ADTs: requirements, contracts  Implementations of stacks: using arrays and linked-lists  Stacks.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 18 Stacks and Queues.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
Stacks And Queues Chapter 18.
1 Lecture 14: Queues Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Linked Structures - Review Chapter 13 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Linked Structures, LinkedStack
CS 367 Introduction to Data Structures Lecture 5.
Computer Science 209 Software Development Inheritance and Composition.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Lecture Objectives  To understand how Java implements a stack  To learn how to implement a stack using an underlying array or linked list  Implement.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Stacks – Cont’d Nour El-Kadri ITI Summary We have seen that arrays are efficient data structures. Accessing any element of an array necessitates.
Click to edit Master text styles Stacks Data Structure.
Linked Structures - Stacks. Linked Structures An alternative to array-based implementations are linked structures A linked structure uses object references.
Section 3.7 Linked-Based Implementations
Stack: a Linked Implementation
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
CHAPTER 4: Linked Structures
Storage Strategies: Dynamic Linking
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Top Ten Words that Almost Rhyme with “Peas”
Stacks Linked Lists Queues Heaps Hashes
Implementations of the ADT Stack
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Stack Implementations
Stacks public interface Stack { public boolean empty();
Lecture 2: Stacks and Queues
Stacks Chapter 5.
Stack Implementations
CHAPTER 3: Collections—Stacks
Presentation transcript:

Linked structures

Overview Overview of linked structures (for lists) Design decision: arrays vs. linked lists Implementing the StackADT with linked lists Using StackADT: the maze program Java's built-in java.util.Stack class Time complexity of stack operations Coming attractions Linked Structures p. 2/19

Linked-lists overview Linked Structures p. 3/19 ABCDEFG vs. ACB

The code public class Person { private String name; // could add address, etc. private Person next; public Person (String n) { this.name = n; next = null; } public String getName() { return name; } //plus get+set methods for ``next'‘ } Linked Structures p. 4/19

The code (2) public class LinearNode { private T element; private LinearNode next; public LinearNode (T elem) { this.element = elem; next = null; } public T getElement() { return element; } //plus get+set methods for ``next'‘ } Linked Structures p. 5/19

Design decision: arrays vs. linked lists Which structure allows quicker access to its elements? A. an array B. a linked list C. both D. neither Linked Structures p. 6/19

Design decision: arrays vs. linked lists (2) Which structure uses space more efficiently if the size of the list stays the same during runtime? A. an array B. a linked list Linked Structures p. 7/19

Design decision: arrays vs. linked lists (3) Which structure uses space more efficiently if the size of the list varies significantly during runtime? A. an array B. a linked list Linked Structures p. 8/19

Arrays vs. linked lists (summary) FeatureArraysLinked Lists Faster access to elements X More efficient use of space if size of list is constant durin g runtime X More efficient use of space if size of list varies significantly X Can be used to implement StackADT, etc. XX Linked Structures p. 9/19

Using linked lists: StackADT Consider the code for the LinkedStack (p. 80). Draw box and arrow diagrams to show, step by step, what happens when this code is executed to 1. Create a LinkedStack 2. Add a node containing the String ``Lion'' followed by a second node containing the String ``Tiger'' 3. Pop the top element off of the LinkedStack Linked Structures p. 10/19

Using linked lists: StackADT (2) Compare the code for the LinkedStack (p. 80) with the code for the ArrayStack (p. 58). Line by line, what are the differences? How do the differences in the two data structures explain the differences in the code? Linked Structures p. 11/19

Using linked lists: StackADT (3) Compare the code for the LinkedStack (p. 80) with the code for the ArrayStack (p. 58). Line by line, what are the differences? How do the differences in the two data structures explain the differences in the code? Linked Structures p. 12/19

Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class ( til/Stack.html). What are the design flaws in this class? Explain. Hint: can it guarantee that the item you pop will always be the last item pushed? Linked Structures p. 13/19

Time Complexity Consider the LinkedStack code on pp The Big-Oh time complexity of the push operation is: A. O(1) B. O(log 2 n) C. O(n) D. O(n 2 ) E. none of the above Linked Structures p. 14/19

Time Complexity (2) Consider the LinkedStack code on pp The Big-Oh time complexity of the pop operation is: A. O(1) B. O(log 2 n) C. O(n) D. O(n 2 ) E. none of the above Linked Structures p. 15/19

Time Complexity (3) Consider the LinkedStack code on pp The Big-Oh time complexity of the peek operation is: A. O(1) B. O(log 2 n) C. O(n) D. O(n 2 ) E. none of the above Linked Structures p. 16/19

Time Complexity (4) Consider the LinkedStack code on pp The Big-Oh time complexity of the isEmpty operation is: A. O(1) B. O(log 2 n) C. O(n) D. O(n 2 ) E. none of the above Linked Structures p. 17/19

Arrays vs. linked lists (continued) OperationBig-Oh of Stack Implementation Big-Oh of Linked-List Implementation push pop peek isEmpty Linked Structures p. 18/19

Coming attractions Next time we'll look at our second Abstract Datatype, QueueADT, and consider how to implement it with either an array or a linked list. Homework: read Chapter 5 Queues (or the equivalent in the earlier edition). Linked Structures p. 19/19