Simple/Normal Queue Examples

          

Example : Data structure program using Queue in C to create a simple or normal queue having five nodes and display its contents.

Simple Queue Examples : Coders Helpline

OR

Simple Queue Example : Coders Helpline

#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct student
{
  int srollno;
  char sname[50];  
  struct student *ptr;  
};
   
struct student *rear, *front, *newnode;
void main()
{
   int i;  
	
   //single node creation code
   rear= (struct student*)malloc(sizeof(struct student));
   printf("Enter Roll No. and Student name = ");
   scanf("%d%s",& rear->srollno, rear->sname);

   rear->ptr=NULL;
   front=rear;		
	
   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->ptr=NULL;
		
	rear->ptr=newnode;
	rear=rear->ptr;			
   }
   
   //Display code
   printf("\nThe Outputs of Simple Queue are = \n");
   newnode=front;
   while(newnode!=NULL)
   {
	printf("%d  %s\n", newnode->srollno, newnode->sname);
	newnode=newnode->ptr;		
   }
   getch();
}

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 of Simple Queue are =
10  A
20  B
30  C
40  D
50  E
Example : Data structure programs using C to insert a new node in a simple or normal queue having five nodes and display its contents. [Insertion in Simple Queue]
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct student
{
  int srollno;
  char sname[50];  
  struct student *ptr;  
};
   
struct student *rear, *front, *newnode;
void main()
{
   int i;  
	
   //single Node creation code
   rear= (struct student*)malloc(sizeof(struct student));
   printf("Enter Roll No. and Student name = ");
   scanf("%d%s",& rear->srollno, rear->sname);

   rear->ptr=NULL;
   front=rear;		
	
   for(i=1;i<4;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->ptr=NULL;
		
	rear->ptr=newnode;
	rear=rear->ptr;			
   }
   
   //Display code
   printf("\nSimple Queue before insertion is = \n");
   newnode=front;
   while(newnode!=NULL)
   {
	printf("%d  %s\n", newnode->srollno, newnode->sname);
	newnode=newnode->ptr;		
   }

	newnode= (struct student*)malloc(sizeof(struct student));
	printf("\nNow Enter Roll No. and Student name for a new node insertion in a simple queue=\n");
	scanf("%d%s",&newnode->srollno,newnode->sname);
	newnode->ptr=NULL;
	
	rear->ptr=newnode;
	rear=rear->ptr;
	
	printf("\nSimple Queue after insertion a new node is = \n");
   	newnode=front;
   	while(newnode!=NULL)
   {
	printf("%d  %s\n", newnode->srollno, newnode->sname);
	newnode=newnode->ptr;		
   }


   getch();
}

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

Simple Queue before insertion is =
10  A
20  B
30  C
40  D

Now Enter Roll No. and Student name for a new node insertion in a simple queue= 50
E

Simple Queue after insertion a new node is =
10  A
20  B
30  C
40  D
50  E
Example : Data structure programs using C to delete/remove an existing node from a simple or normal queue having five nodes and display its contents. [Deletion from Simple Queue]
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct student
{
  int srollno;
  char sname[50];  
  struct student *ptr;  
};
   
struct student *rear, *front, *newnode;
void main()
{
   int i;  
	
   //single Node creation code
   rear= (struct student*)malloc(sizeof(struct student));
   printf("Enter Roll No. and Student name = ");
   scanf("%d%s",& rear->srollno, rear->sname);

   rear->ptr=NULL;
   front=rear;		
	
   for(i=1;i<4;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->ptr=NULL;
		
	rear->ptr=newnode;
	rear=rear->ptr;			
   }
   
   //Display code
   printf("\nSimple Queue before insertion is = \n");
   newnode=front;
   while(newnode!=NULL)
   {
	printf("%d  %s\n", newnode->srollno, newnode->sname);
	newnode=newnode->ptr;		
   }

	newnode=front;
	front=front->ptr;
	free(newnode);	
		
	printf("\nSimple Queue after deletion an existing node is = \n");
   	newnode=front;
   	while(newnode!=NULL)
   {
	printf("%d  %s\n", newnode->srollno, newnode->sname);
	newnode=newnode->ptr;		
   }

   getch();
}

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

Simple Queue before insertion is =
10  A
20  B
30  C
40  D

Simple Queue after deletion an existing node is =
20  B
30  C
40  D

Circular Queue Examples

Example : Data structure programs using C to create and display the node values in a Circular Queue having user-defined nodes and display its contents. [Creation and Traversing from Circular Queue]
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct student
{
  int srollno;
  char sname[50];  
  struct student *ptr;  
};
   
struct student *rear, *front, *temp, *link;
void main()
{
	int i,n;
	printf("Enter the node value you want to create in a circular queue\n");
	scanf("%d",&n);
	printf("Enter %d records of student as Roll No. and Student name =\n",n);  
	if(rear==NULL)
	{
	   //single node creation code
 	   rear=(struct student*)malloc(sizeof(struct student)); 		
 	   scanf("%d%s",& rear->srollno, rear->sname);
	   front=rear;	
 	   rear->ptr=front; 		
	}
 	if(rear!=NULL)
	 {
	    for(i=1;i<n;i=i+1)
	      {
		temp= (struct student*)malloc(sizeof(struct student));				
		scanf("%d%s",&temp->srollno,temp->sname);
		temp->ptr=front;
					
		rear->ptr=temp;
		rear=rear->ptr;			
	      }	 	
	 } 
   
   //First method of Display or Traversing code in Circular Queue
   printf("\nThe Output of Circular Queue are = \n");
   temp=front;
   do
   {
	printf("%d  %s\n", temp->srollno, temp->sname);
	temp=temp->ptr;		
   }while(temp!=front);


//Second method of Display or Traversing code in Circular Queue
   printf("\nThe Output of Circular Queue are = \n");
   temp=front;
   
   printf("%d  %s\n", temp->srollno, temp->sname);
   temp=temp->ptr;
			
   while(temp!=front)
   {
	printf("%d  %s\n", temp->srollno, temp->sname);
	temp=temp->ptr;		
   }

//Third method of Display or Traversing code in Circular Queue
   printf("\nThe Output of Circular Queue are = \n");
   	temp=front;
   	link=NULL;
			
   while(temp!=link)
   {
	printf("%d  %s\n", temp->srollno, temp->sname);
	temp=temp->ptr;
	link=front;		
   }
   getch();
}

