Showing posts with label Queue. Show all posts
Showing posts with label Queue. Show all posts

Saturday, April 23, 2011

Queue Circular

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

#define MAX 5

struct cqueue
{
int info[MAX];
int front, rear, count;
} cq;

int main(void)
{
int ch, n;
cq.front = 0;
cq.rear = -1;
cq.count = 0;
while(1)
{
clrscr();
f_disp(&cq);
printf("\n\n1. Enqueue \n2. Dequeue\n3. Display\n4. Exit.");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueue(&cq, n);
break;
case 2: n = dequeue(&cq);
if(n!=-1)
{
printf("Dequeued : %d",n);
getch();
}
else
{
printf("Queue underflow...");
getch();
break;
}
break;
case 3: disp(&cq);
break;
case 4: exit(0);
default: printf("Invalid choice. \nPlease enter 1-4");
}
}

getch();
return 0;
}

/* add node to queue list */
enqueue(struct cqueue *q, int n)
{
if(q->count == MAX)
{
printf("\nQueue overflow...");
getch();
return;
}
q->rear = (q->rear+1)%MAX;
q->info[q->rear] = n;
q->count++;
}

/* delete node from queue list */
dequeue(struct cqueue *q)
{
int n;
if(q->count==0)
{
printf("Queue underflow...");
getch();
return -1;
}

n = q->info[q->front];
q->front = (q->front+1)%MAX;
q->count--;
return n;
}


/* display items of queue */
disp(struct cqueue *q)
{
int i, n = q->count;
clrscr();
if(n == 0)
{
printf("No item...");
getch();
return;
}
else
{
printf("\n\n\tQueue are : \n");
for(i=q->front;n>0;n--)
{
printf("\t\t\t%d\n ", q->info[i]);
i = (i+1)%MAX;
}
}
printf("\n\nPress any key to return to main menu...");
getch();

}

/*display items on head of menu display. */
f_disp(struct cqueue *q)
{
int i, n = q->count;
if(n == 0)
{
printf("No item...");
return;
}
printf("Queue are : \n\t\t");
for(i=q->front;n>0;n--)
{
printf("%d, ", q->info[i]);
i = (i+1)%MAX;
}
}

Queue (Priority)

/* priority queue */

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

struct q
{
int info;
struct q *next;
} queue, *rear, *front;

int main(void)
{
int n, ch;
while(1)
{
clrscr();
f_disp(front);
printf("\n\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit.");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueuePqueue(front, n);
break;
case 2: n = dequeue(front);
if(n!=-1)
{
printf("Dequeued : %d",n);
getch();
}
break;
case 3: disp(front);
break;
case 4: exit(0);
default: printf("Invalid choice. \nPlease enter 1-4");
}
}

getch();
return 0;
}

/* add node to queue list */

enqueuePqueue(struct q *q, int n)
{
struct q *temp, *p = NULL;
temp = (struct q*)malloc(sizeof(struct q));
temp->info = n;
if(q==NULL)
{
front = rear = temp;
temp->next = NULL;
}
else if(ninfo)
{
temp->next = q;
front = temp;
}
else
{
while(q!=NULL&&q->infonext;
}
p->next = temp;
temp->next = q;
}
}



/* delete node from queue list */
dequeue(struct q *d)
{
int n;
if(d==NULL)
{
printf("Queue underflow...");
return -1;
}

n = d->info;
front = d->next;

if(front == NULL)
rear = NULL;
free(d);
return n;
}

/* display items of queue */
disp(struct q *dis)
{
clrscr();
if(dis == NULL)
{
printf("No queue to display");
getch();
return;
}
else
{
printf("Queue is : \n");
while(dis!=NULL)
{
printf("\t\t%d\n",dis->info);
dis = dis->next;
}
getch();
}
}

/*display items on head of menu display. */
f_disp(struct q *s)
{
if(s == NULL)
printf("No item...");
else
{ printf("\nItems are : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s = s->next;
}
}
}

Queue Circular

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

#define MAX 5

struct cqueue
{
int info[MAX];
int front, rear, count;
} cq;

int main(void)
{
int ch, n;
cq.front = 0;
cq.rear = -1;
cq.count = 0;
while(1)
{
clrscr();
f_disp(&cq);
printf("\n\n1. Enqueue \n2. Dequeue\n3. Display\n4. Exit.");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueue(&cq, n);
break;
case 2: n = dequeue(&cq);
if(n!=-1)
{
printf("Dequeued : %d",n);
getch();
}
else
{
printf("Queue underflow...");
getch();
break;
}
break;
case 3: disp(&cq);
break;
case 4: exit(0);
default: printf("Invalid choice. \nPlease enter 1-4");
}
}

