Revised based on textbook author’s notes.

Slides:



Advertisements
Similar presentations
Linear Lists – Linked List Representation
Advertisements

Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Computer Science 112 Fundamentals of Programming II Lists.
COMPSCI 105 S Principles of Computer Science Linked Lists 2.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Summary of lectures (1 to 11)
COMPSCI 105 S Principles of Computer Science Linked Lists 1.
Lecture 14 Linked Lists 14-1 Richard Gesick. Linked Lists Dynamic data structures can grow and shrink at execution time. A linked list is a linear collection.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues.
COMPSCI 105 SS 2015 Principles of Computer Science Linked Lists 1.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
COMPSCI 105 SS 2015 Principles of Computer Science
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Fundamentals of Programming II Linked Lists
Lecture 6 of Computer Science II
Review Array Array Elements Accessing array elements
COSC160: Data Structures Linked Lists
Cpt S 122 – Data Structures Abstract Data Types
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
CE 221 Data Structures and Algorithms
Doubly Linked List Review - We are writing this code
Data Structures and Algorithms
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
Advanced Linked lists Doubly Linked and Circular Lists
EEL 4854 IT Data Structures Linked Lists
Java collections library
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Revised based on textbook author’s notes.
Lists.
Object Oriented Programming COP3330 / CGS5409
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Lecture 20 Linked Lists Richard Gesick.
Linear Data Structures Chapter 3
Revised based on textbook author’s notes.
Binary Tree Application Operations in Heaps
Linked Lists.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
CS212D: Data Structures Week 5-6 Linked List.
Computer Science 210 Computer Organization
Node Applications in Abstract Data Types
Python LinkedLists.
Binary Tree Application Heap Sorting
Data Structures and Algorithms
Problem Understanding
Linear Search Binary Search Tree
Chapter 17: Linked Lists.
LINKED LISTS.
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
Lecture 14 Linked Lists CSE /26/2018.
Data Structures & Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
Building Java Programs
Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/11.
Binary Tree Implementation
Revised based on textbook author’s notes.
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

Revised based on textbook author’s notes. Singly Linked lists Revised based on textbook author’s notes.

Consider this problem Given a sorted list of numbers, insert a new number into this sorted list [3, 5, 7, 10, 12, 20] insert 6 into this list to become [3, 5, 6, 7, 10, 12, 20] How do YOU accomplish this seemly simple task? Take 5 min to work it out.

Here is a possible solution n steps n steps n steps How many steps to complete this work? It takes 3*n steps to do this. Can we do better? How?

Let’s look at one issue at a time new_list = [x for x in my_list] + [k] We need to increase the capacity of the list to hold the new element. Weather the list is implemented as a Python list or an array, this would take n steps. i = find_pos(k, my_list) We need to find the right spot for the new number, which takes n steps for j in range(len(new_list)-1, i-1, -1): Shifting elements to the right takes n steps

Linked lists Use linked list we can make two of the three operations in constant time!

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.

Linked List Terms: node – each element in the list. head – first node in the list. tail – last node in the list; link field has a null reference. tail

How does it look like in Python? The nodes are constructed from a simple storage class: List contains two nodes, a head and a tail class ListNode: def __init__( self, data ): self.data = data self.next = None class UserList: def __init__( self ): self.head = None self.tail = None

How to build a list? my_list = UserList() # initial list head == None a_node = ListNode(12) # create a node my_list.insert_after(node) # insert the node to list my_list.insert_after(ListNode(3))# another node tail head 12 The ‘next’ field has a value of None tail head 12 3

Try out an example

Exercise: write the function insert_before() that inserts the node before the head.

Removing nodes An item can be removed from a linked list by removing or unlinking the node containing the item. Find the node containing the item. Unlink it from the list.

Removing nodes Removing a node from the middle of the list requires a second external reference. Resulting list.

Removing Nodes Removing the first node is a special case. The head reference must be reposition to reference the next node in the list.

Removing nodes Given the head reference, we can remove a target from a linked list.