Review Learn about linked lists

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Chapter 3 Lists Dr Zeinab Eid.
Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Linked Lists CS-212 Dick Steflik. Linked Lists A sequential collection of information Can be unordered; i.e. in no specific order Can be ordered; may.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
Data Structures Using C++
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
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.
Data Structures: A Pseudocode Approach with C
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Linked Lists
Chapter 4 ADT Sorted List.
Variations of Linked Lists CS 302 – Data Structures Sections 6.2, 6.3 and 6.4.
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.
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.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Doubly Linked Lists CS 308 – Data Structures. Node data info: the user's data next, back: the address of the next and previous node in the list.back.next.info.
Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
Data Structures Using Java1 Chapter 4 Linked Lists.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Data Structures Using C++1 Chapter 5 Linked Lists.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
1 What is a Circular Linked List? l A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first”
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Data Structure & Algorithms
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
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.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
  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.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
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.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Lectures linked lists Chapter 6 of textbook
Review Deleting an Element from a Linked List Deletion involves:
C++ Plus Data Structures
Chapter 4 Linked Lists.
Linked lists.
EEL 4854 IT Data Structures Linked Lists
LINKED LISTS CSCD Linked Lists.
Linked Lists.
Linked lists.
Linked Lists Chapter 5 (continued)
Presentation transcript:

Review Learn about linked lists Become aware of the basic properties of linked lists Explore the insertion and deletion operations on linked lists Discover how to build and manipulate a linked list Learn how to construct a doubly linked list

Doubly linked lists Doubly linked lists Become aware of the basic properties of doubly linked lists Explore the insertion and deletion operations on doubly linked lists Discover how to build and manipulate a doubly linked list Learn about circular linked list

WHY DOUBLY LINKED LIST The only way to find the specific node that precedes p is to start at the beginning of the list. The same problem arias when one wishes to delete an arbitrary node from a singly linked list. If we have a problem in which moving in either direction is often necessary, then it is useful to have doubly linked lists. Each node now has two link data members, One linking in the forward direction One in the backward direction

Introduction A doubly linked list is one in which all nodes are linked together by multiple links which help in accessing both the successor (next) and predecessor (previous) node for any arbitrary node within the list. Every nodes in the doubly linked list has three fields: LeftPointer RightPointer DATA.

Cont….. LPoint will point to the node in the left side (or previous node) that is LPoint will hold the address of the previous node. RPoint will point to the node in the right side (or next node) that is RPoint will hold the address of the next node. Data will hold the information of the node.

REPRESENTATION OF DOUBLY LINKED LIST A node in the doubly linked list can be represented in memory with the following declaration. Struct Node { int Data; struct Node *next; struct Node *prev; };

INSERTION AND DELETION OF NODES

Doubly-Linked Lists It is a way of going both directions in a linked list, forward and reverse. Many applications require a quick access to the predecessor node of some node in list.

Advantages over Singly-linked Lists Quick update operations: such as: insertions, deletions at both ends (head and tail), and also at the middle of the list. A node in a doubly-linked list store two references: A next link; that points to the next node in the list, and A prev link; that points to the previous node in the list.

Doubly Linked List A doubly linked list provides a natural implementation of the List ADT Nodes implement Position and store: element link to the previous node link to the next node Special trailer and header nodes prev next elem node trailer header nodes/positions elements

Sentinel Nodes To simplify programming, two special nodes have been added at both ends of the doubly-linked list. Head and tail are dummy nodes, also called sentinels, do not store any data elements. Head: header sentinel has a null-prev reference (link). Tail: trailer sentinel has a null-next reference (link).

What we see from a Doubly-linked List? A doubly-linked list object would need to store the following: Reference to sentinel head-node; Reference to sentinel tail-node; and Size-counter that keeps track of the number of nodes in the list (excluding the two sentinels).

Empty Doubly-Linked List: Using sentinels, we have no null-links; instead, we have: head.next = tail tail.prev = head Singl Node List: Size = 1 This single node is the first node, and also is the last node: first node is head.next last node is tail.prev header trailer first last header trailer

Removal from a Doubly-Linked List Notice that before removal, we must check for empty list. If not, we will remove the last node in the list, as shown in Figure above.

Insertion p A B C p q A B C X p q A B X C We visualize operation AddAfter(p, X), which returns position q p A B C p q A B C X p q A B X C

Deletion A B C D p A B C p D A B C We visualize remove(p), where p = last() A B C D p A B C p D A B C

Performance In the implementation of the List ADT by means of a doubly linked list The space used by a list with n elements is O(n) The space used by each position of the list is O(1) All the operations of the List ADT run in O(1) time Operation element() of the Position ADT runs in O(1) time

Node data info: the user's data next, back: the address of the next and previous node in the list .back .info .next

Node data (cont.) template<class ItemType> struct NodeType { ItemType info; NodeType<ItemType>* next; NodeType<ItemType>* back; };

Finding a List Item We no longer need to use prevLocation (we can get the predecessor of a node using its back member)

Finding a List Item (cont.)

Inserting into a Doubly Linked List 1. newNode->back = location->back; 3. location->back->next=newNode; 2. newNode->next = location 4. location->back = newNode;

FindItem (listData, item, location, found) RetrieveItem, InsertItem, and DeleteItem all require a search ! Write a general non-member function FindItem that takes item as a parameter and returns location and found. InsertItem and DeleteItem need location (ignore found) RetrieveItem needs found (ignores location)

Deleting from a Doubly Linked List Be careful about the end cases!!

Headers and Trailers Special cases arise when we are dealing with the first or last nodes How can we simplify the implementation?  Idea: make sure that we never insert or delete the ends of the list How? Set up dummy nodes with values outside of the range of possible values

