PROGRAM-03: Linked List Date: 26-08-2025
#include<stdio.h>
#include<malloc.h>
struct node{
int val;
struct node* next;
};
struct node* head=NULL,*p,*q,*nw;
void ins_beg();
void ins_end();
void ins_pos();
void del_beg();
void del_end();
void del_pos();
void display();
int main(){
printf(“Krishna Shrivastava”\n24E51A6792\n");
int ch;
while(1){
printf("\nEnter 1 for insert at beginning\nEnter 2 for insert at end\nEnter 3
for insert at any position\nEnter 4 for delete at beginning\nEnter 5 for delete at
end\nEnter 6 for delete at any position\nEnter 7 for display\nEnter 8 for exit\n");
scanf("%d",&ch);
if(ch==1)
ins_beg();
else if(ch==2)
ins_end();
else if(ch==3)
ins_pos();
else if(ch==4)
del_beg();
else if(ch==5)
del_end();
else if(ch==6)
del_pos();
else if(ch==7)
display();
else if(ch==8)
break;
else
printf("Invalid Input\n");
return 0;
}
void ins_beg(){
int x;
printf("Enter x: ");
scanf("%d",&x);
nw=(struct node*)malloc(sizeof(struct node));
nw->val=x;
nw->next=NULL;
if(head==NULL)
head=nw;
else{
nw->next=head;
head=nw;
printf("Inserted %d at beginning\n",x);
void ins_end()
int x;
printf("Enter x: ");
scanf("%d",&x);
nw=(struct node*)malloc(sizeof(struct node));
nw->val=x;
nw->next=NULL;
if(head==NULL)
head=nw;
else{
p=head;
while(p->next!=NULL){
p=p->next;
p->next=nw;
printf("Inserted %d at end\n",x);
void ins_pos(){
int x,pos,c=0;
printf("Enter value and position: ");
scanf("%d%d",&x,&pos);
nw=(struct node*)malloc(sizeof(struct node));
nw->val=x;
nw->next=NULL;
if(head==NULL)
head=nw;
else
p=head;
while(c<pos-1 && p!=NULL){
q=p;
p=p->next;
c++;
q->next=nw;
nw->next=p;
printf("Inserted %d at position %d\n",x,pos);
void del_beg(){
if(head==NULL)
printf("List is empty\n");
else{
p=head;
head=head->next;
free(p);
printf("Value Deleted\n");
void del_end(){
if(head==NULL)
printf("List is empty\n");
else{
p=head;
while(p->next!=NULL){
q=p;
p=p->next;
q->next=NULL;
free(p);
printf("Value Deleted\n");
void del_pos(){
int pos,c=0;
printf("Enter position: ");
scanf("%d",&pos);
if(head==NULL)
printf("List is empty\n");
else{
p=head;
while(c<pos-1){
q=p;
p=p->next;
c++;
q->next=p->next;
free(p);
printf("Value Deleted\n");
void display(){
if(head==NULL)
printf("List is empty\n");
else{
p=head;
printf("The Linked List is\n: ");
while(p!=NULL){
printf("%d==>",p->val);
p=p->next;
printf("\n");
}
}