1. C program to implement singly linked lists.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head=NULL;
int count=0;
void createlist(int n)
struct node *newnode, *temp;
for (int i=0;i<n;i++)
newnode = (struct node*)(malloc(sizeof(struct node)));
printf("\nEnter data for node %d: ",i+1);
scanf("%d", &newnode->data);
newnode->next=NULL;
if (head==NULL)
head=temp=newnode;
else
{
temp->next=newnode;
temp=newnode;
count++;
void display()
struct node *temp;
temp = head;
printf("\nThe elements in the linked list are:\n");
while (temp!=NULL)
printf("%d\n", temp->data);
temp=temp->next;
void insert_at_beginning()
struct node *newnode;
newnode = (struct node*)(malloc(sizeof(struct node)));
printf("\nEnter data to insert at the beginning: ");
scanf("%d", &newnode->data);
newnode->next=head;
head=newnode;
printf("\nThe inserted data is %d",newnode->data);
count++;
}
void insert_at_end()
struct node *newnode, *temp;
temp = head;
newnode = (struct node*)(malloc(sizeof(struct node)));
printf("\nEnter data to insert at the end: ");
scanf("%d", &newnode->data);
newnode->next=NULL;
while (temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
printf("\nThe inserted data is %d",newnode->data);
count++;
void insert_at_pos()
struct node *newnode, *temp;
int pos, val, i=1;
newnode = (struct node*)(malloc(sizeof(struct node)));
printf("\nEnter the position at which you want to insert the data: ");
scanf("%d", &pos);
if(pos<=0 || pos>(count+1))
printf("Invalid position");
else
{
if(pos==1){
insert_at_beginning();
else if(pos==count+1){
insert_at_end();
else
printf("\nEnter data to be inserted: ");
scanf("%d", &newnode->data);
while(i<pos-1)
temp=temp->next;
i++;
newnode->next=temp->next;
temp->next=newnode;
count++;
int main()
{
int n, choice, con;
printf("Enter the number of nodes you want to create for your list");
scanf ("%d", &n);
createlist(n);
display();
printf("choose 1 for insert at the beginning\n");
printf("choose 2 for insert at the end\n");
printf("choose 3 for insert at any position\n");
printf("choose 4 for dispay\n");
do {
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
case 1: insert_at_beginning();
break;
case 2: insert_at_end();
break;
case 3: insert_at_pos();
break;
case 4: display();
break;
default: printf("Exit\n");
printf("Press 1 to continue: ");
scanf("%d", &con);
}while (con==1);
return 0;