Programming Linked Lists COMP 104 Linked Lists Slide

  • Slides: 31
Download presentation
Programming Linked Lists

Programming Linked Lists

COMP 104 Linked Lists / Slide 2 Motivation * A “List” is a useful

COMP 104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n * Currently, we use arrays for lists Examples: List of ten students marks int student. Marks[10]; List of temperatures for the last two weeks double temperature[14];

COMP 104 Linked Lists / Slide 3 Motivation * list using static array int

COMP 104 Linked Lists / Slide 3 Motivation * list using static array int my. Array[10]; We have to decide in advance the size of the array (list) * list using dynamic array int n, *my. Array; cin >> n; my. Array = 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.

COMP 104 Linked Lists / Slide 4 Linked Lists: Basic Idea *A linked list

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

COMP 104 Linked Lists / Slide 5 Linked Lists: Basic Ideas * The 20

COMP 104 Linked Lists / Slide 5 Linked Lists: Basic Ideas * The 20 list can grow and shrink 45 add(75), add(85) 20 45 75 delete(85), delete(45), delete(20) 75 85

COMP 104 Linked Lists / Slide 6 Linked Lists: Operations * Original linked list

COMP 104 Linked Lists / Slide 6 Linked Lists: Operations * Original linked list of integers: 20 45 75 85 * Insertion: 20 old value 45 85 60 * Deletion 20 75 45 deleted item 75 85

#include <iostream> using namespace std; struct Node{ int data; Node *next; }; typedef Node*

#include <iostream> using namespace std; struct Node{ int data; Node *next; }; typedef Node* Node. Ptr;

COMP 104 Linked Lists / Slide 8 typedef * typedef allows you to make

COMP 104 Linked Lists / Slide 8 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* Node. Ptr; Node. Ptr Head; // same as: Node* Head;

COMP 104 Linked Lists / Slide 9 Linked List Structure * Node n :

COMP 104 Linked Lists / Slide 9 Linked List Structure * Node n : Data + Link Definition struct Node { int data; Node* next; }; n Create a Node* p; p = new Node; n //contains useful information //points to next element or NULL Delete a Node delete p; //points to newly allocated memory

COMP 104 Linked Lists / Slide 10 Linked List Structures n Access fields in

COMP 104 Linked Lists / Slide 10 Linked List Structures n 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 COMP 104 Linked Lists / Slide 11 Head 20

Representing and accessing Linked Lists COMP 104 Linked Lists / Slide 11 Head 20 * 45 75 85 We define a pointer Node. Ptr Head; that points to the first node of the linked list. When the linked list is empty then Head is NULL.

COMP 104 Linked Lists / Slide 12 Passing a Linked List to a Function

COMP 104 Linked Lists / Slide 12 Passing a Linked List to a Function * 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) Head 20 45 75 85

COMP 104 Linked Lists / Slide 13 Manipulation of a Unsorted Linked List

COMP 104 Linked Lists / Slide 13 Manipulation of a Unsorted Linked List

COMP 104 Linked Lists / Slide 14 Start the first node from scratch Head

COMP 104 Linked Lists / Slide 14 Start the first node from scratch Head = NULL; Head Node. Ptr new. Ptr; new. Ptr = new Node; new. Ptr->data = 20; new. Ptr->next = NULL; Head = new. Ptr; 20 Head new. Ptr

COMP 104 Linked Lists / Slide 15 Inserting a Node at the Beginning new.

COMP 104 Linked Lists / Slide 15 Inserting a Node at the Beginning new. Ptr = new Node; new. Ptr->data = 13; new. Ptr->next = Head; Head = new. Ptr; 20 Head 13 new. Ptr

COMP 104 Linked Lists / Slide 16 Keep going … Head 50 new. Ptr

COMP 104 Linked Lists / Slide 16 Keep going … Head 50 new. Ptr 40 13 20

