/*Serial No.147 [swami129.cpp]*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
typedef struct node
{
int info;
struct node *next;
}node;
void enqueue_at_end(node **head)
{
node *temp,*q=*head;
temp=(node *)malloc(sizeof(node));
printf("\nenter the value= ");
scanf("%d",&temp->info);
temp->next=NULL;
if(*head==NULL)
{
*head=temp;
return;
}
while(q->next!=NULL)
{
q=q->next;
}
q->next=temp;
}
void disp(node *head)
{
node *q=head;
if(head==NULL)
printf("\nQueue is empty");
else
{
printf("\nElements in Queue are:\n");
do
{
printf("%d\t",q->info);
q=q->next;
}while(q!=NULL);
}
}
void dequeue_from_beg(node **head)
{
node *q=*head,*temp;
if(q==NULL)
{
printf("\nQueue is empty.");
return;
}
temp=q;
if(q->next==NULL)
*head=NULL;
else
*head=q->next;
free(temp);
}
void enqueue_at_beg(node **head)
{
node *temp;
temp=(node *)malloc(sizeof(node));
printf("\nenter the value= ");
scanf("%d",&temp->info);
temp->next=*head;
*head=temp;
}
void dequeue_from_end(node **head)
{
node *temp,*q=*head;
if(q==NULL)
{
printf("\nQueue is empty.");
return;
}
if(q->next==NULL)
{
temp=q;
*head=NULL;
}
else
{
while(q->next->next!=NULL)
{
q=q->next;
}
temp=q->next;
q->next=NULL;
}
free(temp);
}
void main()
{
node *start=NULL;
int n;
clrscr();
do{
printf("\n[1] enqueue_at_end");
printf("\n[2] dequeue_from_beg");
printf("\n[3] display");
printf("\n[4] enqueue_at_beg");
printf("\n[5] dequeue_from_end");
printf("\n\tEnter your choice= ");
scanf("%d",&n);
switch(n)
{
case 1: enqueue_at_end(&start);
break;
case 2: dequeue_from_beg(&start);
break;
case 3: disp(start);
break;
case 4: enqueue_at_beg(&start);
break;
case 5: dequeue_from_end(&start);
break;
case 7: break;
}
}while(n!=7);
getch();
}
No comments:
Post a Comment
If you have any doubt, feel free to ask...