getch();
return 0;
}

/* add node to queue list */
enqueue(struct cqueue *q, int n)
{
if(q->count == MAX)
{
printf("\nQueue overflow...");
getch();
return;
}
q->rear = (q->rear+1)%MAX;
q->info[q->rear] = n;
q->count++;
}

/* delete node from queue list */
dequeue(struct cqueue *q)
{
int n;
if(q->count==0)
{
printf("Queue underflow...");
getch();
return -1;
}

n = q->info[q->front];
q->front = (q->front+1)%MAX;
q->count--;
return n;
}


/* display items of queue */
disp(struct cqueue *q)
{
int i, n = q->count;
clrscr();
if(n == 0)
{
printf("No item...");
getch();
return;
}
else
{
printf("\n\n\tQueue are : \n");
for(i=q->front;n>0;n--)
{
printf("\t\t\t%d\n ", q->info[i]);
i = (i+1)%MAX;
}
}
printf("\n\nPress any key to return to main menu...");
getch();

}

/*display items on head of menu display. */
f_disp(struct cqueue *q)
{
int i, n = q->count;
if(n == 0)
{
printf("No item...");
return;
}
printf("Queue are : \n\t\t");
for(i=q->front;n>0;n--)
{
printf("%d, ", q->info[i]);
i = (i+1)%MAX;
}
}

Queue (Priority)

/* priority queue */

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

struct q
{
int info;
struct q *next;
} queue, *rear, *front;

int main(void)
{
int n, ch;
while(1)
{
clrscr();
f_disp(front);
printf("\n\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit.");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueuePqueue(front, n);
break;
case 2: n = dequeue(front);
if(n!=-1)
{
printf("Dequeued : %d",n);
getch();
}
break;
case 3: disp(front);
break;
case 4: exit(0);
default: printf("Invalid choice. \nPlease enter 1-4");
}
}

getch();
return 0;
}

/* add node to queue list */

enqueuePqueue(struct q *q, int n)
{
struct q *temp, *p = NULL;
temp = (struct q*)malloc(sizeof(struct q));
temp->info = n;
if(q==NULL)
{
front = rear = temp;
temp->next = NULL;
}
else if(ninfo)
{
temp->next = q;
front = temp;
}
else
{
while(q!=NULL&&q->infonext;
}
p->next = temp;
temp->next = q;
}
}



/* delete node from queue list */
dequeue(struct q *d)
{
int n;
if(d==NULL)
{
printf("Queue underflow...");
return -1;
}

n = d->info;
front = d->next;

if(front == NULL)
rear = NULL;
free(d);
return n;
}

/* display items of queue */
disp(struct q *dis)
{
clrscr();
if(dis == NULL)
{
printf("No queue to display");
getch();
return;
}
else
{
printf("Queue is : \n");
while(dis!=NULL)
{
printf("\t\t%d\n",dis->info);
dis = dis->next;
}
getch();
}
}

/*display items on head of menu display. */
f_disp(struct q *s)
{
if(s == NULL)
printf("No item...");
else
{ printf("\nItems are : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s = s->next;
}
}
}

Queue Double (can insert through both end)

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

typedef struct q
{
int info;
struct q *next;
} queue;

queue *rear, *front;

int main(void)
{
int ch, n;
clrscr();
while(1)
{
clrscr();
f_disp(front);
printf("\n1. Enqueue rear\n2. Enqueue Front\n3. Dequeue front\n4. Dequeue rear\n5. Display\n6. Exit");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: n = getinput();
enqueue_rear(rear,n);
break;
case 2: n = getinput();
enqueue_front(front, n);
break;
case 3: dequeue_front(front);
break;
case 4: dequeue_rear(front);
break;
case 5: disp(front);
break;
case 6: exit(0);
default: printf("Invalid choice.\nPlease enter 1 - 6\n");
getch();
}
}
}

int getinput()
{
int n;
printf("Enter the new info : ");
scanf("%d",&n);
return n;
}

enqueue_rear(queue *s, int n)
{
queue *temp;
temp = (queue *)malloc(sizeof(queue));
temp->info = n;
temp->next = NULL;
rear = temp;
if(s==NULL)
front = temp;
else
s->next = temp;
}

enqueue_front(queue *s, int n)
{
queue *temp;
temp = (queue *)malloc(sizeof(queue));
temp->info = n;
temp->next = s;
front = temp;
if(s == NULL)
rear = temp;
}

dequeue_front(queue *s)
{
int n;
if(s!=NULL)
{
n = s->info;
front = s->next;
if(s->next == NULL)
rear = NULL;
free(s);
return n;
}
else
{ clrscr();
printf("\n\n\t\tQueue underflow...");
getch();
return -1;
}
getch();
}

