Singly Linked List Program-Bhoomi Patel
Singly Linked List Program-Bhoomi Patel
Prog: Write a program for singly linked list where nodes are inserted at the
front.
#include <stdio.h>
#include <stdlib.h>
struct node {
int info; // info
struct node *link; // Address
}*first,*newNode, *temp;
void insert_first(int x);
void display();
void main()
{
int x,ch;
do
{
printf("\n 1: Insert at first\n 2: Display\n 3:exit\n");
printf("Enter choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n\nEnter Data to insert at beginning of the list: ");
scanf("%d", &x);
insert_first(x);
break;
case 2: display();
break;
case 3:exit(0);
default: printf("Enter only 1 to 3\n");
}
}while(ch!=3);
}
void insert_first(int x)
{
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->info = x; // Link info part
newNode->link = first; // Link address part
void display()
{
int count=0;
if(first == NULL)
{
printf("List is empty.");
}
else
{
temp = first;
while(temp != NULL)
{
printf("---->| %d | %d | ", temp->info, temp);
temp = temp->link;
count++;
}
}
printf("\n Total nodes=%d",count);
}
Prog: Write a program for singly linked list where nodes are inserted at the last.
#include <stdio.h>
#include <stdlib.h>
struct node {
int info; // info
struct node *link; // Address
}*first,*newNode, *temp,*save;
void insert_last(int x);
void display();
void main()
{
int x,ch;
do
{
printf("\n 1: Insert at Last\n 2: Display\n 3:exit\n");
printf("Enter choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n\nEnter Data to insert at Last of the list: ");
scanf("%d", &x);
insert_last(x);
break;
case 2: display();
break;
case 3:exit(0);
default: printf("Enter only 1 to 3\n");
}
}while(ch!=3);
}
void insert_last(int x)
{
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->info = x;
newNode->link = NULL;
}
if(first==NULL)
{
first = newNode;
}
else
{
save=first;
while(save->link!=NULL)
{
save=save->link;
}
save->link=newNode;
}
printf("\nData INSERTED SUCCESSFULLY\n");
}
void display()
{
int count=0;
if(first == NULL)
{
printf("List is empty.");
}
else
{
temp = first;
while(temp != NULL)
{
Prog: Write a program for singly linked list where nodes are inserted in Ordered
List.
#include <stdio.h>
#include <stdlib.h>
struct node {
int info; // info
struct node *link; // Address
}*first,*newNode, *temp,*save;
void insert_order(int x);
void display();
void main()
{
int x,ch;
do
{
printf("\n 1: Insert in ordered list\n 2: Display\n 3:exit\n");
printf("Enter choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n\nEnter Data to insert in ordered list: ");
scanf("%d", &x);
insert_order(x);
break;
case 2: display();
break;
case 3:exit(0);
default: printf("Enter only 1 to 3\n");
}
}while(ch!=3);
}
void insert_order(int x)
{
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
newNode->info = x;
if(first==NULL)
{
newNode->link = NULL;
first = newNode;
}
else if(newNode->info<=first->info)
{
newNode->link=first;
first=newNode;
}
else
{
save=first;
while(save->link!=NULL&&(save->link)->info<=newNode->info)
{
save=save->link;
}
newNode->link=save->link;
save->link=newNode;
}
printf("\nData INSERTED SUCCESSFULLY\n");
}
void display()
{
int count=0;
if(first == NULL)
{
printf("List is empty.");
}
else
{
temp = first;
while(temp != NULL)
{
printf("| %d | %d |--->", temp->info, temp);
temp = temp->link;
count++;
}
}
printf("\n Total nodes=%d",count);
}
Prog: Write a program to delete a node from a given singly linked list.
#include <stdio.h>
#include <stdlib.h>
struct node {
int info; // info
struct node *link; // Address
}*first,*newNode, *temp,*save,*pred;
void insert_last(int x);
void display();
void delete(d);
void main()
{
int x,ch,d;
do
{
printf("\n 1: Insert at Last\n 2: Delete \n 3: Display\n 4:exit\n");
printf("Enter choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\n\nEnter Data to insert at Last of the list: ");
scanf("%d", &x);
insert_last(x);
break;
case 2: printf("\n\nEnter Data to delete from the list: ");
scanf("%d", &d);
delete(d);
break;
case 3: display();
break;
case 4:exit(0);
default: printf("Enter only 1 to 3\n");
}
} while(ch!=4);
void insert_last(int x)
{
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->info = x;
newNode->link = NULL;
}
if(first==NULL)
{
first = newNode;
}
else
{
save=first;
while(save->link!=NULL)
{
save=save->link;
}
save->link=newNode;
}
void display()
{
int count=0;
if(first == NULL)
{
printf("List is empty.");
}
else
{
temp = first;
while(temp != NULL)
{
printf("| %d | %d |--->", temp->info, temp);
temp = temp->link;
count++;
}
}
printf("\n Total nodes=%d",count);
}
void delete(int d)
{
if(first == NULL)
{
printf("list is empty");
return;
}
temp = first;
while(temp->info!=d && temp->link!=NULL)
{
pred = temp;
temp = temp->link;
}
if(temp->info!=d)
{
printf("node not found");
return;
}
else if(d==first->info)
{
first = first->link;
}
else
{
pred->link = temp->link;
}
printf("\nData deleted sucessfully");
free(temp);
}
Prog: Write a program to implement Stack Using Singly Linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
}*top,*newNode, *temp;
void push(int x);
void display();
void pop();
int main()
{
int x,ch;
do
{
printf("\n 1: push the data \n 2: Display\n 3: pop the data\n 4: exit\n");
printf("Enter choice=");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("push the data:\n");
scanf("%d",&x);
push(x);
break;
case 2: display();
break;
case 3: pop();
break;
case 4:exit(0);
default: printf("Enter only 1 to 4\n");
}
}while(ch!=4);
}
void push(int x)
{
newNode= (struct node*) malloc (sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
newNode->info=x;
newNode->link=top;
top=newNode;
return;
}
void pop()
{
if(top==NULL)
{
printf("our stack is underflow");
}
else
{
printf("deleted value:%d", top->info);
temp=top;
top=top->link;
}
free(temp);
}
void display()
{
temp=top;
if(temp==NULL)
{
printf("list is empty");
}
else{
while(temp!=NULL)
{
printf("%d\n",temp->info);
temp=temp->link;
}
}
}
Prog: Write a program to implement Queue Using Singly Linked list.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
}*f,*r,*newNode, *temp,*x;
int main()
{
int x,ch;
do
{
printf("\n 1: insert data \n 2: Display\n 3: delete the data\n 4: exit\n");
printf("Enter choice=");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("insert the data:\n");
scanf("%d",&x);
qinsert(x);
break;
case 2: display();
break;
case 3: qdelete();
break;
case 4:exit(0);
default: printf("Enter only 1 to 4\n");
}
}while(ch!=4);
}
void qinsert(int x)
{
newNode= (struct node*) malloc (sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
newNode->info=x;
newNode->link=NULL;
if(f==NULL)
{
f=r=newNode;
}
else{
r->link=newNode;
r=newNode;
}
}
void qdelete()
{
temp=f;
if(f==NULL)
{
printf("queue is underflow\n");
return;
}
else
{
printf("deleted value:%d\n", f->info);
f=f->link;
free(temp);
}
}
void display()
{
if(f==NULL && r==NULL)
{
printf("list is empty");
}
else
{
temp=f;
while(temp!=NULL)
{
printf("\t %d \t",temp->info);
temp=temp->link;
}
}
}