This C program implements a singly linked list data structure to manage a list of integers. The user can perform various operations on the linked list, including inserting elements at the beginning, in the middle, and at the end, as well as displaying the list.
- Compile the program using a C compiler.
- Run the compiled executable.
- Follow the on-screen instructions to interact with the linked list.
The C code defines a structure coNode
to represent each node in the singly linked list. It contains two fields:
value
: An integer to store the data of the node.nextvalue
: A pointer to the next node in the linked list.
The program provides the following functions to manipulate the linked list:
insertAtBeginning(struct coNode **head_ref, int new_data)
: Inserts a new node with the given data at the beginning of the linked list.insertAfter(struct coNode *prev_node, int new_data)
: Inserts a new node with the given data after the specified previous node in the linked list.insertAtEnd(struct coNode **head_ref, int new_data)
: Inserts a new node with the given data at the end of the linked list.printList(struct coNode *node)
: Displays the elements of the linked list along with the total number of nodes.
The main
function provides a simple user interface to perform operations on the linked list. Users can choose to insert elements at the beginning, in the middle, or at the end of the list, and also display the list.
insertAtBeginning(struct coNode **head_ref, int new_data)
: Inserts a new node with the given data at the beginning of the linked list.insertAfter(struct coNode *prev_node, int new_data)
: Inserts a new node with the given data after the specified previous node in the linked list.insertAtEnd(struct coNode **head_ref, int new_data)
: Inserts a new node with the given data at the end of the linked list.printList(struct coNode *node)
: Displays the elements of the linked list along with the total number of nodes.
Please make sure to handle memory deallocation (freeing) appropriately, especially in a larger implementation, to prevent memory leaks. This simple example code does not include memory deallocation for brevity.
- Name: Om Shingare
- GitHub Profile: ShingareOm