dequeue_rear(queue *s)
{
queue *p;
int n;
if(s == NULL)
{
clrscr();
printf("\n\n\t\tQueue Underflow...");
getch();
return -1;
}
if(s == rear)
{
n = s->info;
front = rear = NULL;
free(s);
}
else
{
for(; (s->next)->next!=NULL;s=s->next);
n = rear->info;
free(rear);
}
s->next = NULL;
rear = s;
return n;
}


disp(queue *s)
{
clrscr();
if(s!=NULL)
{
printf("Items in queue : \n\t");
while(s!=NULL)
{
printf("\n\t\t%5d ",s->info);
s = s->next;
}
}
else
printf("No item in queue.");
getch();
}

f_disp(queue *s)
{
if(s!=NULL)
{
printf("Items in queue : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s = s->next;
}
}
else
printf("No item in queue.");
}

Queue Double (can insert through both end)

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

typedef struct q
{
int info;
struct q *next;
} queue;

queue *rear, *front;

int main(void)
{
int ch, n;
clrscr();
while(1)
{
clrscr();
f_disp(front);
printf("\n1. Enqueue rear\n2. Enqueue Front\n3. Dequeue front\n4. Dequeue rear\n5. Display\n6. Exit");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: n = getinput();
enqueue_rear(rear,n);
break;
case 2: n = getinput();
enqueue_front(front, n);
break;
case 3: dequeue_front(front);
break;
case 4: dequeue_rear(front);
break;
case 5: disp(front);
break;
case 6: exit(0);
default: printf("Invalid choice.\nPlease enter 1 - 6\n");
getch();
}
}
}

int getinput()
{
int n;
printf("Enter the new info : ");
scanf("%d",&n);
return n;
}

enqueue_rear(queue *s, int n)
{
queue *temp;
temp = (queue *)malloc(sizeof(queue));
temp->info = n;
temp->next = NULL;
rear = temp;
if(s==NULL)
front = temp;
else
s->next = temp;
}

enqueue_front(queue *s, int n)
{
queue *temp;
temp = (queue *)malloc(sizeof(queue));
temp->info = n;
temp->next = s;
front = temp;
if(s == NULL)
rear = temp;
}

dequeue_front(queue *s)
{
int n;
if(s!=NULL)
{
n = s->info;
front = s->next;
if(s->next == NULL)
rear = NULL;
free(s);
return n;
}
else
{ clrscr();
printf("\n\n\t\tQueue underflow...");
getch();
return -1;
}
getch();
}

dequeue_rear(queue *s)
{
queue *p;
int n;
if(s == NULL)
{
clrscr();
printf("\n\n\t\tQueue Underflow...");
getch();
return -1;
}
if(s == rear)
{
n = s->info;
front = rear = NULL;
free(s);
}
else
{
for(; (s->next)->next!=NULL;s=s->next);
n = rear->info;
free(rear);
}
s->next = NULL;
rear = s;
return n;
}


disp(queue *s)
{
clrscr();
if(s!=NULL)
{
printf("Items in queue : \n\t");
while(s!=NULL)
{
printf("\n\t\t%5d ",s->info);
s = s->next;
}
}
else
printf("No item in queue.");
getch();
}

f_disp(queue *s)
{
if(s!=NULL)
{
printf("Items in queue : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s = s->next;
}
}
else
printf("No item in queue.");
}

Queue (Dynamic implementation)

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

typedef struct q
{
int info;
struct q *next;
} queue;
queue *rear, *front;

int main(void)
{
int n, ch;
while(1)
{
clrscr();
f_disp(front);
printf("\n\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit.");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueue(rear, n);
break;
case 2: n = dequeue(front);
if(n!=-1)
{
printf("Dequeued : %d",n);
getch();
}
break;
case 3: disp(front);
break;
case 4: exit(0);
default: printf("Invalid choice. \nPlease enter 1-4");
}
}

getch();
return 0;
}

/* add node to queue list */
enqueue(queue *s, int n)
{
queue *temp;
temp = (queue *)malloc(sizeof(queue));
temp->info = n;
temp->next = NULL;
rear = temp;
if(s==NULL)
front = temp;
else
s->next = temp;
}

/* delete node from queue list */
dequeue(queue *d)
{
int n;
if(d==NULL)
{
printf("Queue underflow...");
return -1;
}

n = d->info;
front = d->next;

if(front == NULL)
rear = NULL;
free(d);
return n;
}

/* display items of queue */
disp(queue *dis)
{
clrscr();
if(dis == NULL)
{
printf("No queue to display");
getch();
return;
}
else
{
printf("Queue is : \n");
while(dis!=NULL)
{
printf("\t\t%d\n",dis->info);
dis = dis->next;
}
getch();
}
}

