Write all the functions for queue implementation using link-list.
// Queue is based on FIFO(first in first out) order.
// Time complexity for insertion as well as deletion is O(1).
// Time complexity for insertion as well as deletion is O(1).
#include<stdio.h>
#include<stdlib.h>
typedef struct nodetype
{
int data;
struct nodetype *next;
} node;
typedef struct qtype{
node *r;
node *f;
} qtype ;
qtype *q;
#include<stdlib.h>
typedef struct nodetype
{
int data;
struct nodetype *next;
} node;
typedef struct qtype{
node *r;
node *f;
} qtype ;
qtype *q;
qtype *create(struct qtype *q);
qtype *insert(struct qtype *q,int info);
qtype *delete(struct qtype *q);
void display(struct qtype *q);
main()
{
int x,info;
while(x!=5)
{
printf("Enter your choice in formation of queue\n");
printf("1.Create\n2.Insert\n3.Delete\n4.Display\n5.Exit\n");
scanf("%d",&x);
switch(x)
{
case 1:
q=create(q);
break;
case 2:
printf("Enter the element\t");
scanf("%d",&info);
q=insert(q,info);
break;
case 3:
q=delete(q);
break;
case 4:
display(q);
}
}
}
qtype *create(struct qtype *q)
{
q=(struct qtype *)malloc(sizeof(struct qtype));
q->r=NULL;
q->f=NULL;
return(q);
}
qtype *insert(struct qtype *q,int info)
{
struct nodetype *temp;
temp=(node *)malloc(sizeof(node));
temp->data=info;
temp->next=NULL;
if((q->r==NULL) && (q->f==NULL))
{
q->r=q->f=temp;
}
else
{
q->r->next=temp;
q->r=temp;
}
printf("\n%d\n",q->f->data);
printf("%d\n",q->r->data);
return(q);
}
qtype *delete(struct qtype *q)
{
node *t;
t=q->f;
if(q->r==q->f)
{
printf("%d\n",q->f->data);
free(q->f);
//q->f=q->r=NULL;
}
else
{
printf("%d\n",q->f->data);
q->f=q->f->next;
free(t);
}
return(q);
}
void display(struct qtype *q)
{
node *t;
t=q->f;
if(t!=q->r)
{
while(t!=q->r)
{
printf("%d\t",t->data);
t=t->next;
}
printf("%d",t->data);
}
else
{
printf("%d\t",t->data);
}
printf("\n\n");
}
qtype *insert(struct qtype *q,int info);
qtype *delete(struct qtype *q);
void display(struct qtype *q);
main()
{
int x,info;
while(x!=5)
{
printf("Enter your choice in formation of queue\n");
printf("1.Create\n2.Insert\n3.Delete\n4.Display\n5.Exit\n");
scanf("%d",&x);
switch(x)
{
case 1:
q=create(q);
break;
case 2:
printf("Enter the element\t");
scanf("%d",&info);
q=insert(q,info);
break;
case 3:
q=delete(q);
break;
case 4:
display(q);
}
}
}
qtype *create(struct qtype *q)
{
q=(struct qtype *)malloc(sizeof(struct qtype));
q->r=NULL;
q->f=NULL;
return(q);
}
qtype *insert(struct qtype *q,int info)
{
struct nodetype *temp;
temp=(node *)malloc(sizeof(node));
temp->data=info;
temp->next=NULL;
if((q->r==NULL) && (q->f==NULL))
{
q->r=q->f=temp;
}
else
{
q->r->next=temp;
q->r=temp;
}
printf("\n%d\n",q->f->data);
printf("%d\n",q->r->data);
return(q);
}
qtype *delete(struct qtype *q)
{
node *t;
t=q->f;
if(q->r==q->f)
{
printf("%d\n",q->f->data);
free(q->f);
//q->f=q->r=NULL;
}
else
{
printf("%d\n",q->f->data);
q->f=q->f->next;
free(t);
}
return(q);
}
void display(struct qtype *q)
{
node *t;
t=q->f;
if(t!=q->r)
{
while(t!=q->r)
{
printf("%d\t",t->data);
t=t->next;
}
printf("%d",t->data);
}
else
{
printf("%d\t",t->data);
}
printf("\n\n");
}
Time Complexity
- Insertion - O(1)
- Deletion - O(1)
No comments:
Post a Comment