0% found this document useful (0 votes)
25 views22 pages

Lecture 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views22 pages

Lecture 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Lecture 3: Linked List

Md. Nazmul Abdal


Lecturer
Department of CSE
University of Liberal Arts Bangladesh (ULAB)
Introduction

⚫ A linked list is a linear data structure that includes a series of


connected nodes.
⚫ Here, each node stores the data and the address of the next node.

⚫ You have to start somewhere, so we give the address of the first


node a special name called HEAD.
⚫ Also, the last node in the linked list can be identified because its
next portion points to NULL.
⚫ Linked lists can be of 3 types: Singly, Doubly, and Circular
Representation

⚫ Each node consists of:


⚫ A data item
⚫ An address of another node

Syntax:
struct node
{
int data;
struct node *next;
};
Example

#include <stdio.h> one = malloc(sizeof(struct node));


#include <stdlib.h> two = malloc(sizeof(struct node));
struct node { three = malloc(sizeof(struct node));
int value; one->value = 1;
struct node *next; two->value = 2;
}; three->value = 3;
void printLinkedlist(struct node *p) { one->next = two;
while (p != NULL) { two->next = three;
printf("%d ", p->value); three->next = NULL;
p = p->next; head = one;
} printLinkedlist(head);
} }
int main() { Output:
struct node *head;
1, 2, 3
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
Applications

⚫ Dynamic memory allocation

⚫ Implemented in stack and queue

⚫ In undo functionality of software

⚫ Hash tables, Graphs


Basic Operations

⚫ Traversal - access each element of the linked list

⚫ Insertion - adds a new element to the linked list

⚫ Deletion - removes the existing elements

⚫ Search - find a node in the linked list

⚫ Sort - sort the nodes of the linked list


Things to Remember

⚫ head points to the first node of the linked list

⚫ next pointer of the last node is NULL, so if the next current node is

NULL, we have reached the end of the linked list.

⚫ In all of the examples, we will assume that the linked list has three

nodes: 1 --->2 --->3


Traverse

Code: Output:
struct node *temp = head; List elements are -
printf("\n\nList elements are - \n"); 1 --->2 --->3 --->
while(temp != NULL) {
printf("%d --->",temp->data);
temp = temp->next;
}
Insert at the Beginning

Algorithm:
1. Allocate memory for new node
2. Store data
3. Change next of new node to point to head
4. Change head to point to recently created node

Code:
struct node *newNode;
newNode = malloc(sizeof(struct node));
newNode->data = 4;
newNode->next = head;
head = newNode;
Insert at the Middle

Code:
struct node *newNode;
Algorithm: newNode = malloc(sizeof(struct node));
1. Allocate memory and store data for newNode->data = 4;
new node struct node *temp = head;
2. Traverse to node just before the for(int i=2; i < position; i++) {
required position of new node if(temp->next != NULL) {
3. Change next pointers to include temp = temp->next;
new node in between
}
}
newNode->next = temp->next;
temp->next = newNode;
Insert at the End

Code:
struct node *newNode;
Algorithm:
newNode = malloc(sizeof(struct node));
1. Allocate memory for new node
newNode->data = 4;
2. Store data
newNode->next = NULL;
3. Traverse to last node
struct node *temp = head;
4. Change next of last node to
while(temp->next != NULL){
recently created node
temp = temp->next;
}
temp->next = newNode;
Delete from Beginning

Algorithm:
1. Point head to the second node

Code:
head = head->next;
Delete from Middle

Code:
Algorithm: for(int i=2; i< position; i++) {
1. Traverse to element before the if(temp->next!=NULL) {
element to be deleted temp = temp->next;
2. Change next pointers to exclude the }
node from the chain }
temp->next = temp->next->next;
Delete from End

Code:
Algorithm: struct node* temp = head;
1. Traverse to second last element while(temp->next->next!=NULL){
2. Change its next pointer to null temp = temp->next;
}
temp->next = NULL;
Search

Algorithm: Code:
1. Make head as the current node. bool searchNode(struct Node** head_ref, int key) {

2. Run a loop until the current node is struct Node* current = *head_ref;
NULL because the last element points while (current != NULL) {
to NULL. if (current->data == key)
3. In each iteration, check if the key of return true;
the node is equal to item. If it the key
current = current->next;
matches the item, return true
}
otherwise return false.
return false;
}
Sort

Algorithm:
1. Make the head as the current node and create another node index for later use.
2. If head is null, return.
3. Else, run a loop till the last node (i.e., NULL).
4. In each iteration, follow the following step 5-6.
5. Store the next node of current in index.
6. Check if the data of the current node is greater than the next node. If it is greater, swap
current and index.
Sort

Code:
void sortLinkedList(struct Node** head_ref) { while (index != NULL) {

struct Node *current = *head_ref, *index = NULL; if (current->data > index->data) {

int temp; temp = current->data;

if (head_ref == NULL) { current->data = index->data;

return; index->data = temp;

} }

else { index = index->next;

while (current != NULL) { }

index = current->next; current = current->next;


}
}
}
Doubly Linked List

⚫ We add a pointer to the previous node in a doubly-linked list.


⚫ Thus, we can go in either direction: forward or backward.

Syntax:
struct node {
int data;
struct node *next;
struct node *prev;
};
Example

/* Initialize nodes */ /* Connect nodes */


struct node *head; one->next = two;
struct node *one = NULL; one->prev = NULL;
struct node *two = NULL;
struct node *three = NULL; two->next = three;
two->prev = one;
/* Allocate memory */
one = malloc(sizeof(struct node)); three->next = NULL;
two = malloc(sizeof(struct node)); three->prev = two;
three = malloc(sizeof(struct node));
/* Save address of first node in head */
/* Assign data values */ head = one;
one->data = 1;
two->data = 2;
three->data = 3;
Circular Linked List

⚫ A circular linked list is a variation of a linked list in which the last


element is linked to the first element.
⚫ A circular linked list can be either singly linked or doubly linked.
⚫ for singly linked list, next pointer of last item points to the
first item
⚫ In the doubly linked list, prev pointer of the first item points
to the last item as well.
Example

/* Initialize nodes */ /* Connect nodes */


struct node *head; one->next = two;
struct node *one = NULL; two->next = three;
struct node *two = NULL; three->next = one;
struct node *three = NULL;
/* Save address of first node in head */
/* Allocate memory */ head = one;
one = malloc(sizeof(struct node));
two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));

/* Assign data values */


one->data = 1;
two->data = 2;
three->data = 3;
Thank You

You might also like