Output:

Enter the node value you want to create in a circular queue
5
Enter 5 records of student as Roll No. and Student name =
10 A
20 B
30 C
40 D
50 E

The Output of Circular Queue are =
10  A
20  B
30  C
40  D
50  E
Example : Data structure program using C to insert and display the node values in an existing Circular Queue having user-defined nodes. [Insertion and then Traversing in Circular Queue]
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct student
{
  int srollno;
  char sname[50];  
  struct student *ptr;  
};
   
struct student *rear, *front, *temp;
void main()
{
	int i,n;
	printf("Enter the node value you want to create in a circular queue\n");
	scanf("%d",&n);
	printf("Enter %d records of student as Roll No. and Student name =\n",n);  
	
	if(rear==NULL)
	{
	   //single node creation code
 	   rear=(struct student*)malloc(sizeof(struct student)); 		
 	   scanf("%d%s",& rear->srollno, rear->sname);
	   front=rear;	
 	   rear->ptr=front; 		
	}
	
 	if(rear!=NULL)
	 {
	    for(i=1;i<n;i=i+1)
	      {
		temp= (struct student*)malloc(sizeof(struct student));				
		scanf("%d%s",&temp->srollno,temp->sname);
		temp->ptr=front;
					
		rear->ptr=temp;
		rear=rear->ptr;			
	      }	 	
	 } 
   
   //Display or Traversing code in Circular Queue
   	printf("\nThe Output of Circular Queue before Insertion are = \n");
   	temp=front;
   	do
   	{
	   printf("%d  %s\n", temp->srollno, temp->sname);
	   temp=temp->ptr;		
   	}while(temp!=front);
   	
  // New node creation for Insertion
	printf("\nEnter Roll No. and Student name again for New node U want to Insert =\n",n);  
   	temp= (struct student*)malloc(sizeof(struct student));				
	scanf("%d%s",&temp->srollno,temp->sname);
	temp->ptr=front;
		
	rear->ptr=temp;
	rear=rear->ptr;
		
//Display or Traversing code in Circular Queue
   	printf("\nThe Output of Circular Queue After Insertion are = \n");
   	temp=front;
   	do
   	{
	   printf("%d  %s\n", temp->srollno, temp->sname);
	   temp=temp->ptr;		
   	}while(temp!=front);
   	
   getch();
}

Output:

Enter the node value you want to create in a circular queue
5
Enter 5 records of student as Roll No. and Student name =
10 A
20 B
30 C
40 D
50 E

The Output of Circular Queue before Insertion are =
10  A
20  B
30  C
40  D
50  E

Enter Roll No. and Student name again for New node U want to Insert =
60 F

The Output of Circular Queue After Insertion are =
10  A
20  B
30  C
40  D
50  E
60  F
Example : Data structure program/Queue Examples using C to delete a node and then display the node values in an existing Circular Queue having user-defined nodes. [Deletion and then Traversing in Circular Queue]
#include <stdio.h>  
#include <conio.h>  
#include <malloc.h>    //#include <stdlib.h>

struct student
{
  int srollno;
  char sname[50];  
  struct student *ptr;  
};
   
struct student *rear, *front, *temp;
void main()
{
	int i,n;
	printf("Enter the node value you want to create in a circular queue\n");
	scanf("%d",&n);
	printf("Enter %d records of student as Roll No. and Student name =\n",n);  
	
	if(rear==NULL)
	{
	   //single node creation code
 	   rear=(struct student*)malloc(sizeof(struct student)); 		
 	   scanf("%d%s",& rear->srollno, rear->sname);
	   front=rear;	
 	   rear->ptr=front; 		
	}
	
 	if(rear!=NULL)
	 {
	  for(i=1;i<n;i=i+1)
	   {
		temp= (struct student*)malloc(sizeof(struct student));				
		scanf("%d%s",&temp->srollno,temp->sname);
		temp->ptr=front;
					
		rear->ptr=temp;
		rear=rear->ptr;			
	   }	 	
	 } 
   
   //Display or Traversing code in Circular Queue
   	printf("\nThe Output of Circular Queue before Deletion are = \n");
   	temp=front;
   	do
   	{
	   printf("%d  %s\n", temp->srollno, temp->sname);
	   temp=temp->ptr;		
   	}while(temp!=front);
   	
  // Code for Deletion of Node
	temp=front;
	front=front->ptr;
	rear->ptr=front;
	free(temp);
				
  //Display or Traversing code in Circular Queue
   	printf("\nThe Output of Circular Queue After Deletion are = \n");
   	temp=front;
   	do
   	{
	   printf("%d  %s\n", temp->srollno, temp->sname);
	   temp=temp->ptr;		
   	}while(temp!=front);
   	
   getch();
}

Output:

Enter the node value you want to create in a circular queue
5
Enter 5 records of student as Roll No. and Student name =
10 A
20 B
30 C
40 D
50 E

The Output of Circular Queue before Deletion are =
10  A
20  B
30  C
40  D
50  E

The Output of Circular Queue After Deletion are =
20  B
30  C
40  D
50  E

Loading


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.