1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Ping Zhang 2010/09/29. 2 Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link.
Linked Lists.
Linked Lists.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Circular Linked List. COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Data Structures: A Pseudocode Approach with C
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor.
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Linked Lists
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
Linked Lists. 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)
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. 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)
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.
Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Motivation * Doubly linked lists are useful for playing video and sound files with “rewind”
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
1 CSC 211 Data Structures Lecture 10 Dr. Iftikhar Azim Niaz 1.
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 17 Linked List.
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.
Data Structures and Algorithms Lecture 7,8 and 9 (Linked List) Instructor: Quratulain Date: 25, 29 September and 2 October, 2009 Faculty of Computer Science,
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Data Structures Using Java1 Chapter 4 Linked Lists.
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)
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. 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.
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. Array List Issues Painful insert/remove at start/middle.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
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.
Data Structure & Algorithms
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
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.
Programming Circular Linked List.
Linked Lists.
Anatomy of a linked list
Lectures linked lists Chapter 6 of textbook
Chapter 4 Linked Lists.
CSCI 3333 Data Structures Linked Lists.
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists.
Arrays and Linked Lists
Linked Lists.
Linked Lists.
Data Structures & Algorithms
Linked Lists.
Presentation transcript:

1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1

2 Last Lecture Summary Comparison of Merge Sort and Quick Sort Shell Sort  Concept, Examples, Algorithm, Complexity Radix Sort  Concept, Examples, Algorithm, Complexity Bucket Sort  Concept, Examples, Algorithm, Complexity Comparison of Sorting Techniques 2

3 Objectives Overview Doubly Linked List Concept Operations on Doubly Linked List  Insertion  Deletion  Traversing  Search Implementation Code Doubly Linked List with Two Pointers

4 Data Structure Operations Following are the major operations: Traversing: Accessing each record exactly once so that certain items in the record may be processed. (This accessing and processing is sometimes called "visiting" the record.) Searching: Finding the location of the record with a given key value, or finding the locations of all records that satisfy one or more conditions Inserting: Adding a new record to the structure Deleting: Removing a record from the structure

5 Data Structure Operations (Cont…) Sometimes two or more of the operations may be used in a given situation;  e.g., we may want to delete the record with a given key, which may mean we first need to search for the location of the record. Following two operations, which are used in special situations, are also be considered: Sorting: Arranging the records in some logical order  (e.g., alphabetically according to some NAME key, or in numerical order according to some NUMBER key, such as social security number or account number) Merging: Combining the records in two different sorted files into a single sorted file Other operations, e.g., copying and concatenation, are also used

6 List Using Linked Memory Various cells of memory are not allocated consecutively in memory. Not enough to store the elements of the list. With arrays, the second element was right next to the first element. Now the first element must explicitly tell us where to look for the second element. Do this by holding the memory address of the second element

7 Linked List Pointer Based Implementation of Linked List ADT Dynamically allocated data structures can be linked together to form a chain. A linked list is a series of connected nodes (or links) where each node is a data structure. A linked list can grow or shrink in size as the program runs. This is possible because the nodes in a linked list are dynamically allocated

8 Composition of Linked List Each node in the linked list contains –  a) One or more members that represent data (e.g. inventory records, customer names, addresses, telephone numbers, etc).  b) A pointer, that can point to another node. Data Members Pointer

9 Composition of linked List A linked list is called “linked” because each node in the series (i.e. the chain) has a pointer to the next node in the list, e.g. Head NULL a) The head is a pointer to the first node in the list. b) Each node in the list points to the next node in the list. c) The last node points to NULL (the usual way to signify the end). Note, the nodes in a linked list can be spread out over the memory.

10 Linked List  Actual picture in memory: head current head current 1065

11 List Declarations How to declare a linked list in C or C++? Step 1) Declare a data structure for the nodes. e.g. the following struct could be used to create a list where each node holds a float - struct ListNode { int data; ListNode *next; };

12 List Declarations a) The first member of the ListNode struct is a int called data. It is to hold the node’s data. b) The second member is a pointer called next. It is to hold the address of any object that is a ListNode struct. Hence each ListNode struct can point to the next one in the list. The ListNode struct contains a pointer to an object of the same type as that being declared. It is called a self-referential data structure. This makes it possible to create nodes that point to other nodes of the same type.

