Using C language to write an algorithm that accepts a list i
Using C language to write an algorithm that accepts a list implemented using a linked list traverses it, and returns the data in the node with the smallest key value.
Thank you very much
Solution
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *head,*x,*y,*z;
void main()
{
void create();
void insbeg();
void insmid();
void insend();
void delbeg();
void delmid();
void delend();
void display();
int ch;
clrscr();
while(1)
{
printf(\"\ 1.Creation\");
printf(\"\ 2.Insertion at beginning\");
printf(\"\ 3.Insertion at middle\");
printf(\"\ 4.Insertion at end\");
printf(\"\ 5.Deletion at beginning\");
printf(\"\ 6.Deletion at middle\");
printf(\"\ 7.Deletion at end\");
printf(\"\ 8.Display\");
printf(\"\ 9.Exit\");
printf(\"\ Enter ur choice:\");
scanf(\"%d\",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insbeg();
break;
case 3:
insmid();
break;
case 4:
insend();
break;
case 5:
delbeg();
break;
case 6:
delmid();
break;
case 7:
delend();
break;
case 8:
display();
break;
default:
exit(0);
}
}
}
void create()
{
int c;
head=NULL;
x=(struct node*)malloc(sizeof(struct node));
printf(\"\ Enter the data:\");
scanf(\"%d\",&x->data);
x->link=NULL;
head=x;
printf(\"\ Do u wish to continue press 1 otherwise 0:\");
scanf(\"%d\",&c);
while(c!=0)
{
y=(struct node*)malloc(sizeof(struct node));
printf(\"\ Enter the data:\");
scanf(\"%d\",&y->data);
y->link=NULL;
x->link=y;
x=y;
printf(\"\ Do u wish to continue press 1 otherwise 0:\");
scanf(\"%d\",&c);
}
}
void insbeg()
{
y=(struct node*)malloc(sizeof(struct node));
printf(\"\ Enter the data:\");
scanf(\"%d\",&y->data);
y->link=head;
head=y;
}
void insmid()
{
int pos,c=1;
y=(struct node*)malloc(sizeof(struct node));
printf(\"\ Enter the data:\");
scanf(\"%d\",&y->data);
printf(\"\ Enter the position to be inserted:\");
scanf(\"%d\",&pos);
x=head;
while(c<pos)
{
z=x;
x=x->link;
c++;
}
y->link=x;
z->link=y;
}
void insend()
{
y=(struct node*)malloc(sizeof(struct node));
printf(\"\ Enter the data:\");
scanf(\"%d\",&y->data);
y->link=NULL;
x=head;
while(x->link!=NULL)
{
x=x->link;
}
x->link=y;
}
void delbeg()
{
if(head==NULL)
printf(\"\ List is empty\");
else
{
x=head;
head=x->link;
free(x);
}
}
void delmid()
{
int pos,c=1;
if(head==NULL)
printf(\"\ List is empty\");
else
{
printf(\"\ Enter the position to be deleted:\");
scanf(\"%d\",&pos);
x=head;
while(c<pos)
{
z=x;
x=x->link;
c++;
}
z->link=x->link;
free(x);
}
}
void delend()
{
if(head==NULL)
printf(\"\ List is empty\");
else
{
x=head;
while(x->link!=NULL)
{
y=x;
x=x->link;
}
y->link=NULL;
free(x);
}
}
void display()
{
if(head==NULL)
printf(\"\ List is empty\");
else
{
x=head;
printf(\"\ List elements are \ \");
while(x->link!=NULL)
{
printf(\"%d->\",x->data);
x=x->link;
}
printf(\"%d\",x->data);
}
}





