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
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
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.
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.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: 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.
List Interface and Linked List Mrs. Furman March 25, 2010.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
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:
Data Structure & Algorithms
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
  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.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Chapter 16: Linked Lists.
Lecture 6 of Computer Science II
Unit – I Lists.
C++ Programming:. Program Design Including
Linked List :: Basic Concepts
CSCI-255 LinkedList.
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
Linked Lists.
Lists.
CSCI 3333 Data Structures Linked Lists.
Chapter 20: Binary Trees.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Lists.
Chapter 21: Binary Trees.
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Arrays and Linked Lists
Chapter 18: Linked Lists.
Linked List Intro CSCE 121 J. Michael Moore.
Further Data Structures
Programming Abstractions
Dynamic Memory A whole heap of fun….
Chapter 17: Linked Lists.
Linked Lists.
Dynamic Memory A whole heap of fun….
Lists.
Lecture No.02 Data Structures Dr. Sohail Aslam
General List.
Linked List Functions.
Building Java Programs
Linked Lists.
Linked List Intro CSCE 121.
Linked List Improvements
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 Up to n wasted space (if just grew) 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 Memory + No extra space for future growth - Extra required for pointers : 4/8 bytes per node

Linked List Implementation will change performance Memory allocated in lots of pieces + no worries about allocating big blocks - lots of calls to new/delete - poorer cache use

Linked List Implementation will change performance BigO's will change…

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

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

Linked List Nodes 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

Nodes vs Pointers new Node: Creating a new permanent thing Need a pointer to keep track of it Need to delete at some point

Nodes vs Pointers pointer variable: May point at May need to delete New memory Existing memory May need to delete Only if “owns” memory

Linked List LinkedList : Object that manages linked ListNode chain Minimal implementation: 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

BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value Retrieve Middle Value Remove From End Remove From Beginning Get Length

BigO's What is BigO for our basic linked list operations? O(1) O(n) InsertStart Insert at middle InsertEnd Retrieve First Value Retrieve Middle Value Remove From End Remove From Beginning Get Length  O(1) O(n)  O(n)   O(1) O(n)   O(1)