Linked Lists.

Slides:



Advertisements
Similar presentations
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Advertisements

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.
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.
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.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compiling time * Dynamic Memory.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class List { public: List(); // constructor List(const.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Concept of lists and array implementations of lists.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class list { public: list(); // constructor list(const.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
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.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Motivation * Doubly linked lists are useful for playing video and sound files with “rewind”
List, (dynamic) linked list Let’s first forget about ‘classes’, but only a dynamic list. We make lists with ‘classes’ afterwards.
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 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
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.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
LINKED LISTS.
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.
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.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
CSCE 210 Data Structures and Algorithms
Programming Circular Linked List.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Lectures linked lists Chapter 6 of textbook
Review Deleting an Element from a Linked List Deletion involves:
Exercise 1 From Lec80b-Ponter.ppt.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Chapter 4 Linked Lists.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
CSCE 210 Data Structures and Algorithms
CSCI 3333 Data Structures Linked Lists.
Chapter 4 Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists Chapter 4.
Summary on linked lists
Chapter 16-2 Linked Structures
Linked Lists.
as an abstract data structure and
Chapter 4 Linked Lists.
Arrays and Linked Lists
Linked Lists.
Programming Abstractions
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Linked List.
Chapter 16 Linked Structures
Dynamic Objects.
Data Structures & Algorithms
List as an Abstract Data Type
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

Linked Lists

Motivation A “List” is a useful structure to hold a collection of data. Currently, we use arrays for lists Examples: List of ten students marks int studentMarks[10]; List of temperatures for the last two weeks double temperature[14];

Motivation list using static array list using dynamic array int myArray[1000]; int n; We have to decide (to oversize) in advance the size of the array (list) list using dynamic array int* myArray; cin >> n; myArray = new int[n]; We allocate an array (list) of any specified size while the program is running linked-list (dynamic size) size = ?? The list is dynamic. It can grow and shrink to any size.

‘n’ is removed, so we don’t need to ‘care’ about it! How to use a linked list? int main() { … initialize(A,0); print(A); A = addEnd(A,5); print(A, n); A = addHead(A,5); A = deleteFirst(A); selectionSort(A); } int main() { … initialize(A, n, 0); print(A, n); A = addEnd(A,n,5); A = addHead(A,n,5); A = deleteFirst(A,n); selectionSort(A, n); } ‘n’ is removed, so we don’t need to ‘care’ about it!

Now the link is explicit, any places! Array naturally represents a (ordered) list, the link is implicit, consecutive and contiguous! Now the link is explicit, any places! Data 75 85 20 45 Link Link Data 45 85 20 75 20 45 75 85 Data Link

Linked Lists: Basic Idea A linked list is an ordered collection of data Each element of the linked list has Some data A link to the next element The link is used to chain the data Example: A linked list of integers: 20 45 75 85 Data Link

Linked Lists: Basic Ideas The list can grow and shrink 20 45 75 85 addEnd(75), addEnd(85) deleteEnd(85), deleteHead(20), deleteHead(45) 75

Linked Lists: Operations Original linked list of integers: Insertion (in the middle): Deletion (in the middle) 20 45 75 85 old value 20 45 75 85 60 20 45 75 85 deleted item

Definition of linked list type: struct Node{ int data; Node* next; }; typedef Node* NodePtr;

typedef typedef allows you to make new shortcuts to existing types typedef int WAH; WAH k; // same as: int k; typedef int* WAHPTR; WAHPTR p; // same as: int* p; typedef Node* NodePtr; NodePtr Head; // same as: Node* Head;

Linked List Structure Definition Definition Create a Node struct Node { int data; Node* next; }; typedef Node* NodePtr; Create a Node NodePtr p; p = new Node; Delete a Node delete p; Definition struct Node { int data; Node* next; }; Create a Node Node* p; p = new Node; Delete a Node delete p;

Access fields in a node (*p).data; //access the data field (*p).next; //access the pointer field Or it can be accessed this way p->data //access the data field p->next //access the pointer field

Representing and accessing linked lists 20 45 75 85 Head We define a pointer NodePtr head; (also: Node* head) that points to the first node of the linked list. When the linked list is empty then head is NULL.

Passing a Linked List to a Function It is roughly the same as for an array!!! When passing a linked list to a function it should suffice to pass the value of head. Using the value of head the function can access the entire list. Problem: If a function changes the beginning of a list by inserting or deleting a node, then head will no longer point to the beginning of the list. Solution: When passing head always pass it by reference or using a function to return a new pointer value

Manipulation (implementation) of a Unsorted Linked List

Start the first node from scratch head = NULL; Head NodePtr newPtr; newPtr = new Node; newPtr->data = 20; newPtr->next = NULL; head = newPtr; Head newPtr 20

Inserting a Node at the Beginning newPtr = new Node; newPtr->data = 13; newPtr->next = Head; head = newPtr; 20 Head 13 newPtr

Keep going … Head newPtr 50 40 13 20

Adding an element to the head: void addHead(NodePtr& head, int newdata){ NodePtr newPtr = new Node; newPtr->data = newdata; newPtr->next = Head; head = newPtr; }

It can also be written (more functionally) as: NodePtr addHead(NodePtr head, int newdata){ NodePtr newPtr = new Node; newPtr->data = newdata; newPtr->next = Head; return newPtr; } Compare it with ‘addHead’ with a dynamic array implementation

Deleting the Head Node NodePtr p; p = head; head = head->next; delete p; head (to delete) 50 40 13 20 p

Or as a function: void deleteHead(NodePtr& head){ if(head != NULL){ NodePtr p = head; head = head->next; delete p; } Or as a function: NodePtr deleteHead(NodePtr head){ if(head != NULL){ NodePtr p = head; head = head->next; delete p; } return head;

Displaying a Linked List p = head; print out (*p) p = p->next; print out (*p) head 20 45 p head 20 45 p

A linked list is displayed by walking through its nodes one by one, and displaying their data fields (similar to an array!). void displayList(NodePtr head){ NodePtr p; p = head; while(p != NULL){ cout << p->data << endl; p = p->next; } For an array: void displayArray(int data[], int size) { int* p; p=data; while ( (p-data)<size ) { cout << *p << endl; p++; } void displayArray(int data[], int size) { int n=0; while ( n<size ) { cout << data[i] << endl; n++; }

Searching for a value in a linked list (look at array searching first

Remember searching algorithm in an array: It is essentially the same! void main() { const int size=8; int data[size] = { 10, 7, 9, 1, 17, 30, 5, 6 }; int value; cout << "Enter search element: "; cin >> value; int n=0; int position=-1; bool found=false; while ( (n<size) && (!found) ) { if(data[n] == value) { found=true; position=n;} n++; } if(position==-1) cout << "Not found!!\n"; else cout << "Found at: " << position << endl; It is essentially the same!

If we use a pointer to an array, it will be much closer! Searching for a value NodePtr searchNode(NodePtr head, int item){ NodePtr p = head; NodePtr result = NULL; bool found=false; while((p != NULL) && (!found)){ if(p->data == item) { found = true; result = p;} p = p->next; } return result; int searchArray(int data[], int size, int value){ int n=0; int position=-1; bool found=false; while ( (n<size) && (!found) ) { if(data[n] == value) { found=true; position=n;} n++; } return position; If we use a pointer to an array, it will be much closer!

More operation: adding to the end Original linked list of integers: Add to the end (insert at the end): 50 40 13 20 50 40 13 20 60 Last element The key is how to locate the last element or node of the list!

Link new object to last->next Link a new object to empty list Add to the end: void addEnd(NodePtr& head, int newdata){ NodePtr newPtr = new Node; newPtr->data = newdata; newPtr->next = NULL; NodePtr last = head; if(last != NULL){ // general non-empty list case while(last->next != NULL) last=last->next; last->next = newPtr; } else // deal with the case of empty list head = newPtr; Link new object to last->next Link a new object to empty list

Add to the end as a function: NodePtr addEnd(NodePtr head, int newdata){ NodePtr newPtr = new Node; newPtr->data = newdata; newPtr->next = NULL; NodePtr last = head; if(last != NULL){ // general non-empty list case while(last->next != NULL) last=last->next; last->next = newPtr; } else // deal with the case of empty list head = newPtr; return head;

Manipulation (implementation) of a Sorted Linked List

Inserting a value in a sorted list How to do it in a sorted array? a static array and a dynamic array 1. Find the position 2. Free up the place by moving the others 3. Insert the new value

Inserting a Node 1. (a) Create a new node using: 20 33 45 75 ... NodePtr newPtr = new node; (b) Fill in the data field correctly. 2. Find “prev” and “cur” such that the new node should be inserted between *prev and *cur. 3. Connect the new node to the list by using: (a) newPtr->next = cur; (b) prev->next = newPtr; Head cur 20 33 45 75 prev ... newPtr

Finding prev and cur Suppose that we want to insert or delete a node with data value newValue. Then the following code successfully finds prev and cur such that prev->data < newValue <= cur->data

It’s a kind of search algo, Prev is necessary as we can’t go back! prev = NULL; cur = head; found=false; while( (cur!=NULL) && (!found) ) { if (newValue > cur->data) { prev=cur; cur=cur->next; } else found = true; Prev is necessary as we can’t go back!

A useful programming ‘trick’: boolean ‘found’ can be avoided. Finally, it is equivalent to: prev = NULL; cur = head; while( (cur!=NULL) && (newValue>cur->data) ) { prev=cur; cur=cur->next; } Logical AND (&&) is short-circuited, sequential, i.e. if the first part is false, the second part will not be executed.

If the position happens to be the head //insert item into linked list according to ascending order void insertNode(NodePtr& head, int item){ NodePtr newp, cur, pre; newp = new Node; newp->data = item; pre = NULL; cur = head; while( (cur != NULL) && (item>cur->data)){ pre = cur; cur = cur->next; } if(pre == NULL){ //insert to head of linked list newp->next = head; head = newp; } else { pre->next = newp; new->next = cur; If the position happens to be the head General case

Deleting a Node To delete a node from the list 1. Locate the node to be deleted (a) cur points to the node. (b) prev points to its predecessor 2. Disconnect node from list using: prev->next = cur->next; 3. Return deleted node to system: delete cur; Head (to delete) ... 20 45 75 85 prev cur

Delete an element in a sorted linked list: void deleteNode(NodePtr& head, int item){ NodePtr prev=NULL, cur = head; while( (cur!=NULL) && (item > cur->data)){ prev = cur; cur = cur->next; } if ( cur!==NULL && cur->data==item) { if(cur==Head) Head = Head->next; else prev->next = cur->next; delete cur; Get the location If it’s in, negation of ‘not in’ We can delete only if the element is present! If (cur==NULL || cur->data!=item) Item is not in the list! If the element is at the head General case

Other variants of linked lists

A note on ‘Dummy Head node’ A well-known implementation ‘trick’ or ‘method’: add one more node at the beginning, which does not store any data, to ease operations. always present, even when the linked list is empty Insertion and deletion algorithms initialize prev to reference the dummy head node, rather than NULL head empty list! head 40 13 20 Dummy head node list of (40,13,20)

Circular Linked Lists A Circular Linked List is a special type of Linked List It support the traversing from the end of the list to the beginning of the list by making the last node points back to the head of the list 10 20 40 55 70 Rear

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 10 70 20 55 40 Head Cur Cur->next Cur->prev

Doubly Linked List Definition struct Node{ int data; Node* next; Node* prev; }; typedef Node* NodePtr;

Doubly Linked Lists 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

Idea of ‘dummy’ object ‘dummy object’ is also called a ‘sentinel’, it allows the simplification of special cases, but confuses the emptyness NULL! Instead of pointing to NULL, point to the ‘dummy’!!! Skip over the dummy for the real list 70 20 55 40 Head 10 Dummy Head Node

Head->next = head; compared with head=NULL; Empty list: Head Dummy Head Node Head->next = head; compared with head=NULL;

Binary tree A drawing of linked list with one pointer … A drawing of binary tree with two pointers … Struct BinaryNode { double element; // the data BinaryNode* left; // left child BinaryNode* right; // right child }