C program for inserting elements in circular Linkedlist
#include <stdio.h>
#include <stdlib.h>
//structure of node
struct node{
int data;
struct node *link;
};
//prototype declaration
void insert(struct node **,struct node **,int );
void display(struct node *);
int main(){
struct node *front;
struct node *rear;
front=rear=NULL;
insert(&front,&rear,10);
insert(&front,&rear,32);
insert(&front,&rear,30);
insert(&front,&rear,50);
insert(&front,&rear,42);
insert(&front,&rear,60);
display(front);
return 0;
}
//inserting elements in the circular linked list
void insert(struct node **f,struct node **r,int num){
struct node *temp;
//allocating memory for the temp pointer
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if((*f)==NULL){
*f=temp;
}
else
(*r)->link=temp;
//last element that is rear sholud point to the front element that is front ,point it's link to front *r=temp;
(*r)->link=*f;
}
//display the elements from front
void display(struct node *f){
struct node *p,*q;
p=NULL;
//q pointer points to the first element that is front
q=f;
while(q!=p){
printf("%d ",q->data);
q=q->link;
p=f;
}
}
Executing the program....
$demo
10 32 30 50 42 60
No comments:
Post a Comment