Headers and Trailers (cont.) Header Node: contains a value smaller than any possible list element Trailer Node: contains a value larger than any possible list element

A linked list as an array of records What are the advantages of using linked lists? (1) Dynamic memory allocation (2) Efficient insertion-deletion (for sorted lists) Can we implement a linked list without dynamic memory allocation ?

A linked list as an array of records (cont.)

Basic Operations on a Doubly-Linked List Add a node. Delete a node. Search for a node. Traverse (walk) the list. Useful for counting operations or aggregate operations. Note: the operations on a doubly-linked list are exactly the same that are required for a singly-linked list. As we discussed before, the reasons for using a doubly-linked list are application dependent for the most part.

Adding Nodes to a Doubly-Linked List Adding a Node   There are four steps to add a node to a doubly-linked list: Allocate memory for the new node. Determine the insertion point to be after (pCur). Point the new node to its successor and predecessor. Point the predecessor and successor to the new node. Current node pointer (pCur) can be in one of two states: it can contain the address of a node (i.e. you are adding somewhere after the first node – in the middle or at the end) it can be NULL (i.e. you are adding either to an empty list or at the beginning of the list)

Adding Nodes to an Empty Doubly-Linked List Initial: Code: pNew = (struct node *) /*create node*/ malloc(sizeof(struct dllnode)); pNew -> data = 39; pNew -> right = pHead; pNew -> left = pHead; pHead = pNew; After: pNew 39 pHead pCur pNew 39 pHead pCur

Adding a Node to the Middle of a Doubly-Linked List Before: After: pNew 64 pCur 55 124 pNew 64 55 124 pCur

Adding a Node to the Middle of a Doubly-Linked List The Code pNew = (struct node *) malloc(sizeof(struct dllnode)); pNew -> data = 64; pNew -> left = pCur; pNew -> right = pCur -> right; pCur -> right -> left = pNew; pCur -> right = pNew;  

Adding a Node to the End of a Doubly-Linked List Before: After: pNew 84 55 74 pCur pNew 84 55 74 pCur

Adding a Node to the end of a Doubly-Linked List The Code pNew = (struct node *) malloc(sizeof(struct dllnode)); pNew -> data = 84; pNew -> left = pCur; pNew -> right = pCur -> right; pCur -> right = pNew;

Deleting a Node from a Doubly-Linked List Deleting a node requires that we logically remove the node from the list by changing various links and then physically deleting the node from the list (i.e., return it to the heap). Any node in the list can be deleted. Note that if the only node in the list is to be deleted, an empty list will result. In this case the head pointer will be set to NULL. To logically delete a node: First locate the node itself (pCur). Change the predecessor’s and succesor’s link fields to point each other (see example). Recycle the node using the free() function.

Deleting the First Node from a Doubly-Linked List Before: Code: pHead = pCur -> right; pCur ->right -> left = NULL; free(pCur); After: pHead 75 124 pCur pHead Recycled 124 pCur

Deleting a Node from a Linked List – General Case Before: After: 75 23 46 124 77 pCur 23 Recycled 77 75 124 pCur

Deleting a Node From a Doubly-Linked List The Code //delete a node from a linked list if (pCur -> left == NULL){ //deletion is on the first node of the list pHead = pCur -> right; pCur -> right -> left = NULL; { else { //deleting a node other than the first node of the list pCur -> left -> right = pCur -> right; pCur -> right -> left = pCur -> left; } free(pCur).

Searching a Doubly-Linked List Notice that both the insert and delete operations on a linked list must search the list for either the proper insertion point or to locate the node corresponding to the logical data value that is to be deleted. //search the nodes in a linked list pCur = pHead; //search until the target value is found or the end of the list is reached while (pCur != NULL && pCur -> data != target) { pCur = pCur -> right; } //determine if the target is found or ran off the end of the list if (pCur != NULL) found = 1; else found = 0;

Doubly Linked Lists Doubly linked list: every node has next and back pointers Can be traversed in either direction

Doubly Linked Lists (cont'd.) Operations: Initialize the list Destroy the list Determine whether the list is empty Search the list for a given item Retrieve the first element of the list Retrieve the last element of the list

Doubly Linked Lists (cont'd.) Insert an item in the list Delete an item from the list Find the length of the list Print the list Make a copy of the doubly linked list

Default Constructor

isEmptyList

Destroy the List This operation deletes all the nodes in the list, leaving the list in an empty state

Initialize the List This operation reinitializes the doubly linked list to an empty state

Length of the List

Print the List

Reverse Print the List

Search the List

First and Last Elements

Insert a Node There are four cases: Case 1: Insertion in an empty list Case 2: Insertion at the beginning of a nonempty list Case 3: Insertion at the end of a nonempty list Case 4: Insertion somewhere in nonempty list Cases 1 and 2 require us to change the value of the pointer first Cases 3 and 4 are similar (after inserting an item, count is incremented by 1)

Delete a Node Case 1: The list is empty Case 2: The item to be deleted is in the first node of the list, which would require us to change the value of the pointer first Case 3: Item to be deleted is somewhere in the list Case 4: Item to be deleted is not in the list After deleting a node, count is decremented by 1

Circular Linked list A circular linked list is one, which has no beginning and no end. A singly linked list can be made a circular linked list by simply storing the address of the very first node in the linked field of the last node.

Cont….. A circular doubly linked list has both the successor pointer and predecessor pointer in circular manner.

Review Doubly linked lists Become aware of the basic properties of doubly linked lists Explore the insertion and deletion operations on doubly linked lists Discover how to build and manipulate a doubly linked list Learn about circular linked list