Linked List Improvements

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review Learn about 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.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Data Structures: A Pseudocode Approach with C
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Comparison summary Array based (dynamic) Keeps place for up to 4N elements Each element takes 1 memory places Fast accession time Slow removals and insertion.
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Zhigang Zhu Department.
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.
Lists: array implementation list_size = 5 lst Obj 1Obj 2Obj 3Obj 4Obj 5.
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.
Stacks, Queues, and Deques
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Linked Lists. 2 Anatomy of a linked list A linked list consists of: A sequence of nodes abcd  Each node contains a value  and a link (pointer or reference)
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
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.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Anatomy of a linked list
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.
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,
UNIT-3 LINKED LIST.
Chapter 4 Linked Lists.
Linked lists.
EEL 4854 IT Data Structures Linked Lists
Linked Lists.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
LinkedList Class.
The Bag and Sequence Classes with Linked Lists
Pointers and Linked Lists
Linked Lists.
Lists.
Chapter 16-2 Linked Structures
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Linked Lists.
Doubly Linked List Implementation
Indirection.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Linked Lists.
Linked Lists.
Recursive Linked Lists
Linked List Functions.
Programming II (CS300) Chapter 07: Linked Lists
Linked Lists.
Stacks, Queues, and Deques
Recursive Linked Lists
Linked Lists.
Doubly Linked List Implementation
Linked lists.
Linked Lists.
Presentation transcript:

Linked List Improvements

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) 

Linked List Gripes Issues with basic linked list: No easy way to get length No easy way to put something at the end

Length Keep track of length of list listSize() now O(1) Increment on any insert Decrement on removal listSize() now O(1)

Length Side Benefit Cheap list size can simplify other algorithms Error check early

InsertEnd Want to efficiently implement: InsertEnd(int value) What do we need?

Tail Pointer Tail pointer Always points to last element

InsertEnd InsertEnd(value) Node* newNode = new Node(value) tail->next = newNode tail = newNode length++

InsertEnd InsertEnd(value) Node* newNode = new Node(value) tail->next = newNode tail = newNode length++

InsertEnd InsertEnd(value) Node* newNode = new Node(value) tail->next = newNode tail = newNode length++

InsertEnd InsertEnd(value) Node* newNode = new Node(value) tail->next = newNode tail = newNode length++

InsertEnd Special case : insertEnd on empty list InsertEnd(value) Pass off to insertStart InsertEnd(value) If length == 0 insertStart(value) return Node* newNode = new Node(value) tail->next = newNode tail = newNode length++

Tail Issues InsertEnd(value) now O(1) But need to worry about tail in other situations InsertStart on empty list  InsertAt on last node RemoveAt on last node

Tail Issues Example: insertStart on empty list

Tail Issues Example Removing at current tail removeAt(2) called Found current and nodeToRemove

Tail Issues Example Removing at current tail removeAt(2) called Removed node from list

Tail Issues Example Removing at current tail removeAt(2) called Deleted from memory

Tail Issues Example Removing at current tail removeAt(2) called We have a new tail!

BigO's What is BigO for our improved linked list operations? With length & tail Get Length Insert Start Insert at InsertEnd Retrieve Retrieve Remove Remove middle First / L ast Middle Beginning End Value Value

BigO's What is BigO for our improved linked list operations? O(1) O(1) With length & tail Get Length Insert Start Insert at InsertEnd Retrieve Retrieve Remove Remove middle First / L ast Middle Beginning End Value Value O(1) O(1) O(n) O(1) O(1) O(n) O(1) O(n)

Linked List & Memory

Memory Management Destructor needs to free allocated nodes Must delete each individually

Destruct ~LinkedList() Node* temp = head while head is not null head = head->next delete temp temp = head

Destruct ~LinkedList() Node* temp = head while head is not null head = head->next delete temp temp = head

Destruct ~LinkedList() Node* temp = head while head is not null head = head->next delete temp temp = head

Destruct ~LinkedList() Node* temp = head while head is not null head = head->next delete temp temp = head

Destruct Alternative Destructor made simple: ~LinkedList() while head is not null removeFirst

Copy Constructor Want a deep copy Whole new list of nodes with identical data, in same order Length, head and tail set correctly foreach node in other get node's value insert value at end

Copy Constructor Want a deep copy Whole new list of nodes with identical data, in same order Length, head and tail set correctly foreach node in other get node's value insert value at end

Copy Constructor Want a deep copy Whole new list of nodes with identical data, in same order Length, head and tail set correctly foreach node in other get node's value insert value at end

Copy Constructor Want a deep copy Whole new list of nodes with identical data, in same order Length, head and tail set correctly foreach node in other get node's value insert value at end

Copy Constructor Want a deep copy Whole new list of nodes with identical data, in same order Length, head and tail set correctly foreach node in other get node's value insert value at end …

Assignment Operator myList = other Delete all nodes Copy nodes from other