C/C++ Programes

Q! write a programe to print the link list , delete it, add node at end and at begining and sort the link list


#include
#include
#include

typedef struct
{ int info;
struct node*next;
}node;

typedef struct
{
node *first;
}list;

void addnode(int n,list* l);
void endnode(int n,list *l);
void addmid(int val,int pos,list *l);
void print(list *l);
void dellistnode(list *l);
void delallnode(list *l);
void sort(list *l);
list *make_list(void);

void main()
{
list *p;
node *m;
clrscr();
p=make_list();
// m=p->first;

addnode(10,p);
addnode(40,p);
addnode(50,p);
addnode(25,p);
//endnode(20,p);
print(p);
//addmid(30,1,p);
//sort(p);
printf("list is\n");
print(p);

printf("\n\n");

//dellistnode(p);
sort(p);

print(p);

printf("\n");
//delallnode(p);
// printf("list is deleted");
getch();
}

//Create the list

list *make_list()
{
list *l;
l=(list*)malloc(sizeof(list));
l->first=NULL;
return l;
}

// add node at the end

void addnode(int n,list *l)
{
node *n1;
n1=(node*)malloc(sizeof(node));
n1->info=n;
if(l->first==NULL)
{
l->first=n1;
n1->next=NULL;
}
else
{n1->next=l->first;
l->first=n1;
}
}



/////////////////////////////

// function for print the list in sorted list

void endnode(int n,list *l)
{
node *n1,*k;
n1=(node*)malloc(sizeof(node));
n1->info=n;
if(l->first==NULL)
{
l->first=n1;
n1->next=NULL;
}
else
{
k=l->first;
while(k->next!=NULL)
{
k=k->next;
}
k->next=n1;
n1->next=NULL;
}
}

//print the delete all the node

void dellistnode(list *l)
{
node *n1;
n1=l->first;
l->first=n1->next;
free(n1);
}



/////////////////////////////

// function for delete all node

void delallnode(list *l)
{
node *p,*temp;
p=l->first;
while(p!=NULL)
{
temp=p->next;
free(p);
p=temp;
}
free(l);
}
void print(list *l)
{
node *p,*temp;
p=l->first;
while(p)
{
printf("%d",p->info);
p=p->next;
}
}



/////////////////////////////

// function for print the list in add at midle value

void addmid(int val,int pos,list *l)
{
int i;
node *n1,*p;
n1=(node*)malloc(sizeof(node));
p=l->first;
n1->info=val;
for(i=0;inext;
n1->next=p->next;
p->next=n1;
}

/////////////////////////////

// function for print the list in sorted list

void sort(list *l)
{
node *p1,*p2,*p3;
int j;
p1=l->first;
p2=l->first->next;
p3=p2->next;
for(;p1->next!=NULL;p1=p1->next)
{
if(p2->info < p3->info)
{
p2->next=p3->next;
p3->next=p2;
p1->next=p3;
p1=p3;
p3=p2->next;
}
else
{
p1=p2;
p2=p3;
p3=p3->next;
}
}
}
===========================================
===========================================