Linked Lists.

Slides:



Advertisements
Similar presentations
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
1 Joe Meehean.  Conceptual Picture N items chained together using pointers pointed to by head variable  Advantage allows list to grow indefinitely without.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
Linked Lists and Generics Written by J.J. Shepherd.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
An Array-Based Implementation of the ADT List
Chapter 16: Linked Lists.
Linked Lists.
Lecture 6 of Computer Science II
Unit – I Lists.
C++ Programming:. Program Design Including
Linked List :: Basic Concepts
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
UNIT – I Linked Lists.
Review Deleting an Element from a Linked List Deletion involves:
Lists CS 3358.
Linked lists.
EEL 4854 IT Data Structures Linked Lists
Programming Abstractions
Lists.
CSCI 3333 Data Structures Linked Lists.
Chapter 20: Binary Trees.
Linked List Sudeshna Sarkar.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists.
Lists.
Chapter 21: Binary Trees.
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Arrays and Linked Lists
Chapter 18: Linked Lists.
Further Data Structures
Programming Abstractions
Dynamic Memory A whole heap of fun….
Chapter 17: Linked Lists.
Dynamic Memory A whole heap of fun….
Lists.
Data Structures & Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
General List.
Linked List Functions.
Linked Lists.
Linked List Intro CSCE 121.
Linked List Improvements
Linked lists.
Linked Lists.
Presentation transcript:

Linked Lists

Array List Issues Painful insert/remove at start/middle

Array List Issues Painful insert/remove at start/middle Average ~n/2 wasted space Worse if shrinks

Array List Issues Painful insert/remove at start/middle Average ~n/2 wasted space Worse if shrinks Data kept in a single block Memory fragmentation may prevent grow(): array other data

Linked List Linked List : Implement List ADT with linked series of nodes One value per node Pointer to next node

Linked List Implementation will change performance Space always based on current size of list But extra required for pointers Data distributed in lots of little pieces + no worries about allocating big blocks - cache coherence BigO's will change

Linked List List Node: Stores one item of data Stores a pointer to another node

Linked List Linked ListNodes: Each node starts with nullptr for next value

Linked List Linked ListNodes: Each node starts with nullptr for next value Set next to address of other node to form a chain End of chain points to null

Linked List Goal : List ADT implemented with LinkNodes What does list need to keep track of? Bare minimum: Where first node is

Traversal Traverse Linked Lists with pointer Start at head

Traversal Traverse Linked Lists with pointer Start at head Advance by following next

Traversal Traverse Linked Lists with pointer Start at head Advance by following next

Traversal Traverse Linked Lists with pointer Start at head Advance by following next Stop when you hit null

Putting It Together Print each item:

listSize int listSize() If size not stored… Find by traversing and counting: int listSize() count = 0 current = head while current is not null count++ current = current->next return count

Partial Traversal Which node is at index 2? How do we retrieve the value?

Partial Traversal Which node is at index 2? How do we retrieve the value? Start from head (0) Advance to next twice Return data

Partial Traversal retrieveAt(num) Start from head (0) Advance to next num times Print what is there

Partial Traversal retrieveAt(num) Start from head (0) Advance to next num times If hit nullptr, error Print what is there currentnext on nullptr will blow up

Remove Start How do we remove the first node?

Remove Start Bad attempt 1: head  head->next node1 is now garbage need to delete but no pointer!

Remove Start Bad attempt 2: delete head Lost track of node 2

Remove Start Can't delete node1 unless we store link first RemoveFirst() if head == nullptr error Node* temp = head->next delete head head = temp

Remove Start Can't delete node1 unless we store link first RemoveFirst() if head == nullptr error Node* temp = head->next delete head head = temp

Remove Start Can't delete node1 unless we store link first RemoveFirst() if head == nullptr error Node* temp = head->next delete head head = temp

Insert Start Now we want to add 12 to start of list…

Insert Start Now we want to add 12 to start of list Need a new Node InsertFirst(12) Node* temp = new Node(12)

Insert Start Now we want to add 12 to start of list new node will point to old head InsertFirst(12) Node* temp = new Node(12) temp->next = head

Insert Start Now we want to add 12 to start of list head can then point to temp InsertFirst(12) Node* temp = new Node(12) temp->next = head head = temp

Insert At Want to insert a new value at index 2

Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value)

Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head

Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head Advance to next 2 times

Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head Advance to next 2 times TOO FAR No way to adjust node2's pointer!

Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head Advance to next 1 times

Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head Advance to next 1 times newNode->next = temp->next

Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head Advance to next 1 times newNode->next = temp->next temp->next = newNode

Insert At Want to insert a new value at index 2 InsertAt(index, value) Node* newNode = new Node(value) Node* temp = head Advance to next (index – 1) times newNode->next = temp->next temp->next = newNode What special case still won't work?

Insert At Want to insert a new value at index InsertAt(index, value) If index = 0 InsertFirst(value) Else Node* newNode = new Node(value) Node* temp = head Advance to next (index – 1) times If temp == nullptr error newNode->next = temp->next temp->next = newNode