linkedlist
linkedlist
Fariha Tasneem
Roll:BBH2211023F
• Linked list is a linear data structure.
Data: It holds the actual value or data associated with the node.
Next Pointer: It stores the memory address (reference) of the next node
in the sequence.
Head and Tail: The linked list is accessed through the head node, which
points to the first node in the list. The last node in the list points to NULL
or nullptr, indicating the end of the list. This node is known as the tail
node
Why linked list data structure needed?
Deletion is also expensive with arrays until unless some special techniques
are used. For example, to delete 1010 in id[], everything after 1010 has to be
moved due to this so much work is being done which affects the efficiency of
the code.
There are mainly three types of linked lists
In a singly linked list, each node contains a reference to the next node in the sequence.
Traversing a singly linked list is done in a forward direction.
Double-linked list:
In a doubly linked list, each node contains references to both the next
and previous nodes. This allows for traversal in both forward and
backward directions, but it requires additional memory for the
backward reference.
Circular linked list:
In a circular linked list, the last node points back to the head node,
creating a circular structure. It can be either singly or doubly linked.
Create a single line linked list:
algorithm
1.Create a class Node which has two attributes: data and next. Next is a pointer to the
next node.
2.Create another class which has two attributes: head and tail.
3.addNode() will add a new node to the list:
a)Create a new node.
b) It first checks, whether the head is equal to null which means the list is empty.
c)If the list is empty, both head and tail will point to the newly added node.
d)If the list is not empty, the new node will be added to end of the list such that tail’s
next will point to the newly added node. This new node will become the new tail of
the list.
4)display() will display the nodes present in the list:
a)Define a node current which initially points to the head of the list.
b)Traverse through the list till current points to null.
c)Display each node by making current to point to node next to it in each iteration.
linked list operation
Traversal - access each element of the linked list