A linked list is a data structure made up of nodes that are connected to each other. Each node contains data and a link to the next node. Linked lists can grow and shrink dynamically as nodes are added or removed. There are several types of linked lists including single linked lists where navigation is forward only, doubly linked lists where navigation is bidirectional, and circular linked lists where the last node links back to the first node. Common operations on linked lists include appending nodes to add to the end, inserting nodes in a sorted manner, and deleting nodes by value. These operations involve allocating memory for new nodes, linking nodes together, and removing nodes by adjusting pointers.
Web & Social Media Analytics Previous Year Question Paper.pdf
Lec-4_Linked-List (1).pdf
1. LINKED LIST
➢ A linked list is a series of connected nodes,
where each node is a data structure.
➢ A linked list can grow or shrink in size as the
program runs
3. Ways to maintain a List in a Memory
Two ways
Array
1 2 3 4 5
Linked List
Data Addr Data Addr
4. Types of Linked List
➢Single Linked List - navigation is forward only.
➢Doubly Linked List - forward and backward navigation
is possible.
➢Circular Linked List - the last element is linked to the
first element
5. What is a Single Linked List?
➢A single link list is a list made up of nodes that consists of
two parts.
✓Data
✓Link
Data Link
contains the actual data
contains the address of the next node of the list.
6. Presentation of Single Linked List
Suppose we want to store a list of numbers 20, 35, 50, 70
20 35 50 70
1000
2000 3000 4000
7. 20 2000 35 3000 50 4000 70 NULL
The Address must be store on each node
1000
Head
8. Creating the Node of a Single Linked List
20 2000 35 3000 50 NULL
1000
struct node{
int data;
struct node *link;
};
9. 20 NULL
1000
#include<stdio.h>
struct node{
int data;
struct node *link;
};
Int main(){
struct node*head = malloc(sizeof(struct node));
head ->data = 20;
head ->link = NULL;
struct node*head = malloc(sizeof(struct node));
head ->data = 35;
head ->link = NULL;
return 0;
}
35 NULL
2000
New head
Head
10. Doubly Linked List
➢forward and backward navigation is possible.
20 2000 35 3000 50 Null
1000 Head pointer
1000 2000 3000
data
next
11. 0 data next previ
ous
data next previ
ous
data next
head
Struct node
Struct node*prev;
Int data;
Struct node*next;
}
Int main(){
struct node*head = malloc(sizeof(struct
node));
head ->=NULL
head ->data = data;
head ->link = NULL;
return 0;
}
12. Circular Linked List
Two types
❑Circular Singly Linked List
❑Circular Doubly Linked List
❑Circular Singly Linked List is a similar to singly linked list except that the
last node of the circular singly linked list points to the first node.
20 2000 35 3000 50 NULL
13. ❑ Circular Doubly Linked List – This is similar to a doubly linked list except that the last
node of the circular doubly linked list points to the first node and the first node point
to the last node.
0 data next previ
ous
data next previ
ous
data next
14. Struct node
Int data;
Struct node*next;
};
int main(){
int data = ______;
struct node*tail;
tail = circularSingly(data);
cout<<tail ->data;
return 0;
}
struct node*circularSingly(int data)
{
struct node*temp = malloc(sizeof(struct node));
temp ->data=data
temp->next= temp;
return temp;
}
20 1000
1000
1000
Circular Singly Linked List
16. Linked List Operations
We will use the following class declaration, which is stored in FloatList.h.
class FloatList
{
private:
// Declare a structure for the list
struct ListNode
{
float value;
struct ListNode *next;
};
ListNode *head; // List head pointer
public:
FloatList(void) // Constructor
{ head = NULL; }
void appendNode(float);
void insertNode(float);
void deleteNode(float);
void displayList(void);
};
17. Appending a Node to the List
To append a node to a linked list means to add the
node to the end of the list.
Create a new node.
Store data in the new node.
If there are no nodes in the list
Make the new node the first node.
Else
Traverse the List to Find the last node.
Add the new node to the end of the list.
End If.
18. Program
void FloatList::appendNode(float num)
{
ListNode *newNode, *nodePtr;
// Allocate a new node & store num
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;
// If there are no nodes in the list
// make newNode the first node
if (!head)
head = newNode;
else // Otherwise, insert newNode at end
{
// Initialize nodePtr to head of list
nodePtr = head;
// Find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node
nodePtr->next = newNode;
}
}
19. Sample Program
// This program demonstrates a simple append
// operation on a linked list.
#include <iostream.h>
#include "FloatList.h”
void main(void)
{
FloatList List;
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
}
20. ➢ The displayList member function traverses the list,
displaying the value member of each node
Assign List head to node pointer.
While node pointer is not NULL
Display the value member of the node pointed to
by node pointer.
Assign node pointer to its own next member.
End While.
Traversing the List
22. Sample Program 2
// This program calls the displayList member function.
// The funcion traverses the linked list displaying
// the value stored in each node.
#include <iostream.h>
#include "FloatList.h"
void main(void)
{
FloatList List;
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
list.displayList();
}
23. Inserting a Node
Create a new node.
Store data in the new node.
If there are no nodes in the list
Make the new node the first node.
Else
Find the first node whose value is greater than or
equal the new value, or the end of the list (whichever is
first).
Insert the new node before the found node, or at the end of
the list if no node was found.
End If.
24. // Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is less
// than num.
while (nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
Algorithm for InsertNode
25. insertNode function
void FloatList::insertNode(float num)
{
ListNode *newNode, *nodePtr, *previousNode;
// Allocate a new node & store Num
newNode = new ListNode;
newNode->value = num;
// If there are no nodes in the list
// make newNode the first node
if (!head)
{
head = newNode;
newNode->next = NULL;
}
26. else // Otherwise, insert newNode.
{
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is less
// than num.
while (nodePtr != NULL && nodePtr->value < num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If the new mode is to be the 1st in the list, // insert it before all other nodes.
if (previousNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else{
previousNode->next = newNode;
newNode->next = nodePtr;
} }
}
27. Sample Program 3
#include <iostream.h>
#include "FloatList.h”
void main(void)
{
FloatList list;
// Build the list
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
// Insert a node in the middle
// of the list.
list.insertNode(10.5);
// Dispay the list
list.displayList();
}
Sample Program 3 Output
2.5
7.9
10.5
12.6
28. The deleteNode function
void FloatList::deleteNode(float num)
{
ListNode *nodePtr, *previousNode;
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the
one.
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
Deleting Node
29. else
{
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is
// not equal to num.
while (nodePtr != NULL && nodePtr->value != num)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// Link the previous node to the node after
// nodePtr, then delete nodePtr.
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
30. Sample Program 4
// This program demonstrates the deleteNode member
function
#include <iostream.h>
#include "FloatList.h“
void main(void)
{
FloatList list;
// Build the list
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
cout << "Here are the initial values:n";
list.displayList();
cout << endl;
31. cout << "Now deleting the node in the
middle.n";
cout << "Here are the nodes left.n";
list.deleteNode(7.9);
list.displayList();
cout << endl;
cout << "Now deleting the last node.n";
cout << "Here are the nodes left.n";
list.deleteNode(12.6);
list.displayList();
cout << endl;
cout << "Now deleting the only remaining
node.n";
cout << "Here are the nodes left.n";
list.deleteNode(2.5);
list.displayList();
}
32. Program Output
Here are the initial values:
2.5
7.9
12.6
Now deleting the node in the middle.
Here are the nodes left.
2.5
12.6
Now deleting the last node.
Here are the nodes left.
2.5
Now deleting the only remaining node.
Here are the nodes left.