Write a program in C, to create and display Single Linked List
Question
Write a program in C, to create and display Single Linked List
Solution
Sure! Here is a step-by-step guide to create and display a single linked list in C:
Step 1: Define the structure for a node in the linked list. Each node should contain a data element and a pointer to the next node.
struct Node {
int data;
struct Node* next;
};
Step 2: Create a function to insert a new node at the beginning of the linked list. This function will take the data value as a parameter and return the updated head of the linked list.
struct Node* insertNode(struct Node* head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = head;
head = newNode;
return head;
}
Step 3: Create a function to display the linked list. This function will traverse the linked list and print the data value of each node.
void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
Step 4: In the main function, create the head pointer and insert nodes into the linked list using the insertNode function. Finally, display the linked list using the displayList function.
int main() {
struct Node* head = NULL;
// Insert nodes into the linked list
head = insertNode(head, 5);
head = insertNode(head, 10);
head = insertNode(head, 15);
// Display the linked list
displayList(head);
return 0;
}
This program will create a single linked list with three nodes containing the data values 15, 10, and 5. It will then display the linked list in reverse order, i.e., 5 10 15.
Similar Questions
Write a program in C, to create a link list and perform operation such as inserting or deleting a node from list
C program to performs all operations on singly linked listWrite a program that uses functions to perform the following operations on singly linked listi) Creation ii) Insertion iii) Deletion iv) TraversalSample Test CasesTest Case 1:Expected Output:Singly·Linked·List·Example·-·All·OperationsOptions1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·111Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·222Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·333Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·1Enter·elements·for·inserting·into·linked·list·:·444Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·3The·elements·in·the·linked·list·are·:·111·222·333·444·Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·2Enter·position·of·the·element·for·deleteing·the·element·:·2Deleted·successfullyOptions1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·3The·elements·in·the·linked·list·are·:·111·333·444·Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·4No·of·elements·in·the·linked·list·are·:·3Options1·:·Insert·elements·into·the·linked·list2·:·Delete·elements·from·the·linked·list3·:·Display·the·elements·in·the·linked·list4·:·Count·the·elements·in·the·linked·list5·:·Exit()Enter·your·option·:·5
What is the output of the following function for a single linked list with the values1 -> 2 -> 3 -> 4 -> 5-> 6? start is pointing to the first node in the list.void fun(struct node *start){if (start == NULL) return;printf(“%d”, start->data);if (start-> next != NULL){fun (start-> next -> next);printf(“%d”, start->data);}}
Write a C program to reverse a single linked list recursively. #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; // Write a function to create a new node with the given data // Write a function to print the linked list // Write a function to reverse the linked list recursively // Write your main function here
Given the head pointers of two linked lists, add them and return the new linked list. Each linked list represents an integer number (each node is a digit).
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.