13 List Declarations Step 2) Declare a pointer to serve as the list head, e.g List *head; Before you use the head pointer, make sure it is initialized to NULL, so that it marks the end of the list. Once you have done these 2 steps (i.e. declared a node data structure, and created a NULL head pointer, you have an empty linked list. struct ListNode { int data; ListNode *next; }; ListNode *head;// List head pointer

14 Singly Linked List (SLL) - More Terminology A node’s successor is the next node in the sequence  The last node has no successor A node’s predecessor is the previous node in the sequence  The first node has no predecessor A list’s length is the number of elements in it  A list may be empty (contain no elements) Head

15 Traversing a SLL (animation) Head nodePtr

16 Inserting after (animation) Head 78 nodePtr Find the node you want to insert after First, copy the link from the node that's already in the list Then, change the link in the node that's already in the list

17 Deleting a node from a SLL In order to delete a node from a SLL, you have to change the link in its predecessor This is slightly tricky, because you can’t follow a pointer backwards Deleting the first node in a list is a special case, because the node’s predecessor is the list header

18 Deleting an element from a SLL Head Head To delete the first element, change the link in the header To delete some other element, change the link in its predecessor (predecessor)

19 Doubly Linked List (DLL) In a singly linked list (SLL) one can move beginning from the head node to any node in one direction only (from left to right) SLL is also termed as one –way list On the other hand, Doubly Linked List (DLL) is a two-way list  One can move in either direction from left to right and from right to left This is accomplished by maintaining two linked fields instead of one as in a SLL

20 Motivation Doubly linked lists are useful for playing video and sound files with “rewind” and “instant replay” They are also useful for other linked data which require “rewind” and “fast forward” of the data

21 Each node on a list has two pointers.   A pointer to the next element.   A pointer to the previous element   The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list … … … … Doubly Linked List

22 Doubly-Linked Lists (DLL) Here is a doubly-linked list (DLL): Each node contains a value, a link to its successor (if any), and a link to its predecessor (if any) The header points to the first node in the list and to the last node in the list (or contains null links if the list is empty) Head

23 Doubly Linked Lists In a Doubly Linked List each item points to both its predecessor and successor  prev points to the predecessor  next points to the successor Head Cur Cur->nextCur->prev

24 DLLs compared to SLLs Advantages:  Can be traversed in either direction (may be essential for some programs)  Some operations, such as deletion and inserting before a node, become easier Disadvantages:  Requires more space to store backward pointer  List manipulations are slower because more links must be changed  Greater chance of having bugs because more links must be manipulated

25 Double Linked List – Definition in C struct Node{ int data; Node* next; Node* prev; } *Head, *nodePtr;

26 Operations on DLL The two node links allow traversal of the list in either direction While adding or removing a node in a doubly linked list requires changing more links than the same operations on a singly linked list The operations are simpler and potentially more efficient (for nodes other than first nodes)  because there is no need to keep track of the previous node during traversal or  no need to traverse the list to find the previous node, so that its link can be modified

27 Doubly Linked List Operations * * insertNode(Node *Head, int item) //add new node to ordered doubly linked list * * deleteNode(Node *Head, int item) //remove a node from doubly linked list * * searchNode(Node *Head, int item) * * print(Node *Head, int item)

28 DLL - Insertion

29 Inserting a Node Insert a node NewNode before Cur (not at front or rear) Head NewNode Cur NewNode->next = Cur; NewNode->prev = Cur->prev; Cur->prev = NewNode; (NewNode->prev)->next = Newnode;

30 DELETE: Pointer change for implementation of a Deletion Doubly – Linked List

31 Deleting a node from a DLL Node deletion from a DLL involves changing two links In this example, we will delete node 67 We don’t have to do anything about the links in node 67 Deletion of the first node or the last node is a special case Head

32 Deleting a Node Delete a node Cur (not at front or rear) (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur; Head Cur

33 Other operations on linked lists Most “algorithms” on linked lists  such as insertion, deletion, and searching—are pretty obvious;  you just need to be careful Sorting a linked list is just messy,  since you can’t directly access the n th element  you have to count your way through a lot of other elements

34 DLL with Dummy Head Node To simplify insertion and deletion by avoiding special cases of deletion and insertion at front and rear, a dummy head node is added at the head of the list The last node also points to the dummy head node as its successor

35 Doubly Linked Lists with Dummy Head  Non-Empty List  Empty List Head 10 Dummy Head Node Head Dummy Head Node

36 DLL– Creating Dummy Node at Head void createHead(Node *Head){ Head = new Node; Head->next = Head; Head->prev = Head; } Head Dummy Head Node

37 Insert a Node New to Empty List (with Cur pointing to dummy head node) Head Dummy Head Node New 20 New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; Cur Inserting a Node as First Node

38 Inserting a Node at Head Insert a Node New after dummy node and before Cur Head Dummy Head Node Cur 20 New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; 10 New

39 Insert a Node New in the middle and before Cur New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; // same as insert front! 55 Head 10 Dummy Head Node 20 Cur 40 New Inserting a Node – in the Middle

40 Insert a Node New at Rear (with Cur pointing to dummy head) New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; // same as insert front! Head 10 Dummy Head Node Cur New Inserting a Node at Rear

41 DLL-Insert – Implementation Code void insertNode(Node *Head, int item){ Node *New, *Cur; New = new Node; New->data = item; Cur = Head->next; while(Cur != Head){//position Cur for insertion if(Cur->data < item) Cur = Cur->next; else break; } New->next = Cur; New->prev = Cur->prev; Cur->prev = New; (New->prev)->next = New; }

42 Deleting a Node – at Head Delete a node Cur at front Head 10 Dummy Head Node Cur (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur;

43 Delete a node Cur in the middle void deleteNode(Node *Head, int item){ Node *Cur; Cur = searchNode(Head, item); if(Cur != NULL){ Cur->prev->next = Cur->next; Cur->next->prev = Cur->prev; delete Cur; } // end of if } // end of function 70 Head 10 Dummy Head Node Cur Deleting a Node – in the Middle

44 Delete a node Cur at rear (Cur->prev)->next = Cur->next; (Cur->next)->prev = Cur->prev; delete Cur;// same as delete front and middle! Head 10 Dummy Head Node Cur Deleting a Node – at end

45 DLL – Searching a Node Node* searchNode(Node *Head, int item){ Node *Cur = Head->next; while(Cur != Head){ if(Cur->data == item) return Cur; if(Cur->data < item) Cur = Cur->next; else break; } return NULL; }

46 DLL – Printing the Whole List void print(Node *Head){ Node *Cur=Head->next; while(Cur != Head){ cout data << " "; Cur = Cur->next; } cout << endl; }

47 Main Program void main(){ Node *Head, *temp; createHead(Head); print(Head); insertNode(Head, 3); print(Head); insertNode(Head, 5); print(Head); insertNode(Head, 2); print(Head); insertNode(Head, 7); insertNode(Head, 1); insertNode(Head, 8); print(Head); deleteNode(Head, 7); deleteNode(Head, 0); print(Head); temp = searchNode(Head, 5); if(temp != NULL) cout << "Data is contained in the list" << endl; else cout << "Data is NOT contained in the list" << endl; } Result is: Data is contained in the list

48 Doubly Linked List – Worst Case insertion at head or tail is in O(1) deletion at either end is on O(1) element access is still in O(n) 48

49 c head tail head = new Node (); tail = new Node (); head->next = tail; tail->prev = head; Doubly Linked List with Two Pointers With two Pointers One for Head Another for Rear

50 newNode = new Node; newNode->prev = current; newNode->next = current->next; newNode->prev->next = newNode; newNode->next->prev = newNode; current = newNode Inserting into a Doubly Linked List ac head tail current

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

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

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

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

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

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

57 Deleting from a Doubly Linked List oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode; ac head b current tail

58 Deleting from a Doubly Linked List ac head b current oldNode tail oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

59 Deleting from a Doubly Linked List ac head b current oldNode tail oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode;

60 ac head b current oldNode tail oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode; Deleting from a Doubly Linked List

61 ac head b current oldNode tail oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode; Deleting from a Doubly Linked List

62 ac head current tail oldNode=current; oldNode->prev->next = oldNode->next; oldNode->next->prev = oldNode->prev; current = oldNode->prev; delete oldNode; Deleting from a Doubly Linked List

63 abcd head Circular Linked lists Insertion and Deletion implementation left as an exercise

64 Summary Doubly Linked List Concept Operations on Doubly Linked List  Insertion  Deletion  Traversing  Search Implementation Code Doubly Linked List with Two Pointers  Insertion and Deletion