1. Program to create a node and display the contents of the node.
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
}*new=NULL,*head=NULL,*temp,*x,*d;
int val;
void display(){
int count=0;
if (head == NULL) {
printf("List is empty.\n");
return;
}
temp=head;
printf("\nElements in the list are:\n");
while(temp!=NULL)
{
count=count+1;
printf("%d->",temp->data);
temp=temp->next;
}
printf("NULL\n");
printf("\n Total number of nodes are =%d\n",count);
}
void insert_begin(){
new=(struct node *)malloc(sizeof (struct node));
printf("enter the value\n");
scanf("%d",&val);
new->data=val;
new->next=head;
head=new;
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
void insert_end(){
temp=head;
new=(struct node *)malloc(sizeof (struct node));
while(temp->next!=NULL)
temp=temp->next;
temp->next=new;
printf("enter the value\n");
scanf("%d",&val);
new->data=val;
new->next=NULL;
temp=new;
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
void insert_specifiedposition()
{
int pos,val;
new=(struct node *)malloc(sizeof (struct node));
temp=head;
printf("enter the position at which element to be inserted\n");
scanf("%d",&pos);
if (pos <= 0) {
printf("Invalid position. Position should be greater than 0.\n");
return;
}
printf("enter the element\n");
scanf("%d",&val);
for(int i=0;i<pos-1;i++)
temp=temp->next;
new->data=val;
new->next=temp->next;
temp->next=new;
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("Element is inserted at position %d",pos+1);
}
void deleteFromBeginning(){
if (head == NULL) {
printf("List is empty ,Deletion not possible\n");
return;
}
temp=head;
printf("Deleted node is %d",temp->data);
head=head->next;
temp->next=NULL;
free(temp);
}
void deleteFromEnd(){
if (head == NULL) {
printf("List is empty,Deletion Not Possible\n");
return;
}
temp=head;
while(temp->next->next!=NULL)
temp=temp->next;
d=temp->next;
printf("Deleted data is %d",d->data);
temp->next=NULL;
free(d);
}
void deleteAtSpecified(){
int pos;
if (head == NULL) {
printf("List is empty,Deletion Not Possible\n");
return;
}
temp=head;
printf("enter the position\n");
scanf("%d",&pos);
for(int i=0;i<pos-1;i++)
temp=temp->next;
d=temp->next;
printf("deleted node is %d",d->data);
temp->next=d->next;
d->next=NULL;
free(d);
}
void search(){
int flag=0,key;
printf("enter the key element\n");
scanf("%d",&key);
temp=head;
while(temp!=NULL)
{
if(key==temp->data)
{
flag=1;
break;
}
temp=temp->next;
}
if(flag==1)
printf("Key element is found\n");
else
printf("key element is not found\n");
}
int main()
{
int val;
char ch;
while(1)
{
int choice;
printf("\n1-Insert at the Beginning\t2-Insert at the End\t3-Insert at specified
position\n4-Display\n5-Delete the node from beginning\t6-Delete from end\t7-Delete at specified
position\t8-Search\t9-Exit\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:insert_begin();
break;
case 2:insert_end();
break;
case 3:insert_specifiedposition();
break;
case 4:display();
break;
case 5: deleteFromBeginning();
break;
case 6:deleteFromEnd();
break;
case 7:deleteAtSpecified();
break;
case 8:search();
break;
case 9:exit(0);
default:printf("Invalid choice\n");
break;
}
}
}
2. Singly Linked list program to perform following operations
1.Insert at Beginning
2.Insert at End
3.Insert at Specified position
4.Delet from beginning
5.Delet from End
6.Delet from Specified position.
7.Display the contents of the list and count number of nodes in the list
8.Search for a given key element in the list and print appropriate message.
9.Sort the elements in the List.
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
}*new=NULL,*head=NULL,*temp,*x,*d;
int val;
void display(){
int count=0;
if (head == NULL) {
printf("List is empty.\n");
return;
}
temp=head;
printf("\nElements in the list are:\n");
while(temp!=NULL)
{
count=count+1;
printf("%d->",temp->data);
temp=temp->next;
}
printf("NULL\n");
printf("\n Total number of nodes are =%d\n",count);
}
void insert_begin(){
new=(struct node *)malloc(sizeof (struct node));
printf("enter the value\n");
scanf("%d",&val);
new->data=val;
new->next=head;
head=new;
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
void insert_end(){
temp=head;
new=(struct node *)malloc(sizeof (struct node));
while(temp->next!=NULL)
temp=temp->next;
temp->next=new;
printf("enter the value\n");
scanf("%d",&val);
new->data=val;
new->next=NULL;
temp=new;
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
void insert_specifiedposition()
{
int pos,val;
new=(struct node *)malloc(sizeof (struct node));
temp=head;
printf("enter the position at which element to be inserted\n");
scanf("%d",&pos);
if (pos <= 0) {
printf("Invalid position. Position should be greater than 0.\n");
return;
}
printf("enter the element\n");
scanf("%d",&val);
for(int i=0;i<pos-1;i++)
temp=temp->next;
new->data=val;
new->next=temp->next;
temp->next=new;
temp=head;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("Element is inserted at position %d",pos+1);
}
void deleteFromBeginning(){
if (head == NULL) {
printf("List is empty ,Deletion not possible\n");
return;
}
temp=head;
printf("Deleted node is %d",temp->data);
head=head->next;
temp->next=NULL;
free(temp);
}
void deleteFromEnd(){
if (head == NULL) {
printf("List is empty,Deletion Not Possible\n");
return;
}
temp=head;
while(temp->next->next!=NULL)
temp=temp->next;
d=temp->next;
printf("Deleted data is %d",d->data);
temp->next=NULL;
free(d);
}
void deleteAtSpecified(){
int pos;
if (head == NULL) {
printf("List is empty,Deletion Not Possible\n");
return;
}
temp=head;
printf("enter the position\n");
scanf("%d",&pos);
for(int i=0;i<pos-1;i++)
temp=temp->next;
d=temp->next;
printf("deleted node is %d",d->data);
temp->next=d->next;
d->next=NULL;
free(d);
}
void search(){
int flag=0,key;
printf("enter the key element\n");
scanf("%d",&key);
temp=head;
while(temp!=NULL)
{
if(key==temp->data)
{
flag=1;
break;
}
temp=temp->next;
}
if(flag==1)
printf("Key element is found\n");
else
printf("key element is not found\n");
}
int main()
{
int val;
char ch;
while(1)
{
int choice;
printf("\n1-Insert at the Beginning\t2-Insert at the End\t3-Insert at specified
position\n4-Display\n5-Delete the node from beginning\t6-Delete from end\t7-Delete at
specified position\t8-Search\t9-Exit\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:insert_begin();
break;
case 2:insert_end();
break;
case 3:insert_specifiedposition();
break;
case 4:display();
break;
case 5: deleteFromBeginning();
break;
case 6:deleteFromEnd();
break;
case 7:deleteAtSpecified();
break;
case 8:search();
break;
case 9:exit(0);
default:printf("Invalid choice\n");
break;
}
}
}
3.Program to sort the list and display the sorted list.
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
}*new,*head=NULL,*temp;
void display(){
temp=head;
while(temp!=NULL){
printf("%d->",temp->data);
temp=temp->next;
}
}
void sort(){
int t;
struct node *t1,*t2;
for(t1=head;t1->next!=NULL;t1=t1->next)
{
for(t2=head;t2->next!=NULL;t2=t2->next)
{
if(t2->data > t2->next->data)
{
t=t2->data;
t2->data=t2->next->data;
t2->next->data=t;
}
}
}
printf("the elements after sorting are\n ");
display();
}
int main()
{
//struct node *head=NULL,*temp,*new;
int val,in,ch;
do{
new=(struct node *)malloc(sizeof(struct node));
printf("enter the node value\n");
scanf("%d",&val);
new->data=val;
new->next=NULL;
if(head==NULL)
{
head=new;
temp=new;
}
else{
temp->next=new;
temp=temp->next;
}
printf("Insert another node 1-yes/0-No");
scanf("%d",&in);
}while(in);
printf("1-Display\t2-Sort\t3-Exit");
while(1){
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch){
case 1:display();
break;
case 2:sort();
break;
case 3:exit(0);
break;
default: printf("Invalid choice\n");
}
}
}