170 likes | 184 Views
Learn about linked list data structure, how it differs from arrays, node declaration, insertion methods, search and deletion operations. Code examples included.
E N D
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
Linked Lists A C B A • 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 Head node data pointer
Linked List payload payload payload payload next next next next struct listItem {type payload;struct listItem *next; };
Linked List (continued) payload payload payload payload next next next next struct listItem {type payload;struct listItem *next; }; struct listItem *head;
DECLARING A NODE TYPE • struct node{ int value; struct node *next; } • struct point { double x,y; }; struct vertex { struct point element; struct vertex *next;}
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”
BUILDING LINKED LIST value next next • 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; p p 10
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)
INSERTING A NODE AT THE BEGINNING OF THE LIST struct Node *first=NULL, *new_node; new_node head head new_node new_node= new Node; • If first points to the first node of the linked list: new_node->next = head; head = new_node;
INSERTING A NODE AT THE BEGINNING OF THE LIST new_node->value=10; new_node->next = head; 10 new_node new_node 10 head = new_node; head head head new_node 10
INSERTING A NODE AT THE BEGINNING OF THE LIST new_node = new node; head head head 10 10 10 new_node->value = 20; new_node 20 new_node->next = head; new_node head = new_node; head new_node new_node 20 20 10
SEARCHING A LINKED LIST head 20 10 • for (p=head; p!=NULL; p=p->next) {… } • int value=20; struct node *p; • for (p=head; p!=NULL; p=p->next) { 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; }
INSERTING A NODE AT THE MIDDLE OF THE LIST head cur • struct node *tmp = new node; • tmp->value= cur->value; • tmp->next = cur->next_ptr; 20 10 • cur->value = 15; • cur->next = tmp; cur cur tmp 10 first first struct *node p=head; struct node * cur= find(p, 10); 20 20 15 15
DELETING A NODE FROM THE LIST head cur 20 tmp • cur->value = tmp->value; cur tmp 15 head 15 15 • cur->next = tmp->next; cur tmp 10 10 head 15 15 10 • delete tmp; head struct *node p=head; struct node * cur= find(p, 20); struct node *tmp; tmp = cur->next; 15 10
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; }
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; } }