Different operation on LINK LIST:
'/' is used instead of '<' and '>'.
#include/stdio.h/
#include/conio.h/
#include/alloc.h/
struct node
{
int data;
struct node *link;
}*start;
void append(struct node **,int);
void addatbeg(struct node **,int);
void addafter(struct node *,int,int);
void reverse(struct node **);
void getdata();
void display(struct node *);
void main()
{
struct node *p;
clrscr();
p=NULL;
append(&p,2);
append(&p,25);
append(&p,8);
display(p);
addatbeg(&p,10);
addatbeg(&p,15);
display(p);
addafter(p,4,20);
display(p);
reverse(&p);
display(p);
getdata();
getch();
}
void append(struct node **q,int num)
{
struct node *temp,*r;
if(*q==NULL)
{
temp=malloc(sizeof(struct node));
temp->data=num;
temp->link=NULL;
*q=temp;
}
else
{
temp=*q;
while(temp->link!=NULL)
temp=temp->link;
r=malloc(sizeof(struct node));
r->data=num;
r->link=NULL;
temp->link=r;
}
}
void addatbeg(struct node **q,int num)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->data=num;
temp->link=*q;
*q=temp;
}
void addafter(struct node *q,int loc,int num)
{
struct node *temp,*r;
int i;
temp=q;
for(i=0;i
if(temp==NULL)
{
printf("\nthere are less than %d elements in list",loc);
return;
}
}
r=malloc(sizeof(struct node));
r->data=num;
r->link=temp->link;
temp->link=r;
}
void display(struct node *q)
{
printf("\n");
while(q!=NULL)
{
printf("%d\t",q->data);
q=q->link;
}
}
void reverse(struct node **q)
{
struct node *t,*r,*s;
t=*q;
r=NULL;
while(t!=NULL)
{
s=r;
r=t;
t=t->link;
r->link=s;
}
*q=r;
}
void getdata()
{
int val,n;
char ch;
struct node *new;
new=NULL;
do
{
printf("\nenter a value");
scanf("%d",&val);
append(&new,val);
display(new);
printf("any more nodes(Y/N)");
ch=getche();
}while(ch=='y' || ch=='Y');
start=new;
}
0 comments:
Post a Comment