KEMBAR78
Single Linked List | PDF | Software Engineering | Computer Programming
0% found this document useful (0 votes)
8 views4 pages

Single Linked List

The document provides a C implementation of a singly linked list with basic operations such as insertion, deletion, display, and search. It includes functions to insert nodes at the beginning, end, or a specific position, as well as to delete nodes from those locations. A menu-driven main function allows users to interact with the linked list through console input.

Uploaded by

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

Single Linked List

The document provides a C implementation of a singly linked list with basic operations such as insertion, deletion, display, and search. It includes functions to insert nodes at the beginning, end, or a specific position, as well as to delete nodes from those locations. A menu-driven main function allows users to interact with the linked list through console input.

Uploaded by

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

Single Linked list to perform basic operations

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node *next;
};

struct Node *head = NULL;

// Function to create a new node


struct Node* createNode(int data) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Insert at the beginning


void insertAtBeginning(int data) {
struct Node *newNode = createNode(data);
newNode->next = head;
head = newNode;
}

// Insert at the end


void insertAtEnd(int data) {
struct Node *newNode = createNode(data);
if (head == NULL) {
head = newNode;
return;
}
struct Node *temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newNode;
}

// Insert at a specific position


void insertAtPosition(int data, int position) {
struct Node *newNode = createNode(data);
if (position == 1) {
newNode->next = head;
head = newNode;
return;
}
struct Node *temp = head;
for (int i = 1; i < position - 1 && temp != NULL; i++)
temp = temp->next;
if (temp == NULL) {
printf("Position out of range\n");
free(newNode);
} else {
newNode->next = temp->next;
temp->next = newNode;
}
}

// Delete from the beginning


void deleteFromBeginning() {
if (head == NULL) {
printf("List is empty\n");
return;
}
struct Node *temp = head;
head = head->next;
free(temp);
}

// Delete from the end


void deleteFromEnd() {
if (head == NULL) {
printf("List is empty\n");
return;
}
if (head->next == NULL) {
free(head);
head = NULL;
return;
}
struct Node *temp = head;
while (temp->next->next != NULL)
temp = temp->next;
free(temp->next);
temp->next = NULL;
}

// Delete from a specific position


void deleteFromPosition(int position) {
if (head == NULL) {
printf("List is empty\n");
return;
}
if (position == 1) {
struct Node *temp = head;
head = head->next;
free(temp);
return;
}
struct Node *temp = head;
for (int i = 1; i < position - 1 && temp->next != NULL; i++)
temp = temp->next;
if (temp->next == NULL) {
printf("Position out of range\n");
} else {
struct Node *toDelete = temp->next;
temp->next = toDelete->next;
free(toDelete);
}
}

// Display the linked list


void displayList() {
if (head == NULL) {
printf("List is empty\n");
return;
}
struct Node *temp = head;
printf("Linked List: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Search for an element in the linked list


void searchElement(int data) {
struct Node *temp = head;
int position = 1;
while (temp != NULL) {
if (temp->data == data) {
printf("Element %d found at position %d\n", data, position);
return;
}
temp = temp->next;
position++;
}
printf("Element %d not found in the list\n", data);
}

int main() {
int choice, data, position;

while (1) {
printf("\n--- Linked List Menu ---\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Position\n");
printf("4. Delete from Beginning\n");
printf("5. Delete from End\n");
printf("6. Delete from Position\n");
printf("7. Display List\n");
printf("8. Search Element\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter data to insert at beginning: ");
scanf("%d", &data);
insertAtBeginning(data);
break;
case 2:
printf("Enter data to insert at end: ");
scanf("%d", &data);
insertAtEnd(data);
break;
case 3:
printf("Enter data and position to insert: ");
scanf("%d %d", &data, &position);
insertAtPosition(data, position);
break;
case 4:
deleteFromBeginning();
break;
case 5:
deleteFromEnd();
break;
case 6:
printf("Enter position to delete: ");
scanf("%d", &position);
deleteFromPosition(position);
break;
case 7:
displayList();
break;
case 8:
printf("Enter element to search: ");
scanf("%d", &data);
searchElement(data);
break;
case 9:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice. Please try again.\n");
}
}
}

You might also like