Experiment 17
To use excepttion handling.
VS Code
Definition A linked list is a fundamental data structure used to store a collection of elements. Unlike arrays, where elements are stored in contiguous memory locations, linked lists store elements in nodes scattered throughout memory. Each node contains two parts:
- Data: The actual value of the element.
- Pointer (or Reference): A reference (or pointer) to the next node in the list.
- Singly Linked List: Each node points to the next node in the sequence, and the last node points to
NULL
. - Doubly Linked List: Each node points to both the next and the previous node. This allows traversal in both directions.
- Circular Linked List: The last node points back to the first node, forming a circle. It can be singly or doubly linked.
Eg-
struct Node {
int data;
Node* next;
};
Here, data stores the value of the element, and next is a pointer to the next node in the list.
- At the beginning: Insert a node at the head of the list.
- At the end: Traverse the list and insert a node at the tail.
- At a specific position: Insert a node at a given position.
- From the beginning: Remove the head node.
- From the end: Traverse to the second-last node and set its
next
pointer toNULL
. - From a specific position: Remove the node at a given index.
// Function to insert a node at the beginning
void insertAtBeginning(Node*& head, int newData) {
Node* newNode = new Node();
newNode->data = newData;
newNode->next = head;
head = newNode;
}
// Function to insert a node at the end
void insertAtEnd(Node*& head, int newData) {
Node* newNode = new Node();
newNode->data = newData;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
- Create a class
Link
to represent a node of the linked list. - Inside the class:
- Declare an integer
data
to store the value of the node. - Declare a pointer
next
of typeLink*
to store the address of the next node.
- Declare an integer
- Create a constructor
Link(int num)
that takes an integer parameter:- Set
data
equal tonum
(the value passed during object creation). - Initialize
next
toNULL
, meaning that the current node doesn’t point to any other node.
- Set
- In the
main
function:- Declare a pointer
l1
of typeLink*
and dynamically allocate memory for it usingnew
. - Initialize the node with the value
6
by calling the constructor.
- Declare a pointer
- Use
cout
to print the value of the node (l1->data
). - Print the address stored in
next
usingl1->next
, which should beNULL
for this node.
- Return
0
to indicate successful completion of the program.
- Create a class named
Node
to represent each node in the linked list. - Inside the class:
- Declare an integer
value
to store the value of the node. - Declare a pointer
next
of typeNode*
to point to the next node in the list.
- Declare an integer
- Define a constructor
Node(int data)
:- Set
value
to the parameterdata
. - Initialize
next
toNULL
, indicating no next node.
- Set
- Define a function
insertAtHead(Node* &head, int value)
to insert a new node at the beginning of the list:- Create a new
Node
object namednew_node
and initialize it with thevalue
. - Set the
next
pointer ofnew_node
to the currenthead
. - Update
head
to point tonew_node
.
- Create a new
- Define a function
display(Node* head)
to print the elements of the linked list:- Initialize a temporary pointer
temp
tohead
. - While
temp
is notNULL
, do the following:- Print the
value
of the current node followed by " -> ". - Move
temp
to the next node (temp = temp->next
).
- Print the
- After the loop, print "NULL" to indicate the end of the list.
- Initialize a temporary pointer
- In the
main
function:- Declare a pointer
head
of typeNode*
and initialize it toNULL
to represent an empty list. - Call
insertAtHead(head, 4)
to insert the value4
at the head and display the list. - Call
insertAtHead(head, 5)
to insert the value5
at the head and display the list again. - Call
insertAtHead(head, 10)
to insert the value10
at the head and display the list one final time.
- Declare a pointer
- Return
0
from themain
function to indicate successful completion of the program.
We learnt the use of linked list in this experiment.