Chapter 4 Linked Lists https://visualgo.net/en/list.

Slides:



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

Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Linked Lists Chapter 4.
DATA STRUCTURES USING C++ Chapter 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 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.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
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 Object-Oriented Design Inheritance and Class Examples.
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 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Chapter 5 Linked Lists II
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 4 Linked.
10/07/2011ecs40 fall Software Development & Object- Oriented Programming ecs40 Fall 2012: Software Development & Object- Oriented Programming #05:
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Link-Based Implementations
Chapter 16: Linked Lists.
Chapter 7 Queues.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
CC 215 Data Structures Queue ADT
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Lists CS 3358.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Data Abstraction: The Walls
CC 215 Data Structures Stack ADT
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Chapter 4 Linked Lists.
The Bag and Sequence Classes with Linked Lists
Array Lists Chapter 6 Section 6.1 to 6.3
Linked Lists Chapter 4.
Queues.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Linked Lists.
Chapter 4 Link Based Implementations
Chapter 4 Linked Lists.
List Implementations Chapter 9.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Doubly Linked List Implementation
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Data Abstraction: The Walls
Chapter 16 Linked Structures
Linked Lists The items in a linked list data structure are linked to one another An item in a linked list references its successor A linked list is able.
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
Linked Lists.
Linked Lists Chapter 5 (continued)
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Data Abstraction: The Walls
Doubly Linked List Implementation
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

Chapter 4 Linked Lists https://visualgo.net/en/list

Preliminaries Figure 4.1 a) A linked list of integers; b) insertion; c) deletion

Preliminaries Options for implementing an Abstract Data Type List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to grow in size as needed Does not require the shifting of items during insertions and deletions

Pointer-Based Linked Lists A node in a linked list is usually a struct struct Node{ int item; Node *next; }; A node is dynamically allocated Node *p; p = new Node; Figure 4.6 A node

Pointer-Based Linked Lists The head pointer points to the first node in a linked list If head is NULL, the linked list is empty (write a function)

Pointer-Based Linked Lists Figure 4.7 A head pointer to a list Figure 4.8 A lost cell

HERE..

Displaying the Contents of a Linked List Reference a node member with the -> operator p->item; A traverse operation visits each node in the linked list A pointer variable cur keeps track of the current node for (Node *cur = head; cur != NULL; cur = cur->next) cout << cur->item << endl;

Displaying the Contents of a Linked List The effect of the assignment cur = cur->next

Deleting a Specified Node from a Linked List Deleting an interior node prev->next=cur->next; Deleting the first node head=head->next; Return deleted node to system cur->next = NULL; delete cur; cur=NULL;

Deleting a Specified Node from a Linked List Deleting a node from a linked list Deleting the first node

Inserting a Node into a Specified Position of a Linked List To insert a node between two nodes newPtr->next = cur; prev->next = newPtr;

Inserting a Node into a Specified Position of a Linked List To insert a node at the beginning of a linked list newPtr->next = head; head = newPtr;

Inserting a Node into a Specified Position of a Linked List Inserting at the end of a linked list is not a special case if cur is NULL newPtr->next = cur; prev->next = newPtr;

Inserting a Node into a Specified Position of a Linked List Determining the point of insertion or deletion for a sorted linked list of objects for( prev = NULL, cur= head; (cur != null)&&(newValue > cur->item); prev = cur, cur = cur->next);

A Pointer-Based Implementation of the ADT List Public methods isEmpty getLength insert remove retrieve Private method find Private Data Members head size Local variables to member functions cur prev

Constructors and Destructors Default constructor initializes size and head Copy constructor allows a deep copy Copies the array of list items and the number of items A destructor is required for dynamically allocated memory

A Pointer-Based Implementation of the ADT List // **************************************** // Header file ListP.h for the ADT list. // Pointer-based implementation. //***************************************** typedef desired-type-of-list-item ListItemType; class List{ // constructors and destructor: public: List(); // default constructor List(const List& aList); // copy constructor ~List(); // destructor

A Pointer-Based Implementation of the ADT List // list operations: bool isEmpty() const; int getLength() const; bool insert(int index, ListItemType newItem); bool remove(int index); bool retrieve(int index,ListItemType& dataItem)const;