Adding an element to the head: void add. Head(Node. Ptr& Head, int newdata){ Node.

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

COMP 104 Linked Lists / Slide 18 Deleting the Head Node. Ptr cur; cur

COMP 104 Linked Lists / Slide 18 Deleting the Head Node. Ptr cur; cur = Head; Head = Head->next; delete cur; Head (to delete) 50 cur 40 13 20

void del. Head(Node. Ptr& Head){ if(Head != NULL){ Node. Ptr cur = Head; Head

void del. Head(Node. Ptr& Head){ if(Head != NULL){ Node. Ptr cur = Head; Head = Head->next; delete cur; } }

COMP 104 Linked Lists / Slide 20 Displaying a Linked List cur = Head;

COMP 104 Linked Lists / Slide 20 Displaying a Linked List cur = Head; Head 20 45 cur = cur->next; Head 20 45 cur

COMP 104 Linked Lists / Slide 21 *A linked list is displayed by walking

COMP 104 Linked Lists / Slide 21 *A linked list is displayed by walking through its nodes one by one, and displaying their data fields. void Display. List(Node. Ptr Head){ Node. Ptr cur; cur = Head; while(cur != NULL){ cout << cur->data << endl; cur = cur->next; } }

Searching for a node //return the pointer of the node has data=item //return NULL

Searching for a node //return the pointer of the node has data=item //return NULL if item does not exist Node. Ptr search. Node(Node. Ptr Head, int item){ Node. Ptr Cur = Head; Node. Ptr Result = NULL; while(Cur != NULL){ if(Cur->data == item) Result = Cur; Cur = Cur->next; } return Result; }

More operation: adding to the end COMP 104 Linked Lists / Slide 23 *

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

Add to the end: void add. End(Node. Ptr& Head, int newdata){ Node. Ptr last

Add to the end: void add. End(Node. Ptr& Head, int newdata){ Node. Ptr last = Head; Node. Ptr new. Ptr = new Node; new. Ptr->data = newdata; new. Ptr->next = NULL; if(Head != NULL){ // non-empty list case while(last->next != NULL) last = last->next; last->next = new. Ptr; } else // deal with the case of empty list Head = new. Ptr; } Link new object to last->next Link a new object to empty list

COMP 104 Linked Lists / Slide 25 Manipulation of a Sorted Linked List

COMP 104 Linked Lists / Slide 25 Manipulation of a Sorted Linked List

COMP 104 Linked Lists / Slide 26 Inserting a Node Head 20 prev 45

COMP 104 Linked Lists / Slide 26 Inserting a Node Head 20 prev 45 33 new. Ptr cur 75 . . .

COMP 104 Linked Lists / Slide 27 * To insert a new node into

COMP 104 Linked Lists / Slide 27 * To insert a new node into the list 1. (a) Create a new node using: Node. Ptr new. Ptr = 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) new. Ptr->next = cur; (b) prev->next = new. Ptr;

COMP 104 Linked Lists / Slide 28 Finding prev and cur * Suppose that

COMP 104 Linked Lists / Slide 28 Finding prev and cur * Suppose that we want to insert or delete a node with data value new. Value. Then the following code successfully finds prev and cur. prev = NULL; cur = Head; while(cur!=NULL && prev = cur; cur = cur->next; } new. Value > cur->data){

//insert item into linked list in ascending order void insert. Node(Node. Ptr& Head, int

//insert item into linked list in ascending order void insert. Node(Node. Ptr& Head, int item){ Node. Ptr New, Cur, Pre; New = new Node; New->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 New->next = Head; Head = New; } else { Pre->next = New; New->next = Cur; } }

COMP 104 Linked Lists / Slide 30 Deleting a Node * To delete a

COMP 104 Linked Lists / Slide 30 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; (to delete) Head 20 45 75 prev cur 85 . . .

Delete an element in a sorted linked list: void delete. Node(Node. Ptr& Head, int

Delete an element in a sorted linked list: void delete. Node(Node. Ptr& Head, int item){ Node. Ptr prev, cur = Head; while(cur!=NULL && item > cur->data){ prev = cur; cur = cur->next; } if(cur==NULL || cur->data!=item){ cout << "Delete error: " << item << " not in list!" << endl; return; } if(cur==Head) Head = Head->next; else prev->next = cur->next; delete cur; }