CSCI 3333 Data Structures Linked Lists.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Chapter 17 Linked Lists.
Linked Lists.
Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
DATA STRUCTURES USING C++ Chapter 5
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
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.
1 CSC 211 Data Structures Lecture 10 Dr. Iftikhar Azim Niaz 1.
Starting Out with C++, 3 rd Edition 1 Chapter 17 Linked Lists.
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.
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.
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.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
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.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Unit – I Lists.
Pointers and Linked Lists
Chapter 12 – Data Structures
Linked List :: Basic Concepts
Pointers and Linked Lists
5.13 Recursion Recursive functions Functions that call themselves
CSCI-255 LinkedList.
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Review Deleting an Element from a Linked List Deletion involves:
12 C Data Structures.
Lists CS 3358.
List ADT & Linked Lists.
UNIT-3 LINKED LIST.
Chapter 4 Linked Lists.
Linked lists.
Data Structures Interview / VIVA Questions and Answers
Linked Lists.
Data Structures and Algorithms IT12112
Notes on Assignment 1 Your code will have several classes, most notably the class that represents the entire list data structure, and the class.
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Linked Lists
Topic 11 Linked Lists -Joel Spolsky
Arrays and Linked Lists
Chapter 18: Linked Lists.
List Data Structure.
Linked Lists.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Linked List.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Introduction to C++ Linear Linked Lists
Lecture 14 Linked Lists CSE /26/2018.
Data Structures & Algorithms
Data Structures & Algorithms
Linked lists.
Linked Lists.
Topic 11 Linked Lists -Joel Spolsky
Sequences 08/30/17 08/30/17 Unit III. Linked Lists 1.
Presentation transcript:

CSCI 3333 Data Structures Linked Lists

Acknowledgement Ms. Krishani Abeysekera Dr. Bun Yue Mr. Charles Moen Dr. Wei Ding Dr. Michael Goodrich

Linked List ADT A linked list is a series of connected nodes (or links) where each node is a data structure. Dynamically allocated data structures can be linked together to form a chain. 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.

Similarities and Differences Between Arrays and Linked Lists Array is one block of contiguous memory List is a collection of scattered memory cells Similarities Both store data in memory Both store data in a sequence Array Linked List

Advantages of Linked Lists over Arrays Linked lists are more complex to code and manage than arrays, but they have some distinct advantages. A linked list can easily grow and shrink in size - The programmer doesn’t need to know how many nodes will be in the list. They are created in memory as needed. Speed of insertion or deletion from the list - Inserting and deleting elements into and out of arrays requires moving elements of the array. When a node is inserted, or deleted from a linked list, none of the other nodes have to be moved.

Some Disadvantages Linked List may not be randomly accessible. Programmers needs to manage memory. Automatic garbage collection may bring performance issues.

Composition of a 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

Composition of a 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. a) The list 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). NULL struct Node { char element; Node *next; }; *next; pHead pTail

Composition of a Linked List The pointers play a big role in maintaining the linked list. The nodes in a linked list can be spread out over memory. Therefore, it is not possible to calculate the address of the next node, like it is possible to calculate the next element of an array. If a pointer from one node to another is lost, the list from that point is lost forever. Therefore, extreme care should be taken when assigning or re-assigning a pointer in a linked list.

Composition of a Linked List B pHead C If the pointer between any nodes (say B and C) is re-assigned erroneously to null or anything else, the access to the rest of the nodes ( C, D and E ) is then lost. If you loose the head pointer, you loose the entire list.

Creating a Linked List Just like any other data type, the information about the node has to be first be declared. Step 1) Declare a data structure for the nodes. struct Node { Object element; Node *next; };

Creating a Linked List a) In this example, the first member of the Node struct is a object called element. It holds the node’s data. This could just as well be just an integer x, or a structure of student records. b) The second member is a pointer called next. It holds the address of any object that is a structure of type Node. Hence each Node struct can point to the next node in the list. The Node 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.

