Javatpoint Logo
Javatpoint Logo

Generic Linked list in C

A linked list is a linear collection of data elements for whom the order is determined by one's physical location in memory rather than by their physical order. Rather, each element serves as a stepping stone to the next. It's a data structure made up of nodes that symbolize a sequence. So every node contains data and a reference (a link) to the next node in the sequence in its most basic form. During iteration, this structure enables efficient insertion and removal of elements from any position in the sequence. More complex variants include extra links, allowing for more efficient insertion and removal of nodes at arbitrary positions.

One disadvantage of linked lists is that access time is linear and difficult to the pipeline. It is not possible to provide faster access, such as random access. When compared to linked lists, arrays have better cache locality.

Linked lists are one of the most basic and widely used data structures. They can be used to incorporate a variety of other popular abstract data types, such as lists, stacks, queues, associative arrays, and S-expressions; however, it is not rare to incorporate those data structures directly without using a linked list as the foundation or underlying data structure.

The primary advantage of a linked list over a traditional array is that the list elements can be easily inserted or removed without requiring reallocation or reorganization of the entire structure because the data items do not need to be stored contiguously in memory or on disc, whereas restructuring an array at run-time is a much more costly operation. Linked lists allow the addition and removal of nodes at any point in the list with a constant number of operations by keeping the link preceding the link being added or removed in memory during list traversal. However, because simple linked lists do not support random access to data or efficient indexing, many basic operations, such as obtaining the last node of the list, locating a node that contains a given datum, or locating the location where a new node should be inserted, may necessitate iterating through most or all of the list elements. The benefits and drawbacks of using linked lists are listed below.

A dynamic array is a data structure that allocates all elements in memory in a contiguous manner and keeps track of the current number of elements. If the dynamic array's allocated space is exceeded, it is reallocated and (possibly) copied, which is an expensive operation. There are several advantages to using linked lists over dynamic arrays. Insertion or deletion of an element at a specific point of a list is a constant-time operation (unless we have already indexed a pointer to the node (before the one to be removed or before the insertion point). In contrast, insertion in a dynamic array at random locations will require moving half of the elements on average and all of the elements in the worst case. While it is possible to "delete" an element from an array in constant time by marking its slot as "vacant," this causes fragmentation that impedes iteration performance.

Depending upon the type of pointer, the nodes of a linked list have the direction of the pointers of the linked list nodes.

Types of Linked list

There are different types of linked lists, and some of the major types of linked lists are:

  • Singly Linked list
  • Doubly Linked list
  • Circular Linked list
  • Doubly Circular Linked list

Singly Linked list

In a singly linked list, the node of the singly linked list points to the address of the memory location where the next node is stored. Similarly, in this way, all the nodes are storing the address of their respective next nodes. Other than the last node, the address in the pointer of the last node is NULL representing it is the last node of the linked list.

Code

Now let's see a sample code to create a singly linked list, add elements in the list, and then traverse the singly linked list in the C programming language.

Output:

The above code gives the following output:

Please Choose one of the Operations::
1. To Insert Data in the Singly Linked list.
2. To Delete Data from the Singly Linked list.
3. To Display Data present in the Singly Linked list.
4. To Display the count of nodes in the Singly Linked list.
1
Enter Element for Insert Linked list: 
2
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Singly Linked list.
2. To Delete Data from the Singly Linked list.
3. To Display Data present in the Singly Linked list.
4. To Display the count of nodes in the Singly Linked list.
1
Enter Element for Insert Linked list: 
3
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Singly Linked list.
2. To Delete Data from the Singly Linked list.
3. To Display Data present in the Singly Linked list.
4. To Display the count of nodes in the Singly Linked list.
1
Enter Element for Insert Linked list: 
6
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Singly Linked list.
2. To Delete Data from the Singly Linked list.
3. To Display Data present in the Singly Linked list.
4. To Display the count of nodes in the Singly Linked list.
1
Enter Element for Insert Linked list: 
89
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Singly Linked list.
2. To Delete Data from the Singly Linked list.
3. To Display Data present in the Singly Linked list.
4. To Display the count of nodes in the Singly Linked list.
1
Enter Element for Insert Linked list: 
56
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Singly Linked list.
2. To Delete Data from the Singly Linked list.
3. To Display Data present in the Singly Linked list.
4. To Display the count of nodes in the Singly Linked list.
1
Enter Element for Insert Linked list: 
31
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Singly Linked list.
2. To Delete Data from the Singly Linked list.
3. To Display Data present in the Singly Linked list.
4. To Display the count of nodes in the Singly Linked list.
1 
Enter Element for Insert Linked list: 
78
Data Added Successfully.
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Singly Linked list.
2. To Delete Data from the Singly Linked list.
3. To Display Data present in the Singly Linked list.
4. To Display the count of nodes in the Singly Linked list.
3
Display Linked list: 
# 2 # # 3 # # 6 # # 89 # # 56 # # 31 # # 78 # 
No Of Items In Linked list: 7
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Singly Linked list.
2. To Delete Data from the Singly Linked list.
3. To Display Data present in the Singly Linked list.
4. To Display the count of nodes in the Singly Linked list.
2
No of Items In Linked list: 7
Display Linked list: 
Enter Position for Delete Element: 
4
Deleted Successfully 
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Singly Linked list.
2. To Delete Data from the Singly Linked list.
3. To Display Data present in the Singly Linked list.
4. To Display the count of nodes in the Singly Linked list.
3
Display Linked list: 
# 2 # # 3 # # 6 # # 56 # # 31 # # 78 # 
No Of Items In Linked list: 6
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Singly Linked list.
2. To Delete Data from the Singly Linked list.
3. To Display Data present in the Singly Linked list.
4. To Display the count of nodes in the Singly Linked list.
4
No of Items in Linked list: 6
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
n  

In the above code, we created different functions for different functionalities of the linked list like inserting a new node to the linked list, displaying the data present in all the nodes of the linked list, deleting a particular node present at a specific position in the linked list and counting the total number of nodes in the linked list.

While inserting a new node to the singly linked list, it is checked whether the first node is zero or not,

  • If the first node is zero, there are no nodes present in the singly linked list at that time, so that a new node is added at the start of the singly linked list, which will act as the head of the linked list.
  • If the first node is not zero, then the new node is added. After that already the last existing node is present in the linked list. For deleting a node from the linked list, first, we need to take the index of that node from the user. Once we have the position of that node next step is to traverse to that node, and after reaching that node, the previous node of the node that we want to delete is holding the address of this node so but we need to change it to the node of the next of that position. And for the printing of the data present in all the nodes, we need to traverse the whole list beginning from the head (first) node all up to the last node, and while traversing each node, the data present in that node is displayed. A similar approach is followed for printing the total count of the nodes in the linked list.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA