Linked List
Linked List
- one major problem with arrays is that the size of an array must be
specified precisely at the beginning, that is, its size cannot be altered later.
- so a different way of representing a list is to make each item in the list part
of a structure that also contains a ‘link’ to the structure containing the next
item.
- This type of list is called a linked list because it is a list whose order is
given by links from one item to the next.
- Each structure of the list is called a node and consists of two fields, one
containing the item, and the other containing the address of the next item in
the list.
The link is in the form of a pointer to another structure of the same type.
Such a structure is represented as follows:
struct node
{ int item;
struct node *next;
};
The first member is a data item and the second a pointer to the next node
in the list as shown below:
Definition: Structures that contain a member field that points to the same
structure type are called self-referential structures.
The structure may contain more than one item with different data types.
However, one of the items must be a pointer of the type tag-name.
Example for two nodes:
struct link_list
{
int age;
struct link_list *next;
}
The above statement creates space for two nodes each containing two
empty fields as shown below:
The next pointer of node1 can be made to point node2 by the statement
Note: ‘xxxx’ is the address of node2 where the value of the variable node2.
age will be stored.
would add another link provided node3 has been declared as a variable of
type struct link_list.
Every list must have an end. C has a special pointer value called null that
can be stored in the next field of the last node.
node2. next = 0;
The value of the age member of node2 can be accessed using the next
member of node1 as follows:
Example program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node {
int intData;
char strData[50];
double doubleData;
struct Node* next;
};
doubleValue) {
Node*)malloc(sizeof(struct Node));
newNode->intData = intValue;
strcpy(newNode->strData,
stringValue);
newNode->doubleData = doubleValue;
newNode->next = NULL;
return newNode;
}
void printList(struct Node* head) {
temp->strData, temp->doubleData);
temp = temp->next;
}
}
int main() {
current = current->next;
printList(head);
return 0;
}
Output:
Integer: 1, String: Node1, Double: 1.10
Integer: 2, String: Node2, Double: 2.20
Integer: 3, String: Node3, Double: 3.30
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int main() {
current->next = createNode(i);
current = current->next;
node9 = node9->next;
}
%d\n", node9->next->data);
return 0;
}
Output:
Data in the 10th node: 10
2. A linked list does not waste memory. It uses the memory that is just
needed for the list at any point of time.
3. The linked list provides flexibility in allowing the items to be rearranged
efficiently. It is easier to insert or delete items by rearranging the links
3. Linked list uses more storage. This is because each item has an
additional link field.
Difference between linked list and arrays:
1. Queue : If we restrict the process of insertions to one end of the list and
deletions to the other end, then we have a model of a queue. That is, we
can insert an item at the rear and remove an item at the front. This obeys
the discipline of “ first in, first out” (FIFO)