Lecture-Linked List
Lecture-Linked List
• It is a list or collection of data items that can be stored in scattered locations (positions) in
memory.
• To store data in scattered locations in memory we have to make link between one data item
to another. So, each data item or element must have two parts: one is data part another is
link (pointer) part.
• Each data item of a linked list is called a node.
• Data part contains (holds) actual data (information) and the link part points to the next node
of the list.
• To locate the list an external pointer is used that pints the first node of the list. The link part
of the last node will not point to any node. So, it will be null. This type of list is called
linear (one way) linked list or simply linked list.
Suppose next node is allocated with an address with an address 10,s the list become,
In the above representation the data stored in the linked list is “INDIA”, the information part of
each node contains one character. The external pointer root points to first node’s address 1005.
The link part of the node containing information I contains 1007, the address of next node. The
last node of the list contains an address 0, the invalid address or NULL address.
Example:
The data stored in the linked list is: “The Is Another: Example Of Linked List”
Create a node
struct node
{
node *back;
int data;
node *next;
};
node *nptr;
nptr = new(node);
nptr -> back = NULL;
nptr -> data = item;
nptr -> next = NULL;
Algorithm (pseudo code) to create a doubly linked list
4. Make link between the last node of the list and the new node:
if (list==NULL)
{
list = nptr;
tptr = nptr;
}
else
{
tptr -> next = nptr;
nptr -> back = tptr;
tptr = nptr;
}
• An array is the data structure that contains a collection of similar type data elements
whereas the Linked list is considered as non-primitive data structure contains a collection
of unordered linked elements known as nodes.
• Random access is possible in array but in linked list, random access is not allowed
• Accessing an element in an array is fast, while Linked list takes linear time, so it is quite
a bit slower.
• Operations like insertion and deletion in arrays consume a lot of time. On the other hand,
the performance of these operations in Linked lists is fast.
• Arrays are of fixed size. In contrast, Linked lists are dynamic and flexible and can expand
and contract its size.
• In an array, memory is assigned during compile time while in a Linked list it is allocated
during execution or runtime.
• Elements are stored consecutively in arrays whereas it is stored randomly in Linked lists.
• The requirement of memory is less due to actual data being stored within the index in the
array. As against, there is a need for more memory in Linked Lists due to storage of
additional next and previous referencing elements.
• In addition memory utilization is inefficient in the array. Conversely, memory utilization
is efficient in the linked list.