/*display items on head of menu display. */
f_disp(queue *s)
{
if(s == NULL)
printf("No item...");
else
{ printf("\nItems are : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s = s->next;
}
}
}

Queue (Dynamic implementation)

#include "conio.h"
#include "stdio.h"
#include "stdlib.h"

typedef struct q
{
int info;
struct q *next;
} queue;
queue *rear, *front;

int main(void)
{
int n, ch;
while(1)
{
clrscr();
f_disp(front);
printf("\n\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit.");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueue(rear, n);
break;
case 2: n = dequeue(front);
if(n!=-1)
{
printf("Dequeued : %d",n);
getch();
}
break;
case 3: disp(front);
break;
case 4: exit(0);
default: printf("Invalid choice. \nPlease enter 1-4");
}
}

getch();
return 0;
}

/* add node to queue list */
enqueue(queue *s, int n)
{
queue *temp;
temp = (queue *)malloc(sizeof(queue));
temp->info = n;
temp->next = NULL;
rear = temp;
if(s==NULL)
front = temp;
else
s->next = temp;
}

/* delete node from queue list */
dequeue(queue *d)
{
int n;
if(d==NULL)
{
printf("Queue underflow...");
return -1;
}

n = d->info;
front = d->next;

if(front == NULL)
rear = NULL;
free(d);
return n;
}

/* display items of queue */
disp(queue *dis)
{
clrscr();
if(dis == NULL)
{
printf("No queue to display");
getch();
return;
}
else
{
printf("Queue is : \n");
while(dis!=NULL)
{
printf("\t\t%d\n",dis->info);
dis = dis->next;
}
getch();
}
}

/*display items on head of menu display. */
f_disp(queue *s)
{
if(s == NULL)
printf("No item...");
else
{ printf("\nItems are : \n\t");
while(s!=NULL)
{
printf("%d, ",s->info);
s = s->next;
}
}
}

Queue (Static implementation)

/* static implementation of queue */
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"

#define MAX 5

typedef struct
{
int info[MAX];
int front , rear;
} queue;

queue que;

main()
{
int n, ch;
clrscr();
que.front = 0;
que.rear = -1;
while(1)
{
clrscr();
f_disp(que);
puts("1. Enqueue\n2. Dequeue\n3. Display\4. Exit");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueue(&que, n);
break;
case 2: n = dequeue(&que);
if(n!=-1)
printf("Dequeued %d", n);
else
printf("Queue underflow...");
getch();
break;
case 3: disp(que);
break;
case 4: exit(0);
default : printf("Invalid choice.");
}
}
}

enqueue(queue *q, int n)
{
if(q->rear == MAX)
{
puts("Queue overflow...");
getch();
return;
}
q->info[++q->rear] = n;
}

dequeue(queue *q)
{
int n;
if(q->fornt > q->rear)
{
puts("Queue underflow...");
getch();
return -1;
}

n = q->inf[q->front++];
return n;
}

disp(queue q)
{
int i;
for(i=q.fornt; i<=q.rear; i++) printf("\n %d", q->info[i]);
getch();
}

f_disp(queue q)
{
int i;
for(i=q.fornt; i<=q.rear; i++) printf("%d, ", q->info[i]);

}

Queue (Static implementation)

/* static implementation of queue */
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"

#define MAX 5

typedef struct
{
int info[MAX];
int front , rear;
} queue;

queue que;

main()
{
int n, ch;
clrscr();
que.front = 0;
que.rear = -1;
while(1)
{
clrscr();
f_disp(que);
puts("1. Enqueue\n2. Dequeue\n3. Display\4. Exit");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter info : ");
scanf("%d",&n);
enqueue(&que, n);
break;
case 2: n = dequeue(&que);
if(n!=-1)
printf("Dequeued %d", n);
else
printf("Queue underflow...");
getch();
break;
case 3: disp(que);
break;
case 4: exit(0);
default : printf("Invalid choice.");
}
}
}

enqueue(queue *q, int n)
{
if(q->rear == MAX)
{
puts("Queue overflow...");
getch();
return;
}
q->info[++q->rear] = n;
}

dequeue(queue *q)
{
int n;
if(q->fornt > q->rear)
{
puts("Queue underflow...");
getch();
return -1;
}

n = q->inf[q->front++];
return n;
}

disp(queue q)
{
int i;
for(i=q.fornt; i<=q.rear; i++) printf("\n %d", q->info[i]);
getch();
}

f_disp(queue q)
{
int i;
for(i=q.fornt; i<=q.rear; i++) printf("%d, ", q->info[i]);

}

Related Posts Plugin for WordPress, Blogger...

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites