Practical # 9
/* Program to reverse a linked list */
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
/* structure containing a data part and link part */
struct node
{
int data;
struct node *link;
};
void addatbeg(struct node **,int);
void reverse(struct node **);
void display(struct node *);
int count(struct node *);
void main()
{
struct node *p;
p=NULL;
addatbeg(&p,7);
addatbeg(&p,43);
addatbeg(&p,17);
addatbeg(&p,3);
addatbeg(&p,23);
addatbeg(&p,5);
clrscr();
display(p);
printf("\nNo. of element in the linked list=%d",count(p));
reverse(&p);
display(p);
printf("\nNo. of elements in the linked list=%d",count(p));
}
/* adds a new node at the beginning of the linked list */
void addatbeg(struct node **q,int num)
{
struct node *temp;
/*add new node*/
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
temp->link=*q;
*q=temp;
}
void reverse(struct node **x)
{
struct node *q,*r,*s;
q=*x;
r=NULL;
/* traverse the entire linked list */
while(q!=NULL)
{
s=r;
r=q;
q=q->link;
r->link=s;
}
*x=r;
}
/*display the contents of the linked list */
void display(struct node *q)
{
printf("\n");
/*traverse the entire linked list */
while(q!=NULL)
{
printf("%d",q->data);
q=q->link;
}
}
/* counts the number of nodes present in the ll:*/
int count(struct node *q)
{
int c=0;
/* traverse the entire ll*/
while(q!=NULL)
{
q=q->link;
c++;
}
return c;
}
Output :-
5 23 3 17 43 7
No. of element in the linked list=6
7 43 17 3 23 5
No. of elements in the linked list=6
No comments :
Post a Comment