Doubly Linked List in C

Doubly Linked List in C

Here you will learn and get the program code of Doubly Linked List in C programming using Data Structure.

What is doubly linked list?

A doubly linked list in C is a fundamental data structure that involves a sequence of elements known as nodes. Each node carries both data and two pointers: one pointing to the previous node and another to the next node. It allowing for easy traversal and manipulation in both directions.

Doubly Linked List in C

A doubly linked list in C:

  • Each node has data and two pointers(previous node, and next node).
  • The prev pointer of the first node and the next pointer of the last node typically hold NULL.
  • In the middle, node points the previous node address and next node address.
  • Elements can be easily inserted or removed at the list’s beginning, end, or in the middle.
  • The two pointers offer advanced capabilities like seamless reverse traversal.

Doubly linked lists provide advantages such as enhanced flexibility compared to Singly linked lists. However, they consume slightly more memory due to the additional pointer in each node.

Difference between Singly and Doubly Linked list

Feature
Singly Link List Doubly Link List
Direction of Pointers Each node points to the next node (forward only). Each node points to both the next and previous nodes.
Memory Usage Requires less memory as it stores only one pointer. Requires more memory due to two pointers per node.
Traversal Efficiency Forward traversal is efficient; backward is not. Efficient for both forward and backward traversal.
Insertion/Deletion Easier to insert/delete at the beginning. Allows easy insertion/deletion at both ends and within.
Implementation Complexity Simpler to implement and requires less code. More complex to implement and requires more code.

Doubly Linked List in C

Output

Doubly Linked List Menu:
1. Create list
2. Insert at beginning
3. Insert at end
4. Insert in between
5. Display list
6. Delete a node
7. Reverse the list
8. Delete list
9. Exit
Enter your choice: 1
List created.

Doubly Linked List Menu:
1. Create list
2. Insert at beginning
3. Insert at end
4. Insert in between
5. Display list
6. Delete a node
7. Reverse the list
8. Delete list
9. Exit
Enter your choice: 2
Enter data to insert: 11
Data inserted at the beginning.

Doubly Linked List Menu:
1. Create list
2. Insert at beginning
3. Insert at end
4. Insert in between
5. Display list
6. Delete a node
7. Reverse the list
8. Delete list
9. Exit
Enter your choice: 2
Enter data to insert: 22
Data inserted at the beginning.

Doubly Linked List Menu:
1. Create list
2. Insert at beginning
3. Insert at end
4. Insert in between
5. Display list
6. Delete a node
7. Reverse the list
8. Delete list
9. Exit
Enter your choice: 2
Enter data to insert: 33
Data inserted at the beginning.

Doubly Linked List Menu:
1. Create list
2. Insert at beginning
3. Insert at end
4. Insert in between
5. Display list
6. Delete a node
7. Reverse the list
8. Delete list
9. Exit
Enter your choice: 2
Enter data to insert: 44
Data inserted at the beginning.

Doubly Linked List Menu:
1. Create list
2. Insert at beginning
3. Insert at end
4. Insert in between
5. Display list
6. Delete a node
7. Reverse the list
8. Delete list
9. Exit
Enter your choice: 3
Enter data to insert: 88
Data inserted at the end.

Doubly Linked List Menu:
1. Create list
2. Insert at beginning
3. Insert at end
4. Insert in between
5. Display list
6. Delete a node
7. Reverse the list
8. Delete list
9. Exit
Enter your choice: 4
Enter data to insert: 55
Enter value after which to insert: 44
Node inserted successfully.

Doubly Linked List Menu:
1. Create list
2. Insert at beginning
3. Insert at end
4. Insert in between
5. Display list
6. Delete a node
7. Reverse the list
8. Delete list
9. Exit
Enter your choice: 5
Doubly Linked List: 44 <-> 55 <-> 33 <-> 22 <-> 11 <-> 88 <-> NULL

Doubly Linked List Menu:
1. Create list
2. Insert at beginning
3. Insert at end
4. Insert in between
5. Display list
6. Delete a node
7. Reverse the list
8. Delete list
9. Exit
Enter your choice: 6
Enter data to delete: 55
Node deleted successfully.

Doubly Linked List Menu:
1. Create list
2. Insert at beginning
3. Insert at end
4. Insert in between
5. Display list
6. Delete a node
7. Reverse the list
8. Delete list
9. Exit
Enter your choice: 5
Doubly Linked List: 44 <-> 33 <-> 22 <-> 11 <-> 88 <-> NULL

Doubly Linked List Menu:
1. Create list
2. Insert at beginning
3. Insert at end
4. Insert in between
5. Display list
6. Delete a node
7. Reverse the list
8. Delete list
9. Exit
Enter your choice: 7
List reversed successfully.

Doubly Linked List Menu:
1. Create list
2. Insert at beginning
3. Insert at end
4. Insert in between
5. Display list
6. Delete a node
7. Reverse the list
8. Delete list
9. Exit
Enter your choice: 5
Doubly Linked List: 88 <-> 11 <-> 22 <-> 33 <-> 44 <-> NULL

Doubly Linked List Menu:
1. Create list
2. Insert at beginning
3. Insert at end
4. Insert in between
5. Display list
6. Delete a node
7. Reverse the list
8. Delete list
9. Exit
Enter your choice: 9
Exiting…..

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top