You are on page 1of 19

#include <stdio.

h>
// Structure Declaration
struct stud_record
{
char name[20];
int roll_no;
struct stud_record * ptr;
};
int main()
{
int i;
struct stud_record stud[3];
printf("*** Circular Singly Linked List ***\n");
//every node pointing to next to next node with last one pointing to first
stud[0].ptr=&stud[1];
stud[1].ptr=&stud[2];
stud[2].ptr=&stud[0];
//data from user for every node
for(i=0;i<3;i++)
{
printf("Node %d :\n",i+1);
printf("Enter Student %d Name : ",i+1);
scanf("%s",stud[i].name);
printf("Enter Student %d Roll No : ",i+1);
scanf("%d",&stud[i].roll_no);
}
// Details of every node
printf("\nRoll No
Name
Address
Pointer Value\n");
for(i=0;i<3;i++)
{
printf("%-10d%-12s%-12p%p\n",stud[i].roll_no,
stud[i].name,&stud[i],stud[i].ptr);
}
return 0;
}

#include <stdio.h>
// Structure Declaration
struct stud_record
{
char name[20];
int roll_no;
struct stud_record * ptr;
};
int main()
{
int n,i;
struct stud_record stud[4];
printf("*** Adding a node to Circular Singly Linked List ***\n");
//every node pointing to next to next node with last one pointing to first
stud[0].ptr=&stud[1];
stud[1].ptr=&stud[2];
stud[2].ptr=&stud[0];
//data from user for every node
for(i=0;i<3;i++)
{
printf("Node %d :\n",i+1);
printf("Enter Student %d Name : ",i+1);
scanf("%s",stud[i].name);
printf("Enter Student %d Roll No : ",i+1);
scanf("%d",&stud[i].roll_no);
}
// Details of every node
printf("\nRoll No
Name
Address
Pointer Value\n");
for(i=0;i<3;i++)
{
printf("%-10d%-12s%-12p%p\n",stud[i].roll_no, stud[i].name,
&stud[i],stud[i].ptr);
}
//Data from user for new node
printf("Node 4 :\n");
printf("Enter Student %d Name : ",4);
scanf("%s",stud[3].name);

printf("Enter Student%d Roll No : ",4);


scanf("%d",&stud[3].roll_no);
printf("Enter Node position for new student: ");
scanf("%d",&n);
if(n==1 || n==4 )
{
stud[3].ptr=&stud[0];
stud[2].ptr=&stud[3];
}
else
{
stud[3].ptr=&stud[n-1];
stud[n-2].ptr=&stud[3];
}
//Data for every node including new one
printf("\nRoll No
Name
Address
Pointer Value\n");
for(i=0;i<=3;i++)
{
printf("%-10d%-12s%-12p%p\n",stud[i].roll_no,stud[i].name,
&stud[i],stud[i].ptr);
}
return 0;
}

#include <stdio.h>
// Structure Declaration
struct stud_record
{
char name[20];
int roll_no;
struct stud_record * ptr;
};
int main()
{
int n,i;
struct stud_record stud[3];
printf("*** Deleting a node in Circular Singly Linked List ***\n");
//every node pointing to next to next node with last one pointing to first
stud[0].ptr=&stud[1];
stud[1].ptr=&stud[2];
stud[2].ptr=&stud[0];
//data from user for every node
for(i=0;i<3;i++)
{
printf("Node %d :\n",i+1);
printf("Enter Student %d Name : ",i+1);
scanf("%s",stud[i].name);
printf("Enter Student %d Roll No : ",i+1);
scanf("%d",&stud[i].roll_no);
}
// Details of every node
printf("\nRoll No
Name
Address
Pointer Value\n");
for(i=0;i<3;i++)
{
printf("%-10d%-12s%-12p%p\n",stud[i].roll_no,stud[i].name,
&stud[i],stud[i].ptr);
}
printf("Enter Node position of student to be deleted: ");
scanf("%d",&n);

if(n==1)
stud[2].ptr=&stud[1];
else if(n==2)
stud[0].ptr=&stud[2];
else if(n==3)
stud[1].ptr=&stud[0];
//Making the node to be deleted to point nowhere
stud[n-1].ptr=NULL;
//Data of every node after deleting the node
printf("\nRoll No
Name
Address
Pointer Value\n");
for(i=0;i<3;i++)
{
printf("%-10d%-12s%-12p%p\n",stud[i].roll_no,stud[i].name,
&stud[i],stud[i].ptr);
}
return 0;
}

#include <stdio.h>
// Structure Declaration
struct stud_record
{
char name[20];
int roll_no;
struct stud_record * ptr;
};
int main()
{
int i;
struct stud_record stud[3];
struct stud_record *st[3];
st[0]=&stud[0];
st[1]=&stud[1];
st[2]=&stud[2];
printf("*** Circular Singly Linked List

***\n");

//every node pointing to next to next node with last one pointing to first
st[0]->ptr=st[1];
st[1]->ptr=st[2];
st[2]->ptr=st[0];
//data from user for every node
for(i=0;i<3;i++)
{
printf("Node %d :\n",i+1);
printf("Enter Student %d Name : ",i+1);
scanf("%s",st[i]->name);
printf("Enter Student %d Roll No : ",i+1);
scanf("%d",&st[i]->roll_no);
}
// Details of every node
printf("\nRoll No
Name
for(i=0;i<3;i++)

Address

Pointer Value\n");

{
printf("%-10d%-12s%-12p%p\n",st[i]->roll_no,st[i]->name,st[i],
st[i]->ptr);
}
return 0;
}

#include <stdio.h>
// Structure Declaration
struct stud_record
{
char name[20];
int roll_no;
struct stud_record * ptr;
};
int main()
{
int i,n;
struct stud_record stud[4];
struct stud_record *st[4];
st[0]=&stud[0];
st[1]=&stud[1];
st[2]=&stud[2];
printf("*** Circular Singly Linked List

***\n");

//every node pointing to next to next node with last one pointing to first
st[0]->ptr=st[1];
st[1]->ptr=st[2];
st[2]->ptr=st[0];
//data from user for every node
for(i=0;i<3;i++)
{
printf("Node %d :\n",i+1);
printf("Enter Student %d Name : ",i+1);
scanf("%s",st[i]->name);
printf("Enter Student %d Roll No : ",i+1);
scanf("%d",&st[i]->roll_no);
}
// Details of every node
printf("\nRoll No
Name
Address
Pointer Value\n");
for(i=0;i<3;i++)
{
printf("%-10d%-12s%-12p%p\n",st[i]->roll_no,st[i]->name,st[i],
st[i]->ptr);
}

//Data from user for new node


printf("Node 4 :\n");
st[3]=&stud[3];
printf("Enter Student %d Name : ",4);
scanf("%s",st[3]->name);
printf("Enter Student%d Roll No : ",4);
scanf("%d",&st[3]->roll_no);
printf("Enter Node position for new student: ");
scanf("%d",&n);
if(n==1 || n==4 )
{
st[3]->ptr=st[0];
st[2]->ptr=st[3];
}
else
{
st[3]->ptr=st[n-1];
st[n-2]->ptr=st[3];
}
//Data for every node including new one
printf("\nRoll No
Name
Address
Pointer Value\n");
for(i=0;i<=3;i++)
{
printf("%-10d%-12s%-12p%p\n",st[i]->roll_no,st[i]->name,st[i],
st[i]->ptr);
}
return 0;
}

#include <stdio.h>
// Structure Declaration
struct stud_record
{
char name[20];
int roll_no;
struct stud_record * ptr;
};
int main()
{
int i,n;
struct stud_record stud[3];
struct stud_record *st[3];
st[0]=&stud[0];
st[1]=&stud[1];
st[2]=&stud[2];
printf("*** Circular Singly Linked List ***\n");
//every node pointing to next to next node with last one pointing to first
st[0]->ptr=st[1];
st[1]->ptr=st[2];
st[2]->ptr=st[0];
//data from user for every node
for(i=0;i<3;i++)
{
printf("Node %d :\n",i+1);
printf("Enter Student %d Name : ",i+1);
scanf("%s",st[i]->name);
printf("Enter Student %d Roll No : ",i+1);
scanf("%d",&st[i]->roll_no);
}
// Details of every node
printf("\nRoll No
Name
Address
Pointer Value\n");
for(i=0;i<3;i++)
{
printf("%-10d%-12s%-12p%p\n",st[i]->roll_no,st[i]->name,st[i],
st[i]->ptr);
}

//Data from user for new node


printf("Enter Node position of student to be deleted: ");
scanf("%d",&n);
if(n==1)
st[2]->ptr=st[1];
else if(n==2)
st[0]->ptr=st[2];
else if(n==3)
st[1]->ptr=st[0];
//Making the node to be deleted to point nowhere
st[n-1]->ptr=NULL;
//Data for every node including new one
printf("\nRoll No
Name
Address
Pointer Value\n");
for(i=0;i<3;i++)
{
printf("%-10d%-12s%-12p%p\n",st[i]->roll_no,st[i]->name,st[i],
st[i]->ptr);
}
return 0;
}

#include <stdio.h>
// Structure Declaration
struct stud_record
{
char name[20];
int roll_no;
struct stud_record * ptr;
};
int main()
{
int i;
struct stud_record * stud[3];
printf("*** Circular Singly Linked List ***\n");
for(i=0;i<3;i++)
{
//dynamic memory allocation for each node
stud[i]=(struct stud_record *)malloc(sizeof(struct stud_record));
//If no memory is allocated, then terminate the program
if(stud[i]==NULL)
goto end;
}
stud[0]->ptr=stud[1];
stud[1]->ptr=stud[2];
stud[2]->ptr=stud[0];
//data from user for every node
for(i=0;i<3;i++)
{
printf("Node %d :\n",i+1);
printf("Enter Student %d Name : ",i+1);
scanf("%s",stud[i]->name);
printf("Enter Student %d Roll No : ",i+1);
scanf("%d",&stud[i]->roll_no);
}
// Details of every node
printf("\nRoll No
Name
for(i=0;i<3;i++)

Address

Pointer Value\n");

{
printf("%-10d%-12s%-12p%p\n",stud[i]->roll_no,stud[i]->name,
stud[i],stud[i]->ptr);
free(stud[i]);
}
end: ;
return 0;
}

#include <stdio.h>
// Structure Declaration
struct stud_record
{
char name[20];
int roll_no;
struct stud_record * ptr;
};
int main()
{
int n,i;
struct stud_record * stud[4];
printf("*** Adding a node to Circular Singly Linked List ***\n");
for(i=0;i<3;i++)
{
//dynamic memory allocation for each node
stud[i]=(struct stud_record *)malloc(sizeof(struct stud_record));
//If no memory is allocated, then terminate the program
if(stud[i]==NULL)
goto end;
}
stud[0]->ptr=stud[1];
stud[1]->ptr=stud[2];
stud[2]->ptr=stud[0];
//data from user for every node
for(i=0;i<3;i++)
{
printf("Node %d :\n",i+1);
printf("Enter Student %d Name : ",i+1);
scanf("%s",stud[i]->name);
printf("Enter Student %d Roll No : ",i+1);
scanf("%d",&stud[i]->roll_no);
}

// Details of every node


printf("\nRoll No
Name
for(i=0;i<3;i++)

Address

Pointer Value\n");

{
printf("%-10d%-12s%-12p%p\n",stud[i]->roll_no,stud[i]->name,
stud[i],stud[i]->ptr);
}
//Data from user for new node
stud[3]=(struct stud_record *)malloc(sizeof(struct stud_record));
printf("Node 4 :\n");
printf("Enter Student %d Name : ",4);
scanf("%s",stud[3]->name);
printf("Enter Student%d Roll No : ",4);
scanf("%d",&stud[3]->roll_no);
printf("Enter Node position for new student: ");
scanf("%d",&n);
if(n==1 || n==4 )
{
stud[3]->ptr=stud[0];
stud[2]->ptr=stud[3];
}
else
{
stud[3]->ptr=stud[n-1];
stud[n-2]->ptr=stud[3];
}
//Data for every node including new one
printf("\nRoll No
Name
Address
Pointer Value\n");
for(i=0;i<=3;i++)
{
printf("%-10d%-12s%-12p%p\n",stud[i]->roll_no,stud[i]>name,stud[i],stud[i]->ptr);
free(stud[i]);
}
end: ;
return 0;
}

#include <stdio.h>
// Structure Declaration
struct stud_record
{
char name[20];
int roll_no;
struct stud_record * ptr;
};
int main()
{
int n,i;
struct stud_record * stud[3];
printf("*** Deleting a node in Circular Singly Linked List ***\n");
for(i=0;i<3;i++)
{
//dynamic memory allocation for each node
stud[i]=(struct stud_record *)malloc(sizeof(struct stud_record));
//If no memory is allocated, then terminate the program
if(stud[i]==NULL)
goto end;
}
stud[0]->ptr=stud[1];
stud[1]->ptr=stud[2];
stud[2]->ptr=stud[0];
//data from user for every node
for(i=0;i<3;i++)
{
printf("Node %d :\n",i+1);
printf("Enter Student %d Name : ",i+1);
scanf("%s",stud[i]->name);
printf("Enter Student %d Roll No : ",i+1);
scanf("%d",&stud[i]->roll_no);
}
// Details of every node
printf("\nRoll No
Name
Address
Pointer Value\n");
for(i=0;i<3;i++)

{
printf("%-10d%-12s%-12p%p\n",stud[i]->roll_no,stud[i]->name,
stud[i],stud[i]->ptr);
}
printf("Enter Node position of student to be deleted: ");
scanf("%d",&n);
if(n==1)
stud[2]->ptr=stud[1];
else if(n==2)
stud[0]->ptr=stud[2];
else if(n==3)
stud[1]->ptr=stud[0];
free(stud[n-1]);
//Making the node to be deleted to point nowhere
stud[n-1]->ptr=NULL;
//Data of every node after deleting the node
printf("\nRoll No
Name
Address
Pointer Value\n");
for(i=0;i<3;i++)
{
printf("%-10d%-12s%-12p%p\n",stud[i]->roll_no,stud[i]->name,
stud[i],stud[i]->ptr);
free(stud[i]);
}
end: ;
return 0;
}

You might also like