Creating a Linked List Next, since there is no physical relationship between nodes, a pointer needs to be created to point to the first logical node in the list. Step 2) Declare a pointer to serve as the head of the list: Node *head = NULL; 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. null head

Declaring a Linked List in a Class class LinkedList{ public: struct Node { Object element; Node *next; Node (Object e, Node* n = NULL ) { element = e, next = n } }; … private: Node* pHead;

Linked List Operations There are 5 basic linked list operations: Appending a node (to the head or to the tail) Traversing a list Inserting a node (into a sorted list) Deleting a node (from the head, tail or middle) Destroying a list

Appending a Node When appending a node, there are several things that need to be taken into consideration. For instance: Memory has to be allocated for a new node, and the data stored in the memory The insertion point has to be determined – this could be to the head of the list, to the tail of the list or somewhere in the middle Once the insertion point is determined, the new node’s logical predecessor has to be identified Then, the new node has to point to its successor Finally, the predecessor has to point to the new node

Appending a Node F D A E B C newNode F D A B E pHead C InsertAfterThis Once the logical predecessor of the new node (InsertAfterThis) is identified, if it is equal to NULL, this signifies that the list is empty or that the insertion is always done to the head of the list. In this instance, the code for inserting the node is similar to: newNode->next = pHead; pHead = newNode;

Appending a Node F D A E B C newNode F D A B E pHead C InsertAfterThis InsertAfterThis However, if the logical predecessor of the new node (InsertAfterThis) is identified and it points to a node, this could mean that the node is appended to the middle of the list or to the end. Regardless of where the insertion of the new node is going to be, the code for inserting the node is similar to: newNode->next = InsertAfterThis->next; InsertAfterThis->next = newNode;

Appending to the Tail When appending always to the end of the list, here is one algorithm that could be used. a) Create a new node. b) Store data in the new node. c) if there are no nodes in the list Make the new node the first node. else Traverse the List to find the last node. Add the new node to the end of the list. endif.

Add a Node to an Empty List NULL h next pHead New Node

Appending a Node to the end of a List pHead e next NULL f next g next h next New Node

Appending a Node to the Tail void LinkedList::appendNode(object e) { Node *newNode, *Walker;   newNode = new Node; // create a new node newNode->element = e; newNode->next = NULL; if ( pHead==NULL) // if the list is empty add to head pHead = newNode; else { Walker = pHead; // Initialize Walker to head of list while (Walker->next != NULL) // Find the last node in the list Walker = Walker->next;  Walker->next = newNode; // Insert newNode as the last node } }

Appending a Node to the Tail In the previous algorithm, the list had to be traversed all the way to the end to find the last node in the list. One way to make this simpler would be to have a pointer that always pointed to the last node. In this method, traversing the list would then not be required.

Appending a Node to the Tail without Traversing the List void LinkedList::appendNode(object e) { Node *newNode;   newNode = new Node; // create a new node newNode->element = e; newNode->next = NULL; if ( pHead==NULL) // if the list is empty add to head { pHead = newNode; pTail = newNode; } else // Insert newNode as the last node { pTail->next = newNode; pTail = newNode; } }

Appending a Node to the Head When appending always to the head of the list, here is one algorithm that could be used. a) Create a new node. b) Store data in the new node. c) if the list is empty Make the new node the head of the list. else Add the new node to the top of the list Make the new node the head of the list endif.

Appending a Node to the Head pHead f next g next h next NULL e NEXT newNode

