Queues on Pointer (Dynamic Memory Allocation) using DevC++
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int data;
struct node *link;
};
node *front = NULL;
node *rear = NULL;
void insertion ()
{
node *temp = (node*) malloc (sizeof(node));
printf ("Insert Value = ");
scanf ("%d",&temp->data);
temp->link = NULL;
if (front==NULL)
{
front=temp;
rear=temp;
}
else
{
rear->link=temp;
rear=temp;
}
}
void deletion ()
{
if(front==NULL)
{
printf ("Queues is Empty");
}
else
{
node *temp;
printf ("Elemen to be deleted = %d",front->data);
temp=front;
front=front->link;
free (temp);
}
}
void display ()
{
if (front==NULL)
{
printf ("Queues is Empty");
}
else
{
node *temp;
temp=front;
printf ("elemen of Queues = \n");
for (temp=front;temp!=NULL;temp=temp->link)
{
printf ("%d\t",temp->data);
}
}
}
void selection ()
{
int pilih;
do
{
printf ("\n\nQueues Using Dynamic Memory Allocation");
printf ("\n1. Insertion ");
printf ("\n2. Deletion");
printf ("\n3. Display");
printf ("\ninsert your selection = ");
scanf ("%d",&pilih);
switch (pilih)
{
case 1:
insertion ();
break;
case 2:
deletion ();
break;
case 3:
display ();
break;
}
}while (pilih!=0);
}
int main ()
{
selection ();
printf ("\n");
getche ();
return 0;
}