A Pointer-Based Implementation of the ADT List private: struct ListNode{ // a node on the list ListItemType item; // a data item on the list ListNode *next; // pointer to next node }; int size; // number of items in list ListNode *head; // pointer to linked list // of items ListNode *find(int index) const; // Returns a pointer to the index-th node // in the linked list.

A Pointer-Based Implementation of the ADT List // ********************************************** // Implementation file ListP.cpp for the ADT list // Pointer-based implementation. #include "ListP.h" // header file #include <cstddef> // for NULL List(): size(0), head(NULL){ }

copy constructor List::List(const List& aList): size(aList.size){ if (aList.head == NULL) head = NULL; // original list is empty else { // copy first node head = new ListNode; head->item = aList.head->item; // copy rest of list ListNode *newPtr = head; // new list ptr for (ListNode *origPtr = aList.head->next; origPtr != NULL; origPtr = origPtr->next) { newPtr->next = new ListNode; newPtr = newPtr->next; newPtr->item = origPtr->item; } newPtr->next = NULL; } // end copy constructor

A Pointer-Based Implementation of the ADT List while (!isEmpty()) remove(1); } // end destructor

A Pointer-Based Implementation of the ADT List bool isEmpty() const{ return size == 0; } // end isEmpty

A Pointer-Based Implementation of the ADT List int getLength() const{ return size; } // end getLength

A Pointer-Based Implementation of the ADT List ListNode * find(int index) const{ // Locates a specified node in a linked list. // Precondition: index is the number of the // desired node. // Postcondition: Returns a pointer to the // desired node. If index < 1 or index > the // number of nodes in the list, returns NULL. if ( (index < 1) || (index > getLength()) ) return NULL; else{ // count from the beginning of the list ListNode *cur = head; for (int i = 1; i < index; ++i) cur = cur->next; return cur; } } // end find

A Pointer-Based Implementation of the ADT List bool retrieve(int index,ListItemType& dataItem) { if ((index < 1) || (index > getLength())) return false; // get pointer to node, then data in node ListNode *cur = find(index); dataItem = cur->item; return true; } // end retrieve

A Pointer-Based Implementation of the ADT List bool insert(int index, ListItemType newItem) { int newLength = getLength() + 1; if ((index < 1) || (index > newLength)) return false; ListNode *newPtr = new ListNode; size = newLength; newPtr->item = newItem; if (index == 1){ newPtr->next = head; head = newPtr; } else{ ListNode *prev = find(index-1); newPtr->next = prev->next; prev->next = newPtr; return true; } // end insert

A Pointer-Based Implementation of the ADT List bool List::remove(int index) { ListNode *cur; if ((index < 1) || (index > getLength())) return false; --size; if (index == 1){ cur = head; head = head->next; } else{ ListNode *prev = find(index-1); cur = prev->next; prev->next = cur->next; cur->next = NULL; delete cur; cur = NULL; return true; } // end remove

Passing a Linked List to a Function A function with access to a linked list’s head pointer has access to the entire list

Processing Linked Lists Recursively Display the contents of a linked list in reverse order void displayReverseOrder(Node *headPtr){ if (headPtr != NULL){ // display the linked list minus // its first item in reverse order displayReverseOrder(headPtr->next); // display the first item cout << headPtr->item; } What is the time complexity of this function?

Processing Linked Lists Recursively Determine whether or not a linked list is sorted The linked list to which head points is sorted if head is NULL or head->next is NULL or head->item < head->next->item, and head->next points to a sorted linked list

Processing Linked Lists Recursively Insert an item into a sorted linked list recursively void linkedListInsert(Node *&headPtr, ListItemType newItem){ if ((headPtr == NULL) || (newItem < headPtr->item)){ Node *newPtr = new Node; newPtr->item = newItem; newPtr->next = headPtr; headPtr = newPtr; } else linkedListInsert(headPtr->next,newItem);

Comparing Array-Based Pointer-Based Implementations and Pointer-Based Implementations

Comparing Array-Based and Pointer-Based Implementations Size Increasing the size of a resizable array can waste storage and time Storage requirements Array-based implementations require less memory than a pointer-based ones

Comparing Array-Based and Pointer-Based Implementations Access time Array-based: constant access time Pointer-based: the time to access the ith node depends on i Insertion and deletions Array-based: require shifting of data Pointer-based: require a list traversal

Saving and Restoring a Linked List by Using a File Do not write pointers to a file, only write data Recreate the list from the file by placing each item at the end of the list Use a tail pointer to facilitate adding nodes to the end of the list Treat the first insertion as a special case by setting the tail to head

HERE..

Circular Linked Lists

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

A circular linked list with an external pointer to the last node Circular Linked Lists A circular linked list with an external pointer to the last node

Circular Linked Lists // display the data in a circular linked list; // list points to its last node if (list != NULL){ // list is not empty Node *first = list->next; // point to first node Node *cur = first; // start at first node // Loop invariant: cur points to next node to // display do{ display(cur->item); // write data portion cur = cur->next; // point to next node } while (cur != first); // list traversed? }

Dummy Head Nodes Dummy head node Always there, even when the linked list is empty Insertion and deletion algorithms initialize prev to reference the dummy head node, rather than NULL

Doubly Linked Lists

Doubly Linked Lists Each node points to both its predecessor and its successor Circular doubly linked list precede pointer of the dummy head node points to the last node next reference of the last node points to the dummy head node No special cases for insertions and deletions

Doubly Linked Lists Figure 4.29 (a) A circular doubly linked list with a dummy head node (b) An empty list with a dummy head node

Doubly Linked Lists To delete the node to which cur points (cur->precede)->next = cur->next; (cur->next)->precede = cur->precede; To insert a new node pointed to by newPtr before the node pointed to by cur newPtr->next = cur; newPtr->precede = cur->precede; cur->precede = newPtr; newPtr->precede->next = newPtr;

Summary Each pointer in a linked list is a pointer to the next node in the list Algorithms for insertions and deletions in a linked list involve traversing the list and performing pointer changes Inserting a node at the beginning of a list and deleting the first node of a list are special cases

Summary Recursion can be used to perform operations on a linked list In a circular linked list, the last node points to the first node Dummy head nodes eliminate the special cases for insertion into and deletion from the beginning of a linked list

Quiz Writing functions by using linked lists Copy, sort, compare two lists, etc. Functions using two linked lists Is the list sorted? Recursive operations with linked lists

Write a function to reverse a linked list static void reverse(struct node * head_ref) {     struct node* prev   = NULL;     struct node* current = head_ref;     struct node* next;     while (current != NULL)     {         next  = current->next;          current->next = prev;           prev = current;         current = next;     }      head_ref = prev; }

Example questions for exam…. Is the linked list a palindrome?? Frequency of a given number in a Linked List Sort a linked list by using bubble sort. Write a function to insert a new value in the middle of a linked list. Write a function to find the index of the given value in the linked list.