WEEK-1
1. Write a program that uses functions to perform the following operations on singly linked
list:
i. Creation
ii. Insertion
iii. Deletion
iv. Traversal
#include <stdio.h>
#include <stdlib.h>
/* Structure of a node */
struct node
{
int data;
struct node *next;
};
struct node *head = NULL;
struct node *newnode;
/* Create a new node */
void create()
{
newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter the data for new node: ");
scanf("%d", &newnode->data);
newnode->next = NULL;
}
/* Insert at beginning */
void insert_begin()
{
create();
newnode->next = head;
WEEK-1
head = newnode;
printf("Node inserted at beginning\n");
}
/* Insert at end */
void insert_end()
{
struct node *temp;
create();
if (head == NULL)
{
head = newnode;
}
else
{
temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = newnode;
}
printf("Node inserted at end\n");
}
/* Insert at position */
void insert_position()
{
int pos, i;
struct node *temp;
WEEK-1
create();
printf("Enter position: ");
scanf("%d", &pos);
if (pos == 1)
{
newnode->next = head;
head = newnode;
return;
}
temp = head;
for (i = 1; i < pos - 1 && temp != NULL; i++)
temp = temp->next;
if (temp == NULL)
{
printf("Invalid position\n");
free(newnode);
}
else
{
newnode->next = temp->next;
temp->next = newnode;
printf("Node inserted at position %d\n", pos);
}
}
/* Delete from beginning */
void delete_begin()
WEEK-1
{
struct node *temp;
if (head == NULL)
{
printf("List is empty\n");
return;
}
temp = head;
head = head->next;
free(temp);
printf("Node deleted from beginning\n");
}
/* Delete from end */
void delete_end()
{
struct node *temp, *prev;
if (head == NULL)
{
printf("List is empty\n");
return;
}
if (head->next == NULL)
{
free(head);
head = NULL;
WEEK-1
}
else
{
temp = head;
while (temp->next != NULL)
{
prev = temp;
temp = temp->next;
}
prev->next = NULL;
free(temp);
}
printf("Node deleted from end\n");
}
/* Delete from position */
void delete_position()
{
struct node *temp, *prev;
int pos, i;
if (head == NULL)
{
printf("List is empty\n");
return;
}
printf("Enter position: ");
scanf("%d", &pos);
WEEK-1
if (pos == 1)
{
temp = head;
head = head->next;
free(temp);
printf("Node deleted from position %d\n", pos);
return;
}
temp = head;
for (i = 1; i < pos && temp != NULL; i++)
{
prev = temp;
temp = temp->next;
}
if (temp == NULL)
{
printf("Invalid position\n");
}
else
{
prev->next = temp->next;
free(temp);
printf("Node deleted from position %d\n", pos);
}
}
/* Display the list */
void display()
WEEK-1
{
struct node *temp = head;
if (head == NULL)
{
printf("List is empty\n");
return;
}
printf("Linked List: ");
while (temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
/* Main function */
int main()
{
int choice;
while (1)
{
printf("\n--- Singly 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");
WEEK-1
printf("5. Delete from End\n");
printf("6. Delete from Position\n");
printf("7. Display\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1: insert_begin(); break;
case 2: insert_end(); break;
case 3: insert_position(); break;
case 4: delete_begin(); break;
case 5: delete_end(); break;
case 6: delete_position(); break;
case 7: display(); break;
case 8: exit(0);
default: printf("Invalid choice\n");
}
}
return 0;
}