Appending a Node to the Head void LinkedList::appendNode(object e) { Node *newNode;   newNode = new Node; // create a new node newNode->element = e; newNode->next = NULL; if ( pHead==NULL) // appending to an empty list { pHead = newNode; } else // appending to an existing list { newNode->next = pHead; pHead = newNode; } }

Appending a Node to the Head In the previous algorithm, when appending a node to an existing list, the order in which the statements are written is extremely important. What would happen if the statement were written in reverse order, in this manner? pHead = newNode; newNode->next = pHead;

Appending a Node to the Head This would result with the new node pointing to itself, which would make the program go in a never-ending loop when accessing the list. This would also result in loosing access to all the nodes that were currently on the linked list. pHead NULL f next g next h next e NEXT newNode

Traversing a Linked List When traversing a list, it is important to remember NOT to move the head pointer from node to node. This would result in loosing access to nodes in the list. Instead, always assign another pointer to the head of the list, and use that pointer to traverse it. Assign list head to walking pointer While walking pointer is not NULL Display the info pointed to by walking pointer Assign walking pointer to its own next member end While

Traversing a Linked List void LinkedList::displayList() { Node *Walker; Walker = pHead; while(Walker != NULL) printInfo(Walker->element) ; Walker = Walker->next; }

Inserting a Node into an Ordered List Create a new node. Store data in the new node. if there are no nodes in the list Make the new node the first node. else Find the first node whose value is greater than or equal the new value, or the end of the list (whichever is first). Insert the new node before the found node, or at end of the list if no node was found. endif

Inserting a Node 7 next 2 next pHead 12 null 9 null newNode

Inserting a Node 7 next 2 next 12 null 9 null nodePtr pHead previousNode 9 null newNode

Inserting a Node 7 next 2 next 12 null 9 null previousNode pHead nodePtr 9 null newNode

Inserting a Node Once the position in which to insert is found, the node needs to be inserted. The order in which the statements are written is important when inserting the node. newNode->next = nodePtr; previousNode->next = newNode; OR newNode->next = previousNode->next;

Inserting a Node The segment of code from the previous slide will result in the following list: previousNode 7 next 2 next pHead 12 null nodePtr 9 next newNode

Inserting a Node However, if the order in which the statements were written were reversed in this manner: previousNode->next = newNode; newNode->next = previousNode->next; This would result with the new node pointing to itself, which would make the program go in a never-ending loop when accessing the list.

Inserting a Node The segment of code from the previous slide will result in the following list, where the nodes after the new node are lost, and the list becomes a never ending loop, when traversed. previousNode 7 next 2 next pHead 12 null nodePtr 9 next newNode

Inserting a Node into an Ordered List void LinkedList::insertNode(Object e) { Node *newNode, *nodePtr, *previousNode; newNode = new Node; // Allocate a new node & store the new object newNode->element = e; newNode->next = NULL; if (pHead==NULL) // empty list – add to head pHead = newNode; else Walker = pHead; // assign Walker to head and use Walker to find where to insert while ( Walker != NULL && Walker->element.value < newNode->element.value ) { previousNode = Walker; Walker = Walker->next; } if (previousNode == NULL) // new node has the smallest value, insert at head { head = newNode; newNode->next = Walker; } else { previousNode->next = newNode; newNode->next = Walker; } }

Deleting a Node Deleting a node is similar to adding a node. The node to be deleted needs to be identified, along with its predecessor. Then, a) Remove the node from the list without breaking the links created by the next pointers. Delete the node from memory. It is important to remember to delete the node so that resources are released.

Deleting a Node Assume that we need to delete node that contains the value 7. nodePtr 7 next 2 next pHead 12 null previousNode The bypassed node is destroyed with the statement delete nodePtr;

Deleting a Node void LinkedList::deleteNode(int num) { ListNode *Walker, *previousNode; if (!pHead) // If the list is empty, do nothing. return; if (pHead->element.value == num) // If it is the first node in the list (special case) { Walker = pHead->next; // set the new head to walker delete pHead; // delete node pHead = Walker; // reassign new head } else { Walker = pHead; // Initialize Walker to head of list while (Walker != NULL && Walker->element.value != num) // skip all nodes whose value is not num { previousNode = Walker; Walker = Walker->next; } previousNode->next = Walker->next; // Link the previous node to the node after Walker delete Walker; // delete Walker } }