0% found this document useful (0 votes)
14 views5 pages

Linkedlist 1

linked lit code
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Linkedlist 1

linked lit code
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Q.

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>

// Define a structure for a node in the linked list


struct Node {
int data; // Data part of the node
struct Node* next; // Pointer to the next node
};

// Function to create a new node with given data


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); //
Allocate memory for new node
newNode->data = data; // Assign data to the node
newNode->next = NULL; // Initialize next pointer to NULL
return newNode;
}
// Function to add a node at the front of the list
void addFront(struct Node** head, int data) {
struct Node* newNode = createNode(data); // Create a new node
newNode->next = *head; // Point new node's next to current head
*head = newNode; // Update head to new node
}

// Function to add a node at the back of the list


void addBack(struct Node** head, int data) {
struct Node* newNode = createNode(data); // Create a new node
if (*head == NULL) { // If list is empty, new node becomes the head
*head = newNode;
} else {
struct Node* temp = *head; // Temporary pointer to traverse the list
while (temp->next != NULL) {
temp = temp->next; // Move to the last node
}
temp->next = newNode; // Point the last node's next to new node
}
}

// Function to print all data in the list


void printList(struct Node* head) {
struct Node* temp = head; // Temporary pointer to traverse the list
printf("Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->data); // Print the data of each node
temp = temp->next; // Move to the next node
}
printf("NULL\n"); // End of list
}

// 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:

You might also like