Linked Structures Chapter John Wiley & Sons, Data Structures and Algorithms Using Python, by Rance D. Necaise.

Similar documents
Linked Lists. Linked List vs Array vs Python List Singly Linked List Bag ADT Revisited Building Linked Lists

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists

CS 331 Midterm Exam 2

Computer Science Fundamentals 107

Data Structures I: Linked Lists

Agenda & Reading. COMPSCI 105 SS 2015 Principles of Computer Science

Algorithms in Systems Engineering ISE 172. Lecture 5. Dr. Ted Ralphs

COMPSCI 105 S Principles of Computer Science. 17 Linked List(1)

CSI33 Data Structures

Outline. LList: A Linked Implementation of a List ADT Iterators Links vs. Arrays In-class work. 1 Chapter 4: Linked Structures and Iterators

CMPT 225. Lecture 6 linked list

n 1 i = n + i = n + f(n 1)

CS 331 Midterm Exam 2

About the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix

Ashish Gupta, Data JUET, Guna

Linked lists. A linked list is considered a recursive data structure because it has a recursive definition.

Queues. Queue ADT Queue Implementation Priority Queues

Announcements. Lab 11 is due tomorrow. Quiz 6 is on Monday. Ninja session tonight, 7-9pm. The final is in two weeks!

What is an algorithm?

Walk through previous lectures

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

CS 331 Fall 2017 Midterm Exam 2

Data Structures CSCI C343, Fall 2014

CS32 Discussion Week 3

CS 331/401 Summer 2018 Midterm Exam

Advanced Sorting. Merge Sort Quick Sort Radix Sort

Basic Data Structures (Version 7) Name:

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

Queues The Queue ADT. The Queue ADT is defined by the following operations: init Initialize a new empty queue.

In addition to the correct answer, you MUST show all your work in order to receive full credit.


Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

16. Dynamic Data Structures

CS 331 Summer 2017 Final Exam

Basilisk Documentation

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

CS532: Design and Analysis of Algorithms. Overview. Linked Lists. Data Structures Intro. Classes, Objects, and Pointers.

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Stack. Stack theory. Stack exercises. Algolab (index.html#chapters) Out[1]: Chapter 2.2: Data Structures - Stacks and.

Binary Search Tree. Revised based on textbook author s notes.

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

CS 331 Midterm Exam 1

A linked list grows as data is added to it. In a linked list each item is packaged into a node.

The time and space are the two measure for efficiency of an algorithm.

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

Data Structures & Algorithms

CSI33 Data Structures

CS 234. Module 3. September 29, CS 234 Module 3 Data structures, ADTs with no order of any kind, Python review 1 / 19

LINKED IMPLEMENTATION OF LIST

Programming. Lists, Stacks, Queues

Linked List. ape hen dog cat fox. tail. head. count 5

CSCA48 Term Test 1 Seminar

CS 331 Midterm Exam 2

03/31/03 Lab 7. Linked Lists

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

Senior Symposium Friday, April 16

Linked Lists. Chapter 4

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

BBM 201 DATA STRUCTURES

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

+ Abstract Data Types

Data Structures (CS 1520) Lecture 23 Name:

Binary Tree Application Expression Tree. Revised based on textbook author s notes.

Data structure and algorithm in Python

Extended Introduction to Computer Science CS1001.py

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

Chapter 17: Linked Lists

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

ECE 2035 Programming HW/SW Systems Spring problems, 5 pages Exam Three 8 April Your Name (please print clearly)

Stack. S Array or Vector representing stack containing N elements

Abstract Data Types Chapter 1

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 4

Structure and Interpretation of Computer Programs

Linked List using a Sentinel

Chapter 17: Linked Lists

Python Lists and for Loops. Learning Outcomes. What s a List 9/19/2012

Lecture 16: Linked Lists

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Data structure and algorithm in Python

A collision is said to occur when two or more keys hash to the same index location.

QUIZ. 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different?

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 3

Short Python function/method descriptions:

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

Data Structures and Algorithms

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

Lesson 3: Understanding General Software Development

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Ch. 17: Linked Lists. Introduction to Linked Lists

Lecture #15: Generic Functions and Expressivity. Last modified: Wed Mar 1 15:51: CS61A: Lecture #16 1

CSE 230 Intermediate Programming in C and C++

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

DNHI Homework 3 Solutions List, Stacs and Queues

Introduction to Programming in C Department of Computer Science and Engineering

Name: UTLN: CS login: Comp 15 Data Structures Midterm 2018 Summer

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Problem 1: Hello World!

Transcription:

Linked Structures Chapter 6

Linked Structure Constructed using a collection of objects called nodes. Each node contains data and at least one reference or link to another node. Linked list a linked structure in which the nodes are linked together in linear order. Chapter 6: Linked Structures 2

Linked List Terms: head first node in the list. tail last node in the list; link field has a null reference. Chapter 6: Linked Structures 3

Node Definition The nodes are constructed from a simple storage class: class _ListNode: def init ( self, data ): self.data = data self.next = None Chapter 6: Linked Structures 4

Traversing the Nodes We can traverse the nodes using a temporary external reference variable. Initialize a temporary reference to the head node. Visit the node. Chapter 6: Linked Structures 5

Traversing the Nodes Advance the temporary reference to the next node using the link field and visit that node. Chapter 6: Linked Structures 6

Traversing the Nodes Repeat the process until the reference falls off the end of the list. Chapter 6: Linked Structures 7

Searching We can perform a linear search to determine if the list contains a specific data item. Chapter 6: Linked Structures 8

Prepending Nodes When working with an unsorted linked list, new values can be inserted at any point. We can prepend new items with little effort. Example: add value 96 to the sample list. Chapter 6: Linked Structures 9

Prepending Nodes Create a new node for the new item. Connect the new node to the list. Chapter 6: Linked Structures 10

Removing Nodes Removing a node from the middle of the list requires a second external reference. Resulting list. Chapter 6: Linked Structures 11

Removing Nodes Removing the first node is a special case. The head reference must be reposition to reference the next node in the list. Chapter 6: Linked Structures 12

Using a Tail Reference Some applications require items be appended to the end of the linked list. tail reference a second external reference indicating the tail or last node in the list. Chapter 5: Searching and Sorting 13

Appending Nodes Must manage the tail reference as nodes are added/removed. Example: append 21 to the list. Chapter 5: Searching and Sorting 14

Appending Nodes Given the head and tail reference, we can add an item to a linked list. newnode = ListNode( item ) if self._head is None : self._head = newnode self._tail = newnode else : self._tail.next = newnode self._tail = newnode What is the time complexity to append a node to a linked list, if no tail reference is used? Chapter 6: Linked Structures 15

Removing Nodes If the tail node is removed, the tail reference has to be adjusted. Chapter 5: Searching and Sorting 16

The Sorted Linked List The items in a linked list can be maintained in sorted order. Chapter 5: Searching and Sorting 17

Sorted List: Searching Searching a sorted list is similar to that of an unsorted list. def sortedsearch( head, target ): curnode = head # Stop early when a larger value is encountered. while curnode is not None and \ target <= curnode.data : if curnode.data == target : return True else : curnode = node.next return False Chapter 6: Linked Structures 18

Sorted List: Insert Adding a new node to a sorted list requires locating the correct position within the list. Locating the position is similar to the removal operation. Use a second temporary reference for the predecessor. There are 3 possible cases. front middle back Chapter 6: Linked Structures 19

Sorted List: Insert (1) Insert at the front. Chapter 6: Linked Structures 20

Sorted List: Insert (2) Insert in the middle. Chapter 6: Linked Structures 21

Singly Linked List / Python List Comparison Operation Linked List Python List append(item) insert(0, item) pop(0) pop(i) getitem (i) setitem (i, item) When does a Linked List make more sense than a contiguous representation? Chapter 6: Linked Structures 22

Singly Linked List / Python List Comparison Operation Linked List Python List append(item) O(n) O(1)* insert(0, item) O(1) O(n) pop(0) O(1) O(n) pop(i) O(n) O(n) getitem (i) O(n) O(1) setitem (i, item) O(n) O(1) When does a Linked List make more sense than a contiguous representation? * Amortized Chapter 6: Linked Structures 23