0% found this document useful (0 votes)
3 views

linked.c

The document contains a C program that implements a singly linked list with various operations such as inserting and deleting nodes at the front, rear, or a specified position, as well as displaying the list. The program includes function prototypes and definitions for each operation, along with a main function that provides a user interface for interacting with the linked list. The program is designed to run continuously until the user chooses to exit.

Uploaded by

bindiyap
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)
3 views

linked.c

The document contains a C program that implements a singly linked list with various operations such as inserting and deleting nodes at the front, rear, or a specified position, as well as displaying the list. The program includes function prototypes and definitions for each operation, along with a main function that provides a user interface for interacting with the linked list. The program is designed to run continuously until the user chooses to exit.

Uploaded by

bindiyap
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/ 8

#include <stdio.

h>
#include <stdlib.h>

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

// Function prototypes
struct Node* newNode(int data);
void Insertfront(struct Node** head, int data);
void insertrear(struct Node** head, int data);
void insertposition(struct Node** head, int data, int
position);
void deletefront(struct Node** head);
void deleterear(struct Node** head);
void deleteposition(struct Node **head, int position);
void display(struct Node* node);

int main() {
struct Node* head = NULL;
int choice, i, data, position;
printf("Program is excuted by Raghavendra S Patil
\n");

while (1) {
printf("\n1. Insert at front\t2. Insert at rear\n3. Insert
at position\t4. Delete at front \n5. Delete at rear\t6. Delete at
position\n7. Display\n8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter data: ");
scanf("%d", &data);
Insertfront(&head, data);
break;
case 2:
printf("Enter data: ");
scanf("%d", &data);
insertrear(&head, data);
break;
case 3:
printf("Enter data and position: ");
scanf("%d %d", &data, &position);
insertposition(&head, data, position);
break;
case 4:
deletefront(&head);
break;
case 5:
deleterear(&head);
break;
case 6:
printf("Enter position: ");
scanf("%d", &position);
deleteposition(&head, position);
break;
case 7:
display(head);
break;
case 8:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}

// Function definitions
struct Node* newNode(int data) {
struct Node* node = (struct
Node*)malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
return node;
}

void Insertfront(struct Node** head, int data) {


struct Node* node = newNode(data);
node->next = *head;
*head = node;
}

void insertrear(struct Node** head, int data) {


struct Node* node = newNode(data);
if (*head == NULL) {
*head = node;
return;
}
struct Node* last = *head;
while (last->next != NULL)
last = last->next;
last->next = node;
}

void insertposition(struct Node** head, int data, int


position) {
if (position == 1) {
Insertfront(head, data);
return;
}
int i = 1;
struct Node* current = *head;
while (i < position - 1 && current != NULL) {
current = current->next;
i++;
}
if (current == NULL) {
printf("Invalid position\n");
} else {
struct Node* node = newNode(data);
node->next = current->next;
current->next = node;
}
}

void deletefront(struct Node** head) {


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

void deleterear(struct Node** head) {


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

void deleteposition(struct Node **head, int position) {


if (position == 1) {
deletefront(head);
return;
}
struct Node* current = *head;
struct Node* prev = NULL;
int i = 1;
while (i < position && current != NULL) {
prev = current;
current = current->next;
i++;
}
if (current == NULL) {
printf("Invalid position\n");
} else {
prev->next = current->next;
free(current);
}
}

void display(struct Node* node) {


if (node == NULL) {
printf("List is empty\n");
return;
}
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}

You might also like