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
Example
Bradley Lord
Kristine
Anthony
Alleyah
Alleyah Anthony Bradley Kristine Lord
Moniquee
Moniquee
Ways to maintain a List in a Memory
Two ways
Array
1 2 3 4 5
Linked List
Data Addr Data Addr
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
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.
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
20 2000 35 3000 50 4000 70 NULL
The Address must be store on each node
1000
Head
Creating the Node of a Single Linked List
20 2000 35 3000 50 NULL
1000
struct node{
int data;
struct node *link;
};
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
Doubly Linked List
➢forward and backward navigation is possible.
20 2000 35 3000 50 Null
1000 Head pointer
1000 2000 3000
data
next
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;
}
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
❑ 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
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
Circular Doubly Linked List
Struct node
Struct node*prev;
Int data;
Struct node*next;
}
int main(){
int data = ______;
struct node*tail;
tail = circularDoubly(data);
cout<<tail ->data;
return 0;
}
struct node*circularDoubly(int data)
{
struct node*temp = malloc(sizeof(struct node));
temp ->data=data
temp->next= temp;
temp->prev= temp;
return temp;
}
100 2 100
100
100
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);
};
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.
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;
}
}
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);
}
➢ 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
void FloatList::displayList(void)
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
The pseudocode that represents the algorithm
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();
}
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.
// 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
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;
}
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;
} }
}
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
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
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;
}
}
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;
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();
}
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.

Lec-4_Linked-List (1).pdf

  • 1.
    LINKED LIST ➢ Alinked 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
  • 2.
  • 3.
    Ways to maintaina List in a Memory Two ways Array 1 2 3 4 5 Linked List Data Addr Data Addr
  • 4.
    Types of LinkedList ➢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 aSingle 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 SingleLinked 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 353000 50 4000 70 NULL The Address must be store on each node 1000 Head
  • 8.
    Creating the Nodeof 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{ intdata; 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 ➢forwardand backward navigation is possible. 20 2000 35 3000 50 Null 1000 Head pointer 1000 2000 3000 data next
  • 11.
    0 data nextprevi 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 Twotypes ❑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 DoublyLinked 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; Structnode*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
  • 15.
    Circular Doubly LinkedList Struct node Struct node*prev; Int data; Struct node*next; } int main(){ int data = ______; struct node*tail; tail = circularDoubly(data); cout<<tail ->data; return 0; } struct node*circularDoubly(int data) { struct node*temp = malloc(sizeof(struct node)); temp ->data=data temp->next= temp; temp->prev= temp; return temp; } 100 2 100 100 100
  • 16.
    Linked List Operations Wewill 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 Nodeto 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 // Thisprogram 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 displayListmember 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
  • 21.
    void FloatList::displayList(void) { ListNode *nodePtr; nodePtr= head; while (nodePtr) { cout << nodePtr->value << endl; nodePtr = nodePtr->next; } } The pseudocode that represents the algorithm
  • 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 Createa 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 nodePtrto 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(floatnum) { 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 voidFloatList::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 nodePtrto 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 << "Nowdeleting 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 arethe 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.