Example : A Data Structure(DS) program of a Double Linked List using C to create a double-linked list having five nodes/student records to store at least two student data in each node and display them using while loops without function.
#include <stdio.h>
#include <conio.h>
#include <malloc.h> //#include <stdlib.h>
struct student
{
struct student *back;
int srollno;
char sname[50];
struct student *next;
};
struct student *home, *link, *newnode;
void main()
{
int i;
//single Node creation code
home= (struct student*)malloc(sizeof(struct student));
printf("Enter Roll No. and Student name = ");
scanf("%d%s",&home->srollno,home->sname);
home->next=NULL;
home->back=NULL;
link=home;
for(i=1;i<5;i=i+1)
{
newnode= (struct student*)malloc(sizeof(struct student));
printf("Enter Roll No. and Student name = ");
scanf("%d%s",&newnode->srollno,newnode->sname);
newnode->next=NULL;
newnode->back=NULL;
link->next=newnode;
newnode->back=link;
link=link->next;
}
//Display code
printf("\nThe Outputs are = \n");
link=home;
while(link!=NULL)
{
printf("%d %s\n",link->srollno,link->sname);
link=link->next;
}
getch();
}
Output :
Enter Roll No. and Student name = 101
A
Enter Roll No. and Student name = 201
B
Enter Roll No. and Student name = 301
C
Enter Roll No. and Student name = 401
D
Enter Roll No. and Student name = 501
E
The Outputs are =
101 A
201 B
301 C
401 D
501 E
Example : A Data Structure(DS) program using C to create a double-list having five nodes/student records to store at least two student data in each node and display them using while loops with user-defined functions.
#include <stdio.h>
#include <conio.h>
#include <malloc.h> //#include <stdlib.h>
struct student
{
struct student *back;
int srollno;
char sname[50];
struct student *next;
};
struct student *home, *link, *newnode;
void create(struct student *);
void display(struct student *);
int i;
void main()
{
//single Node creation code
home= (struct student*)malloc(sizeof(struct student));
printf("Enter Roll No. and Student name = ");
scanf("%d%s",&home->srollno,home->sname);
home->next=NULL;
home->back=NULL;
create(home);
display(home);
getch();
}
void create(struct student *home1)
{
link=home1;
for(i=1;i<5;i=i+1)
{
newnode= (struct student*)malloc(sizeof(struct student));
printf("Enter Roll No. and Student name = ");
scanf("%d%s",&newnode->srollno,newnode->sname);
newnode->next=NULL;
newnode->back=NULL;
link->next=newnode;
newnode->back=link;
link=link->next;
}
}
void display(struct student *home2)
{
//Display code
printf("\nThe Outputs are = \n");
link=home2;
while(link!=NULL)
{
printf("%d %s\n",link->srollno,link->sname);
link=link->next;
}
}
Output :
Enter Roll No. and Student name = 10
A
Enter Roll No. and Student name = 20
B
Enter Roll No. and Student name = 30
C
Enter Roll No. and Student name = 40
D
Enter Roll No. and Student name = 50
E
The Outputs are =
10 A
20 B
30 C
40 D
50 E
Example : A Data Structure (DS) program using C to insert a new node at the beginning of a double list and display it using a user-defined function.
#include <stdio.h>
#include <conio.h>
#include <malloc.h> //#include <stdlib.h>
struct student
{
struct student *back;
int srollno;
char sname[50];
struct student *next;
};
struct student *home, *link, *newnode;
void create(struct student *);
void display(struct student *);
void addatbeg(struct student *);
void display(struct student *);
int i,n;
void main()
{
printf("Enter the no. of nodes U want to create in DLL = ");
scanf("%d",&n);
printf("Enter %d records of students",n);
//single Node creation code
home= (struct student*)malloc(sizeof(struct student));
printf("\nNow Enter Roll No. and Student name = ");
scanf("%d%s",&home->srollno,home->sname);
home->next=NULL;
home->back=NULL;
create(home);
display(home);
addatbeg(home);
display(home);
getch();
}
void create(struct student *home1)
{
link=home1;
for(i=1;i<n;i=i+1)
{
newnode= (struct student*)malloc(sizeof(struct student));
printf("Enter Roll No. and Student name = ");
scanf("%d%s",&newnode->srollno,newnode->sname);
newnode->next=NULL;
newnode->back=NULL;
link->next=newnode;
newnode->back=link;
link=link->next;
}
}
void display(struct student *home2)
{
//Display code
printf("\nThe Outputs are = \n");
link=home2;
while(link!=NULL)
{
printf("%d %s\n",link->srollno,link->sname);
link=link->next;
}
}
void addatbeg(struct student * home3)
{
printf("\nEnter Single node value to insert at beggining");
newnode= (struct student*)malloc(sizeof(struct student));
printf("\nEnter Roll No. and Student name = ");
scanf("%d%s",&newnode->srollno,newnode->sname);
newnode->next=NULL;
newnode->back=NULL;
newnode->next=home3;
home3->back=newnode;
home=newnode;
}
Output:
Enter the no. of nodes U want to create in DLL = 3
Enter 3 records of students
Now Enter Roll No. and Student name = 10 a
Enter Roll No. and Student name = 20 b
Enter Roll No. and Student name = 30 c
The Outputs are =
10 a
20 b
30 c
Enter Single node value to insert at beggining
Enter Roll No. and Student name = 40 d
The Outputs are =
40 d
10 a
20 b
30 c
Example : A Data Structure (DS) program using C to insert a new node at the End of a double-linked list and display them using user defined function.
#include <stdio.h>
#include <conio.h>
#include <malloc.h> //#include <stdlib.h>
struct student
{
struct student *back;
int srollno;
char sname[50];
struct student *next;
};
struct student *home, *link, *newnode;
void create(struct student *);
void display(struct student *);
void addatend(struct student *);
void display(struct student *);
int i,n;
void main()
{
printf("Enter the no. of nodes U want to create in DLL = ");
scanf("%d",&n);
printf("Enter %d records of students",n);
//single Node creation code
home= (struct student*)malloc(sizeof(struct student));
printf("\nNow Enter Roll No. and Student name = ");
scanf("%d%s",&home->srollno,home->sname);
home->next=NULL;
home->back=NULL;
create(home);
display(home);
addatend(home);
display(home);
getch();
}
void create(struct student *home1)
{
link=home1;
for(i=1;i<n;i=i+1)
{
newnode= (struct student*)malloc(sizeof(struct student));
printf("Enter Roll No. and Student name = ");
scanf("%d%s",&newnode->srollno,newnode->sname);
newnode->next=NULL;
newnode->back=NULL;
link->next=newnode;
newnode->back=link;
link=link->next;
}
}
void display(struct student *home2)
{
//Display code
printf("\nThe Outputs are = \n");
link=home2;
while(link!=NULL)
{
printf("%d %s\n",link->srollno,link->sname);
link=link->next;
}
}
void addatend(struct student * home3)
{
printf("\nEnter Single node value to insert at End");
newnode= (struct student*)malloc(sizeof(struct student));
printf("\nEnter Roll No. and Student name = ");
scanf("%d%s",&newnode->srollno,newnode->sname);
newnode->next=NULL;
newnode->back=NULL;
link=home3;
while(link->next!=NULL)
{
link=link->next;
}
link->next=newnode;
newnode->back=link;
}
Output :
Enter the no. of nodes U want to create in DLL = 3
Enter 3 records of students
Now Enter Roll No. and Student name = 10 A
Enter Roll No. and Student name = 20 B
Enter Roll No. and Student name = 30 C
The Outputs are =
10 A
20 B
30 C
Enter Single node value to insert at End
Enter Roll No. and Student name = 100 Z
The Outputs are =
10 A
20 B
30 C
100 Z
0 Comments