Data Structures Doubly and Circular Lists Lecture 07: Linked Lists

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked Lists Geletaw S..
Lists CS 3358.
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
Data Structures Using C++
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
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.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
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.
Summary of lectures (1 to 11)
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
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.
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.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Linked List Chapter Data Abstraction separates the logical properties of a data type from its implementation LOGICAL PROPERTIES – What are the.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Linked List by Chapter 5 Linked List by
Subject Name : Data Structure Using C Title : Linked Lists
Data Structures Using C++1 Chapter 5 Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
COMPSCI 105 SS 2015 Principles of Computer Science
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
Data Structure & Algorithms
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.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
LINKED LISTS.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Linked List, Stacks Queues
Chapter 16: Linked Lists.
Linked List ADT used to store information in a list
Lecture 6 of Computer Science II
Unit – I Lists.
C++ Programming:. Program Design Including
Review Deleting an Element from a Linked List Deletion involves:
Lecture - 6 On Data Structures
Chapter 4 Linked Lists.
LINKED LISTS CSCD Linked Lists.
Arrays and Linked Lists
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.
Linked Lists Chapter 5 (continued)
Presentation transcript:

Data Structures Doubly and Circular Lists Lecture 07: Linked Lists Azhar Maqsood NUST Institute of Information Technology (NIIT)

Problems with Linked Lists -3 4 -1 NULL header A node in the list NULL (An empty list) header Two problems – we can’t get back to the beginning of the list from the end, and we can’t go backwards through the list. So, circular linked lists and doubly linked lists were invented.

Other list flavors Doubly-linked list Circular list Sorted list Each node has a pointer to its successor and its predecessor. Faster insert/delete, but more space. Circular list The last node points back to the head. Sorted list Items stored in sorted order.

Doubly Linked List

Doubly-Linked Lists A common variation on linked lists is to have two references to other nodes within each node: one to the next node on the list, and one to the previous node Doubly-linked lists make some operations, such as deleting a tail node, more efficient

Doubly Linked List A doubly linked list is a linked list in which every node has a next pointer and a back pointer Every node (except the last node) contains the address of the next node, and every node (except the first node) contains the address of the previous node. A doubly linked list can be traversed in either direction

Doubly-Linked Lists Other operations, such as adding an item to an ordered linked list, are easier to program with doubly-linked lists Tradeoffs: The NodeType class is more complex: extra instance variable, extra methods, revised constructors Each node requires 4 additional bytes

Doubly-linked Lists - properties Allow sequential access to the list in both directions Each element points to Next element Previous element previous value next

Doubly-Linked List head size tail 3 . .

Example - Doubly-linked Lists Non-empty doubly linked list size head tail 3 null Bill Jill null Karl p n data Empty doubly linked list null head tail size

Advantages The doubly linked list eliminates the need for the “pPrevious” pointer since each node has pointers to both the previous and next nodes in the list. You can go to the previous node easily Traversal is in both directions. Implementations: ALT+TAB and ALT+SHIFT+TAB (Window Browsing) Picture Viewers, Power Point Presentations

Doubly-linked lists add a prev pointer to our Node class allows backward iteration some methods need to be modified when adding or removing a node, we must fix the prev and next pointers to have the correct value! can make it easier to implement some methods such as remove

Linked list add operation When adding a node to the list at a given index, the following steps must be taken: Advance through the list to the node just before the one with the proper index. Create a new node, and attach it to the nodes that should precede and follow it. How many 'arrows' (references) will need to be changed?

Linked list remove operation When removing a node from the list at a given index, the following steps must be taken: Advance through the list to the node with the proper index. Detach it from the nodes that used to precede and follow it. How many 'arrows' (references) will need to be changed?

addFirst Method Before: After: 4 count head tail 3 null Bill Jill null Karl Amma New value count head tail After: 4 Bill Jill null Karl null Amma

addLast method Before: New value count head tail 3 Laura null Bill Jill null Karl After: count head tail Amma null 3 Bill Jill Karl

remove method Case 1: remove a middle element Before: After: size head tail 3 null Bill Jill null Karl to be removed size head tail 2 null Bill null Karl Jill

remove method Case 2: remove head element Before: After: 3 head tail count Bill null Jill Karl to be removed 2 head tail count null Karl Jill Bill

remove method Case 3: remove the only element Before: After null Bill null Bill to be removed null null Bill

To Remove Last Item From a Doubly-Linked List Note: We no longer need to traverse the list. Save a reference to the last data object so that it can be returned later head size tail 4 . .

To Remove Last Item From a Doubly-Linked List Reset tail so that it points at the node prior to the original tail node head size tail 4 . .

To Remove Last Item From a Doubly-Linked List Get rid of the successor node of the new tail node head size tail 4 . .

To Remove Last Item From a Doubly-Linked List Set the successor of the new tail node to NULL, decrement the size of the list, and return the reference to the deleted data object head size tail 3 . . .

Circular Lists

Circular Linked List A linked list in which the last node points to the first node is called a circular linked list In a circular linked list with more than one node, it is convenient to make the pointer first point to the last node of the list

A Circular Linked List 2 -3 4 -1 header Have the last node link back to the first (or the header).

Circular Linked Lists Circular linked lists avoid the use of null references in their nodes Can be useful for certain algorithms Can also make programming simpler: fewer special cases to consider Successor of tail node is the head node; in doubly- linked list

Circular linked lists Insertions and deletions into a circular linked list follow the same logic patterns used in a singly linked list except that the last node points to the first node. Therefore, when inserting or deleting the last node, in addition to updating the tail pointer, we must also point the link field of the new last node to the first node.

Circular linked list Advantage is that we can start searching from any node of the linked list and get to any other node. No logical head and tail pointers. We follow the conventions as first element inserted into the list is given status of head and last element entered is called as a tail.

Circular Linked Lists Last node references the first node Every node has a successor No node in a circular linked list contains NULL Figure 4.25 A circular linked list

Circular Doubly-Linked List head size tail 3

Assignment 3 Implement Doubly Linked List in your project. Please Refer to the Assignment folder for details. Due April 15th Before Class.