Single Circular Linked List
Example: A data structure program using C to create a Single Circular Linked List with user-defined nodes to store records with at least two attributes of student and then display all the stored records.
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct number
{
int num;
struct number *ptr;
};
struct number *home, *link, *newnode;
void create(struct number *);
void display(struct number *);
int i,x;
void main()
{
printf("Enter the Node value for a Single Circular Linked List = ");
scanf("%d",&x);
printf("Enter %d Integer Numeric Value = ",x);
home= (struct number*)malloc(sizeof(struct number));
scanf("%d",&home->num);
home->ptr=home;
create(home);
display(home);
getch();
}
void create(struct number *home1)
{
link=home1;
int i;
for(i=1;i<x;i++)
{
newnode= (struct number*)malloc(sizeof(struct number));
printf("Enter Integer Numeric Value = ");
scanf("%d",&newnode->num);
newnode->ptr=home;
link->ptr=newnode;
link=link->ptr;
}
}
// Traversing or Printing code for Single Circular Linked List
void display(struct number *home2)
{
printf("\nThe Outputs are = \n");
link=home2;
do
{
printf("%d ",link->num);
link=link->ptr;
}while(link!=home);
}
OR
void display(struct number *home2)
{
printf("\nThe Outputs are = \n");
link=home2;
newnode=NULL;
while(link!=newnode)
{
printf("%d ",link->num);
link=link->ptr;
newnode=home;
}
}
OR
void display(struct number *home2)
{
printf("\nThe Outputs are = \n");
link=home2;
printf("%d ",link->num);
link=link->ptr;
while(link!=home2)
{
printf("%d ",link->num);
link=link->ptr;
}
}
Output:
Enter the Node value for a Single Circular Linked List = 4
Enter 4 Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30
Enter Integer Numeric Value = 40
The Outputs are =
10 20 30 40
Example: A data structure program using C to Insert a new node into a Single Circular Linked List at the Beginning with the user-defined existing nodes and then display all the records.
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct number
{
int num;
struct number *ptr;
};
struct number *home, *link, *newnode;
void create(struct number *);
void display(struct number *);
void addatbeg(struct number *);
int i,x;
void main()
{
printf("Enter the Node value for a Single Circular Linked List = ");
scanf("%d",&x);
printf("Enter %d Integer Numeric Value = ",x);
home= (struct number*)malloc(sizeof(struct number));
scanf("%d",&home->num);
home->ptr=home;
create(home);
printf("\nThe output before Insertion at beginningare\n");
display(home);
addatbeg(home);
printf("\nThe output after Insertion at beginning are\n");
display(home);
getch();
}
void create(struct number *home1)
{
link=home1;
int i;
for(i=1;i<x;i++)
{
newnode= (struct number*)malloc(sizeof(struct number));
printf("Enter Integer Numeric Value = ");
scanf("%d",&newnode->num);
newnode->ptr=home;
link->ptr=newnode;
link=link->ptr;
}
}
void addatbeg(struct number *home3)
{
link=home3;
while(link->ptr!=home)
{
link=link->ptr;
}
newnode=(struct number*)malloc(sizeof(struct number));
printf("\n\nEnter Single Integer Numeric Value for Insertion = ");
scanf("%d",&newnode->num);
newnode->ptr=newnode;
newnode->ptr=home;
home=newnode;
link->ptr=home;
}
void display(struct number *home2)
{
link=home2;
do
{
printf("%d ",link->num);
link=link->ptr;
}while(link!=home);
}
Output:
Enter the Node value for a Single Circular Linked List = 4
Enter 4 Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30
Enter Integer Numeric Value = 40
The output before Insertion at beginning are
10 20 30 40
Enter Single Integer Numeric Value for Insertion = 50
The output after Insertion at beginning are
50 10 20 30 40
Example: A data structure program using C to Insert a new node into a Single Circular List at the End with the user-defined existing nodes and then display all the records.
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct number
{
int num;
struct number *ptr;
};
struct number *home, *link, *newnode;
void create(struct number *);
void display(struct number *);
void addatend(struct number *);
int i,x;
void main()
{
printf("Enter the Node value for a Single Circular Linked List = ");
scanf("%d",&x);
printf("Enter %d Integer Numeric Value = ",x);
home= (struct number*)malloc(sizeof(struct number));
scanf("%d",&home->num);
home->ptr=home;
create(home);
printf("\nThe output before Insertion at the End are\n");
display(home);
addatend(home);
printf("\nThe output after Insertion at the End are\n");
display(home);
getch();
}
void create(struct number *home1)
{
link=home1;
int i;
for(i=1;i<x;i++)
{
newnode= (struct number*)malloc(sizeof(struct number));
printf("Enter Integer Numeric Value = ");
scanf("%d",&newnode->num);
newnode->ptr=home;
link->ptr=newnode;
link=link->ptr;
}
}
void addatend(struct number *home3)
{
link=home3;
while(link->ptr!=home)
{
link=link->ptr;
}
newnode=(struct number*)malloc(sizeof(struct number));
printf("\n\nEnter Single Integer Numeric Value for Insertion = ");
scanf("%d",&newnode->num);
newnode->ptr=home;
link->ptr=newnode;
}
void display(struct number *home2)
{
link=home2;
do
{
printf("%d ",link->num);
link=link->ptr;
}while(link!=home);
}
Output:
Enter the Node value for a Single Circular Linked List = 3
Enter 3 Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30
The output before Insertion at the End are
10 20 30
Enter Single Integer Numeric Value for Insertion = 40
The output after Insertion at the End are
10 20 30 40
Example: A data structure program using C to Delete a node from a Single Circular List from the Beginning with the user-defined existing nodes and then display all the records.
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct number
{
int num;
struct number *ptr;
};
struct number *home, *link, *newnode;
void create(struct number *);
void display(struct number *);
void delatbeg(struct number *);
int i,x;
void main()
{
printf("Enter the Node value for a Single Circular Linked List = ");
scanf("%d",&x);
printf("Enter %d Integer Numeric Value = ",x);
home= (struct number*)malloc(sizeof(struct number));
scanf("%d",&home->num);
home->ptr=home;
create(home);
printf("\nThe output before Deletion from the Beginning are\n");
display(home);
delatbeg(home);
printf("\nThe output after Deletion from the Beginning are\n");
display(home);
getch();
}
void create(struct number *home1)
{
link=home1;
int i;
for(i=1;i<x;i++)
{
newnode= (struct number*)malloc(sizeof(struct number));
printf("Enter Integer Numeric Value = ");
scanf("%d",&newnode->num);
newnode->ptr=home;
link->ptr=newnode;
link=link->ptr;
}
}
void delatbeg(struct number *home3)
{
link=home3;
while(link->ptr!=home)
{
link=link->ptr;
}
newnode=home3;
home=home->ptr;
link->ptr=home;
free(newnode);
}
void display(struct number *home2)
{
link=home2;
do
{
printf("%d ",link->num);
link=link->ptr;
}while(link!=home);
}
Output:
Enter the Node value for a Single Circular Linked List = 4
Enter 4 Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30
Enter Integer Numeric Value = 40
The output before Deletion from the Beginning are
10 20 30 40
The output after Deletion from the Beginning are
20 30 40
Example: A data structure program using C to Delete a node from a Single Circular List from the End with the user-defined existing nodes and then display all the records.
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct number
{
int num;
struct number *ptr;
};
struct number *home, *link, *newnode;
void create(struct number *);
void display(struct number *);
void delatend(struct number *);
int i,x;
void main()
{
printf("Enter the Node value [>3] for a Single Circular Linked List = ");
scanf("%d",&x);
printf("Enter %d Integer Numeric Value = ",x);
home= (struct number*)malloc(sizeof(struct number));
scanf("%d",&home->num);
home->ptr=home;
create(home);
printf("\nThe output before Deletion from the End are\n");
display(home);
delatend(home);
printf("\nThe output after Deletion from the End are\n");
display(home);
getch();
}
void create(struct number *home1)
{
link=home1;
int i;
for(i=1;i<x;i++)
{
newnode= (struct number*)malloc(sizeof(struct number));
printf("Enter Integer Numeric Value = ");
scanf("%d",&newnode->num);
newnode->ptr=home;
link->ptr=newnode;
link=link->ptr;
}
}
void delatend(struct number *home3)
{
link=home3;
while(link->ptr->ptr!=home)
{
link=link->ptr;
}
newnode=link->ptr;
link->ptr=home;
free(newnode);
}
void display(struct number *home2)
{
link=home2;
do
{
printf("%d ",link->num);
link=link->ptr;
}while(link!=home);
}
Output:
Enter the Node value [>3] for a Single Circular Linked List = 5
Enter 5 Integer Numeric Value = 10
Enter Integer Numeric Value = 20
Enter Integer Numeric Value = 30
Enter Integer Numeric Value = 40
Enter Integer Numeric Value = 50
The output before Deletion from the End are
10 20 30 40 50
The output after Deletion from the End are
10 20 30 40
Double Circular Linked List
0 Comments