Wednesday, February 17, 2010

C/C++

                 OUPUT RESTRICTED QUEUE.

                      By- Tanaya Karmakar
(Future Group  of Engineering College,Kolkata)

/* Queue is a data strucure where insertion of new elements are done at rear while deletion of elements are done
from front of the queue.
OUPUT RESTRICTED QUEUE is a queue where insertion of elements are done both rear and front, and deletion can only be done
from front of the queue.
here is a C program that implements the OUPUT RESTRICTED QUEUE.
Hope this will help to all Data Structure students.
Though the full program is given below, I have created a link for your easier download.
http://rapidshare.com/files/351465112/output_restricted.c
*/

/*OUPUT RESTRICTED QUEUE */
#include
#include
struct list
{
int data;
struct list *next;
};
typedef struct list node;
node *front,*rear;
void ins_at_beg(node **,node **,int);
void ins_at_end(node **,node **,int);
void del(node **);
void show(node *);
void main()
{
int ch,num;
front=rear=NULL;
while(ch!=4)
{
printf("\n1->enter item at the beginning\n");
printf("2->enter item at the end\n");
printf("3->delete item\n");
printf("Enter choice(1,2 or 3)->");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter num: ");
scanf("%d",&num);
ins_at_beg(&front,&rear,num);
printf("\nThe list is:\n");
show(front);
break;
case 2:
printf("Enter num: ");
scanf("%d",&num);
ins_at_end(&front,&rear,num);
printf("\nThe list is:\n");
show(front);
break;
case 3:
del(&front);
printf("\nAfter deletion,the list is:\n");
show(front);
break;
default:
printf("\nWrong choice\n");
exit(0);
}
}
}
void ins_at_beg(node **front,node **rear,int num)
{
node *q;
q=(node *)malloc(sizeof(node));
q->data=num;
if(*front==NULL)
{
q->next=*front;
*front=q;
*rear=*front;
}
else
{
q->next=*front;
*front=q;
}
}
void ins_at_end(node **front,node **rear,int num)
{
node *q;
node *p;
p=*front;
while(p->next!=NULL)
{
p=p->next;
}
q=(node *)malloc(sizeof(node));
q->data=num;
p->next=q;
*rear=q;
(*rear)->next=NULL;
}
void del(node **front)
{
node *q;
if(*front==NULL)
{
printf("Queue is empty");
return;
}
else
{
q=(node *)malloc(sizeof(node));
q->data=(*front)->data;
*front=(*front)->next;
free(q);
}
}
void show(node *p)
{
while(p!=NULL)
{
printf("%d\t",p->data);
p=p->next;
}
}

-----------------------

1 comment:

  1. I am Soumen from Orissa. I am a regular visitor of your site.
    This is a nice program.. very helpful. Thanks!

    ReplyDelete