SINGLE LINKED LIST by smd
#include <stdio.h>
#include <stdlib.h>
int count = 0;
struct node {
int data;
struct node *next;
}
*head = NULL, *newnode, *trav;
void create_list() {
int value;
struct node *temp;
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter the value to be inserted: ");
scanf("%d", &value);
newnode->data = value;
newnode->next = NULL;
if (head == NULL) {
head = newnode;
} else {
temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newnode;
}
count++;
}
void insert_at_begin() {
int value;
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter the value to be inserted: ");
scanf("%d", &value);
newnode->data = value;
newnode->next = head;
head = newnode;
count++;
}
void insert_at_end() {
int value;
struct node *temp;
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter the value to be inserted: ");
scanf("%d", &value);
newnode->data = value;
newnode->next = NULL;
if (head == NULL) {
head = newnode;
} else {
temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newnode;
}
count++;
}
void insert_at_specific_point() {
int loc, value, i = 1;
struct node *temp;
if (count == 0) {
printf("List is empty. Use insert at beginning or end.\n");
return;
}
printf("Enter the position (1 to %d): ", count + 1);
scanf("%d", &loc);
if (loc < 1 || loc > count + 1) {
printf("Invalid position\n");
return;
}
printf("Enter the value to be inserted: ");
scanf("%d", &value);
newnode = (struct node *)malloc(sizeof(struct node));
newnode->data = value;
if (loc == 1) {
newnode->next = head;
head = newnode;
} else {
temp = head;
while (i < loc - 1) {
temp = temp->next;
i++;
}
newnode->next = temp->next;
temp->next = newnode;
}
count++;
}
void delete_at_begin() {
struct node *temp;
if (head == NULL) {
printf("There are no elements to delete\n");
return;
}
temp = head;
head = head->next;
printf("%d is deleted\n", temp->data);
free(temp);
count--;
}
void delete_at_end() {
struct node *temp, *prev;
if (head == NULL) {
printf("There are no elements to delete\n");
return;
}
temp = head;
if (temp->next == NULL) {
printf("%d is deleted\n", temp->data);
free(temp);
head = NULL;
} else {
while (temp->next != NULL) {
prev = temp;
temp = temp->next;
}
printf("%d is deleted\n", temp->data);
prev->next = NULL;
free(temp);
}
count--;
}
void delete_at_specific_point() {
struct node *temp, *nextnode;
int pos, i = 1;
if (head == NULL) {
printf("There are no elements to delete\n");
return;
}
printf("Enter the position to delete (1 to %d): ", count);
scanf("%d", &pos);
if (pos < 1 || pos > count) {
printf("Invalid position\n");
return;
}
if (pos == 1) {
delete_at_begin();
return;
}
if (pos == count) {
delete_at_end();
return;
}
temp = head;
while (i < pos - 1) {
temp = temp->next;
i++;
}
nextnode = temp->next;
temp->next = nextnode->next;
printf("%d is deleted\n", nextnode->data);
free(nextnode);
count--;
}
void display() {
trav = head;
if (trav
== NULL) {
printf("List is empty\n");
return;
}
printf("Elements in the list: ");
while (trav != NULL) {
printf("%d -> ", trav->data);
trav = trav->next;
}
printf("NULL\n");
}
void create_list();
void insert_at_begin();
void insert_at_end();
void insert_at_specific_point();
void delete_at_begin();
void delete_at_end();
void delete_at_specific_point();
void display();
int main() {
int choice = 0;
while (choice != 9) {
printf("\nChoose the options below\n");
printf("1. Create list\n");
printf("2. Insert at beginning\n");
printf("3. Insert at end\n");
printf("4. Insert at specific point\n");
printf("5. Delete at beginning\n");
printf("6. Delete at end\n");
printf("7. Delete at specific point\n");
printf("8. Display\n");
printf("9. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1: create_list(); break;
case 2: insert_at_begin(); break;
case 3: insert_at_end(); break;
case 4: insert_at_specific_point(); break;
case 5: delete_at_begin(); break;
case 6: delete_at_end(); break;
case 7: delete_at_specific_point(); break;
case 8: display(); break;
case 9: printf("Exiting... Bye.\n"); break;
default: printf("Invalid entry. Choose correct option\n"); break;
}
}
return 0;
}
Comments
Post a Comment