You are on page 1of 3

Padre Conceicao College of Engineering

PROGRAMS :

1.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node*front = NULL;
struct node *rear = NULL;
void enqueue()
{
struct node *newnode = (struct node*)malloc(sizeof(struct node));
if (newnode == NULL)
printf("MEMORY NOT ALLOCATED \n");
else
{
printf("ENTER ELEMENT ");
scanf_s("%d", &newnode->data);
newnode->next = NULL;
if (front == NULL && rear == NULL)
{
front = rear = newnode;
newnode->next = front;
}
else
{
rear->next = newnode;
newnode->next = front;
rear = newnode;
}
printf("ELEMENT ADDED\n ");
}
}
void dequeue()
{

if (front == NULL && rear == NULL)


{
printf("QUEUE IS EMPTY \n");
}
else if (front == rear)
{
free(front);
front = rear = NULL;
printf("FIRST NODE DELETED \n");
}
else
{
struct node *temp;
temp = front;
front = front->next;
rear->next = front;
free(temp);
printf("FIRST NODE DELETED \n");
}

Data Structure Programming Lab, Department of Computer Engineering RollNo. CE


Padre Conceicao College of Engineering
}
void peek()
{
if (front == NULL && rear == NULL)
{
printf("QUEUE IS EMPTY \n");
}
else
{
printf("THE FIRST ELEMENT IS %d\n", front->data);
}

}
void display()
{
if (front == NULL && rear == NULL)
{
printf("QUEUE IS EMPTY \n");
}
else
{
struct node *temp;
temp = front;
do
{
printf("%d ", temp->data);
temp = temp->next;
} while (temp != front);
}
}
int main()
{
int a = 1, ch;
printf("1:ENQUEUE 2:DEQUEUE 3:PEEK 4:DISPLAY 5:END\n");
do
{
printf("\n ENTER YOUR CHOICE ");
scanf_s("%d", &ch);
switch (ch)
{
case 1:enqueue();break;
case 2:dequeue();break;
case 3:peek();break;
case 4:display();break;
case 5:a = 0;break;
default:printf("WRONG CHOICE \n");break;
}
} while (a != 0);
_getch();
}

Output:

Data Structure Programming Lab, Department of Computer Engineering RollNo. CE


Padre Conceicao College of Engineering

Data Structure Programming Lab, Department of Computer Engineering RollNo. CE

You might also like