Chapter 17 Linked List.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

JAVA & Linked List Implementation
Linked Lists Chapter 4.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Data Structures Using C++
Linked Lists CENG 213 Data Structures.
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.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Data Structures & Algorithms
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.
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.
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
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.
Chapter 3: Arrays, Linked Lists, and Recursion
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
Data Structures Using C++ 2E
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.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
Data Structures Using Java1 Chapter 4 Linked Lists.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 18 Linked Lists, Stacks, Queues, and Priority Queues.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
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.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Data Structures Using C++1 Chapter 5 Linked Lists.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 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.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Data.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture1.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
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.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
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 and Algorithm Analysis Dr. Ken Cosh Linked Lists.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
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.
One implementation of the LIST ADT Insert new node before current and new node becomes current (assume new node created) node newNode = new node; head.
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.
Linked List ADT used to store information in a list
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.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Circularly Linked Lists
Chapter 18: Linked Lists.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Chapter 17: Linked Lists.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
slides created by Marty Stepp
Presentation transcript:

Chapter 17 Linked List

Objective Linked lists basic ideas: header nodes and iterator classes Implementation details doubly linked lists circular linked lists

Arrays Linked Lists arrayname contiguous direct access of elements insertion / deletion difficult Linked Lists noncontiguous must scan for element insertion /deletion easy arrayname

Iterating through a data structure for (int i = 0; i < length; i++) cout<< a[i]; for (ListNode p = theList.first; p != null; p = p.next) cout<< p.data ;

Adding an element A0 A1 A2 first last class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: last->next = new ListNode(); last = last->next; last->data = x; last->next = null;

A0 A1 A2 first last class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: last->next = new ListNode() ; last = last->next; last->data = x; last->next = null;

A0 A1 A2 first last class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: last->next = new ListNode(); last = last->next; last->data = x; last->next = null;

A0 A1 A2 x first last class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: last->next = new ListNode(); last = last->next; last->data = x; last->next = null;

A0 A1 A2 x first last class ListNode { Object data; ListNode* next; } At any point, we can add a new last item x by doing this: last->next = new ListNode(); last = last->next; last->data = x; last->next = null;

Inserting an element A0 A1 A2 current first last class ListNode { Object element; ListNode* next; } At any point, we can add a new last item x by doing this: tmp = new ListNode(); tmp->element = x; tmp->next = current->next; Current->next = tmp;

A0 A1 A2 current first last tmp class ListNode { Object element; ListNode* next; } At any point, we can add a new last item x by doing this: tmp = new ListNode(); tmp->element = x; tmp->next = current->next; Current->next = tmp; tmp

A0 A1 A2 current x first last tmp class ListNode { Object element; ListNode* next; } At any point, we can add a new last item x by doing this: tmp = new ListNode(); tmp->element = x; tmp->next = current->next; current->next = tmp; tmp

A0 A1 A2 current x first last tmp class ListNode { Object element; ListNode* next; } At any point, we can add a new last item x by doing this: tmp = new ListNode(); tmp->element = x; tmp->next = current->next; current->next = tmp; tmp

A0 A1 A2 current x first last tmp class ListNode { Object element; ListNode* next; } At any point, we can add a new last item x by doing this: tmp = new ListNode(); tmp->element = x; tmp->next = current->next; current->next = tmp; tmp

Simplified version current->next = new ListNode(x, current->next);

Deleting an element A0 A1 A2 current last class ListNode { Object element; ListNode* next; } current->next = current->next->next;

Deleting an element A0 A1 A2 current last class ListNode { Object element; ListNode* next; } Current->next = current->next->next; Memory leak!

Deleting an element A0 A1 A2 current last Node *deletedNode = current->next; current->next = current->next->next; delete deletedNode;

Header Nodes a b c header Header nodes allow us to avoid special cases [in the code] such as insertion of the first element and removal of the last element. The header node holds no data but serves to satisfy the requirement that every node have a previous node. Not necessarily a standard implementation.

C++ implementation We have a list. This list consists of listNodes. In order to access these listNodes, we need an iterator. Code: online

Doubly Linked Lists a b c head tail Consider how hard it is to back up in a singly linked list. Also consider text editor example class DoubleListNode { Object element; ListNode* next; ListNode* prev; }

Empty Doubly Linked List c head tail // constructor DoubleList() { head = new DoubleListNode (); tail = new DoubleListNode (); head->next = tail; tail->prev = head; }

Inserting into a Doubly Linked List c head tail current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

Inserting into a Doubly Linked List c b head tail current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

Inserting into a Doubly Linked List c b head tail current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

Inserting into a Doubly Linked List c b head tail current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

Inserting into a Doubly Linked List c b head tail current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

Inserting into a Doubly Linked List c b head tail current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

Inserting into a Doubly Linked List c b head tail current newNode = new DoublyLinkedListNode() newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode

Deleting an element from a double linked list c b head current oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

Deleting an element from a double linked list c b head current oldNode oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

Deleting an element from a double linked list c b head current oldNode oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

Deleting an element from a double linked list c b head current oldNode oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

Deleting an element from a double linked list c b head current oldNode oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

Deleting an element from a double linked list c head current oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

Circular Linked lists a b c d first

Sorted Linked List A sorted link list is one in which items are in sorted order. It can be derived from a list class. code

Common errors (Page 599 ) Splicing in nodes incorrectly when performing insertion Forgetting incomplete class declaration Calling delete at wrong time during remove() More errors on page 599 are given

In class exercises … Question 17.3 from the book. Write an algorithm for printing a singly linked list in reverse. (Don’t use recursion).

In class exercises Question 17.7 from the book. Suppose that you have a pointer to a node in singly linked list that guaranteed not to be the last node in the list. You do not have pointers to any other nodes (except by following links). Describe an O(1) algorithm that logically removes the value stored in such a node from the linked list, maintaining the integrity of the linked list (Hint: Involve the next node)

Programming Homework Implement a linked list On line Due Feb 28th.