Example : A Data Structure(DS) program 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 linked list having five nodes/student records to store at least two student data in each node and display them using while loops with 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 *);

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

 6,383 total views,  1 views today


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.