Dequeue is basically queue .there is one difference b/w dequeue & queue .
In queue insertion take place at end & deletion at beginning But in dequeue Insertion & deletion can take place at both end
BUT here we wan't to create input restricted dequeue .
what it means : Dequeue with input restricted to only one end . at beginning or at end.
In this we make a dequeue with input restricted at end only.
C program :
//deQueue in one way list with input restricted only at end;
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int data;
struct node *link;
}*head,*tell; // creating to variable globly
typedef struct node node; // using typedef
/*........................main......................*/
main()
{
head=NULL; // initialy list is empty so head=tell=NULL
tell=NULL;
int i,a,n;
printf("inter elements :\n");
for(i=0;i<5;i++) // taking 5 element as input & create a deqeue
{
scanf("%d",&a);
create(a);
}
display();
printf("\n 1. insert a no");
printf("\n 2. delete a no at beg");
printf("\n 3. delete a no from end");
printf("\n any other to exit\n");
while(1) // documentation for operation on dequeue
{
printf("\n inter choice :");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\n inset volue:");
scanf("%d",&a);
create(a);
display();
break;
case 2:
del_at_begg();
display();
break;
case 3:
del_at_end();
display();
break;
default :
exit(0);
break;
}
}
}//end of main
/*..............funtion def..........................*/
create(int d) // insert data in dequeue at end
{
node *r;
r=(node*)malloc(sizeof(node)); //alocating space dynamicaly
if(head==NULL)
{
r->data=d;
head=r;
tell=r;
head->link=NULL;
}
else
{
r->data=d;
tell->link=r;
tell=tell->link;
tell->link=NULL;
}
}
display() // to dispaly data element of dequeue
{
node *temp;
if(head==NULL)
{
printf("\n queue is empty :\n");
}
else
{
temp=head;
printf("\n data element of list is ::");
while(temp!=NULL)
{
printf(" %d ",temp->data);
temp=temp->link;
}
}
}
del_at_begg() //delete at beg ; just point head to next node & free
{ // space of first node
node *r;
r=head;
head=head->link;
free(r);
}
del_at_end() // just point tell to node just before last
{ // & tell->link=NULL & free last node
node *temp;
temp=head;
if(head==tell) // if only one node in list
{
temp=head;
head=NULL;
tell=NULL;
free(temp);
}
else
{
while(temp->link != tell)
{
temp=temp->link;
}
tell=temp;
temp=temp->link;
tell->link=NULL;
free(temp);
}
}
OUTPUT:-
Input restricted dequeue in one way link list |
No comments:
Post a Comment
THANKS FOR UR GREAT COMMENT