Linked Lists. Array List Issues Painful insert/remove at start/middle.

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.
Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
DATA STRUCTURES USING C++ Chapter 5
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists
Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
CCSB364 Data Structures & Algorithms Pointer & Linked List.
Data Structures Data Structures Topic #5. Today’s Agenda Other types of linked lists –discuss algorithms to manage circular and doubly linked lists –should.
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.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
1 CSC 211 Data Structures Lecture 10 Dr. Iftikhar Azim Niaz 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
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 Writing a Good Program 8. Elementary Data Structure.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. 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.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Internal Sorting File Sorting Part 2.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
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 © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 20: Binary Trees.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
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:
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Array 10 GB Hard Disk 2 GB 4 GB2 GB 3 GB DATA 4 GB Free Store data in the form of Array (Continuous memory locations) Solution-1: No Solution. Memory Insufficient.
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.
  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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
C++ Programming:. Program Design Including
Linked lists.
Programming Abstractions
Linked Lists.
Chapter 20: Binary Trees.
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
Chapter 17: Linked Lists.
Lecture No.02 Data Structures Dr. Sohail Aslam
Linked List Functions.
Linked Lists.
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 worst case larger than array – 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 still 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 Size of list not explicitly 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