Download presentation
Presentation is loading. Please wait.
1
Linked Lists
2
Preliminaries 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
3
Linked Lists A linked list is a series of connected nodes
B C Head A linked list is a series of connected nodes Each node contains at least A piece of data (any type) Pointer to the next node in the list Head: pointer to the first node The last node points to NULL node A data pointer
4
Linked List struct listItem { type payload; struct listItem *next; };
5
Linked List (continued)
struct listItem { type payload; struct listItem *next; }; struct listItem *head; payload next payload next payload next payload next
6
DECLARING A NODE TYPE struct node{ int value; struct node *next; }
struct point { double x,y; }; struct vertex { struct point element; struct vertex *next;}
7
Usage of Linked Lists Not massive amounts of data
Linear search is okay Sorting not necessary or sometimes not possible Need to add and delete data “on the fly” Even from middle of list Items often need to be added to or deleted from the “ends”
8
BUILDING LINKED LIST A node in a linked list is usually a struct
struct node { int value; Node *next; }; //end struct A node is dynamically allocated node *p; p = new node; (*p).value = 10; p->value= 10; next value p p 10
9
Inserting a new node Possible cases of InsertNode
Insert into an empty list Insert in front Insert at back Insert in middle But, in fact, only need to handle two cases Insert as the first node (Case 1 and Case 2) Insert in the middle or at the end of the list (Case 3 and Case 4)
10
INSERTING A NODE AT THE BEGINNING OF THE LIST
If first points to the first node of the linked list: new_node->next = head; head = new_node; struct Node *first=NULL, *new_node; head new_node new_node= new Node; head new_node
11
INSERTING A NODE AT THE BEGINNING OF THE LIST
new_node->value=10; 10 new_node head new_node->next = head; head 10 new_node head = new_node; head 10 new_node
12
INSERTING A NODE AT THE BEGINNING OF THE LIST
new_node = new node; head 10 new_node new_node->value = 20; head 10 20 new_node new_node->next = head; head 10 20 new_node head = new_node; head 20 new_node 10
13
SEARCHING A LINKED LIST
for (p=head; p!=NULL; p=p->next) {… } int value=20; struct node *p; { if (p->value== value) return p; } struct node *find(struct node *list, int n){ while (list!=NULL and list->value!=n ) p=p->next; return list; } head 20 10
14
INSERTING A NODE AT THE MIDDLE OF THE LIST
head 20 10 cur struct *node p=head; struct node * cur= find(p, 10); struct node *tmp = new node; tmp->value= cur->value; tmp->next = cur->next_ptr; 10 tmp cur->value = 15; cur->next = tmp; first 20 cur 15 first 20 cur 15
15
DELETING A NODE FROM THE LIST
head 20 15 cur tmp 10 struct *node p=head; struct node * cur= find(p, 20); struct node *tmp; tmp = cur->next; cur->value = tmp->value; cur tmp head 15 15 10 cur->next = tmp->next; cur tmp head 15 15 10 delete tmp; head 15 10 15
16
Printing all the elements
void DisplayList(void) Print the data of all the elements Print the number of the nodes in the list void DisplayList() { int num = 0; Node* currNode = head; while (currNode != NULL){ cout << currNode->value << endl; currNode = currNode->next; num++; } cout << "Number of nodes in the list: " << num << endl;
17
Destroying the list Void DestroyList(void)
Step through the list and delete each node one by one. Void DestroyList(void) { Node* currNode = head, *nextNode = NULL; while (currNode != NULL) { nextNode = currNode->next; // destroy the current node delete currNode; currNode = nextNode; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.