Linkedlist 1
Linkedlist 1
Learn and create a C program for below logic (Self learning and
practice)
1. Create a functions to create new linked list
2. add list at the front
3. add list at the back
4. Print all the data in the list
Code:
#include <stdio.h>
#include <stdlib.h>
// Main function to interact with the user and test the linked list functions
int main() {
struct Node* head = NULL; // Initialize an empty linked list
int choice, data;
while (1) {
// Display menu
printf("\nMenu:\n");
printf("1. Add a node at the front\n");
printf("2. Add a node at the back\n");
printf("3. Print all data in the list\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter data to add at the front: ");
scanf("%d", &data);
addFront(&head, data);
break;
case 2:
printf("Enter data to add at the back: ");
scanf("%d", &data);
addBack(&head, data);
break;
case 3:
printList(head);
break;
case 4:
printf("Exiting program.\n");
return 0;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}
return 0;
}
Output: