Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.

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
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
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.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
@ 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.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Stacks, Queues, and Deques
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
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.
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,
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.
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 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
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
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.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
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.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
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.
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.
Pointers and Linked Lists
Linked Lists.
Lists.
Chapter 16-2 Linked Structures
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Lists List: finite sequence of data elements
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Doubly Linked List Implementation
Indirection.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Linked Lists.
Recursive Linked Lists
Linked Lists.
Doubly Linked List Implementation
Linked List Improvements
Linked Lists.
Linked Lists Chapter 5 (continued)
Presentation transcript:

Linked List Improvements & Memory

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? InsertStart Insert at middle InsertEnd Retrieve First Value Retrieve Middle Value Remove From End Remove From Beginning Get Length O(1)O(n) O(1)O(n) O(1) O(n)

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 – 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 – 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!

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 nodes 4 and 5 – Copy nodes from other