Professional Documents
Culture Documents
Q List
Q List
Experiment 6
Number
Problem write a C program to implement queue using Linked List
Statement
struct node
{
int data;
struct node *next;
};
void push()
{
int x;
printf("Enter the data: ");
scanf("%d", &x);
struct node *newnode = (struct node *)malloc(sizeof(struct
node));
newnode->data = x;
newnode->next = NULL;
if (rear == NULL) // Check rear for emptiness
{
front = rear = newnode;
}
else
{
rear->next = newnode;
rear = newnode;
}
}
void pop()
{
if (front == NULL)
printf("Underflow\n");
else
{
struct node *temp = front;
printf("%d",front->data);
front = front->next;
free(temp); // Free memory of the removed node
}
}
void display()
{
struct node *temp = front; // Start from the front
printf("The elements are: ");
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
int ch;
do
{
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Invalid input\n");
}
} while (ch != 4);
return 0;
}
Output: