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

Tuesday, June 19, 2012

Doubly linked list implementation of Queue


This is a program on queue using doubly linked list.

# include< stdio.h>
# include< stdlib.h>
struct link
{
int info;
struct link *next;
struct link *prev;
};
struct link *p;
void display(struct link *rec)
{
while(rec != NULL)
{
printf(" %d ",rec->info);
rec = rec->next;
}
}
struct link *insert(struct link *node)
{
struct link *xx,*new1;
xx=node;
p=NULL;
new1 = (struct link *) malloc(sizeof(struct link));
fflush(stdin);
printf("\nEnter the value: ");
scanf("%d", &new1->info);
if (node == NULL)
{
node->next=new1;
new1->prev=node;
new1->next=NULL;
node=new1;
}
else
{
while(xx)
{
xx = xx->next;
p=p->next;
}
p->next = new1;
new1->prev = p;
new1->next = xx;
}
 return node;
}
struct  link * del(struct link *rec)
{
struct link *temp;
if(rec == NULL)
{
printf("\n Empty");
}
else
{
temp = rec->next;
rec->next->prev=temp;
free(rec);
rec = temp;
printf("\n After deletion the queue is as follows:\n");
display(rec);
if(rec  == NULL)
printf("\n Queue is empty");
}
return rec;
}
int selection()
{
int choice;
do
{
printf("\n 1<-Push ");
printf("\n 2<-Pop");
printf("\n 3<-Quit");
printf("\n Input your choice :");
scanf("%d", &choice);
if(choice <1 || choice >3)
printf("\n Incorrect choice-> Try once again");
} while(choice <1 || choice >3);
return choice;
}
void main()
{
struct link *start ;
int choice;
clrscr();
start = NULL;
do
{
choice = selection();
switch(choice)
{
case 1:
start = insert(start);
printf("\n After insertion queue is as follows:\n");
display(start);
break;
case 2:
start = del(start);
break;
default :
printf("\n End of session");
}
} while(choice != 3);
getch();
}

C Language Program on Circular queue


A queue is called circular when the last node comes just before the first node in case of  queue implementation of linked list.There may a header node at the front.

# include < stdio.h>
# include < stdlib.h>
struct link
{
char info[20];
struct link *next;
};
struct link *p,*node,*new1,*start=NULL;
int x=0;
struct link *insert(struct link *nn)
{
if(x==0)
{
/*this will be a header node with no value in it's information part*/
fflush(stdin);
new1 = (struct link *) malloc(sizeof(struct link));
new1->next=new1;
node=new1;
x++;
}
else
{
x++;
node = nn;
p=start;
new1 = (struct link *) malloc(sizeof(struct link));
while(node->next!=node)
{
 node=node->next;
 p=p->next;
 }
 p->next=new1;
 new1->next=node;
 fflush(stdin);
printf("\n Input the node value: ");
gets(new1->info);
node=start->next;
 }
return node;
}
void display(struct link *node)
{
while (node->next!=node)
{
printf(" %s", node->info);
node = node->next;
}
}
struct link *del(struct link *nn)
{
node=nn;
if(node==NULL)
printf("\nDeletion not possible.");
else
{
p=node->next;
free(node);
node=p;
}
return node;
}
void main()
{
struct link *start=NULL;
int k = 0;
char choice='i';

/* at the start of the program you must create a location*/
clrscr();
do
{
  if(x!=0)
 {
printf("\nInsert->i Delete->d Quit->q:");
printf("\nInput the choice : ");
do
   {
choice = getchar();
choice = tolower(choice);
}while(strchr("idq",choice)==NULL);
 printf("Your choice is: %c ", choice);
}

/* end of the if block.At the first iteration of the do loop , no need to take choice from the user as firstly a node will be inserted as a header node. */

switch(choice)
{
case 'i' :
{
       start=insert(start);
  if(x==1) /* after creating the header node x becomes 1*/
  {
printf("\nHeader node is created.");
}
else
{
printf("\nQueue after inserting ");
display(start);
}/* no need to display when only the header node is created. */
break;
}
case 'd' :
{
start=del(start);
printf("\nQueue content after deleteion is as follows:\n");
display(start);
break;
}
case 'q':
{
k = 1;
}
}
} while(!k);
getch();
}

Linear Linked list implementation of Queue


Here is the program on linear linked list implementation of queue where insertion and deletion can be performed as required, no limit.

 # include < stdio.h>
# include < stdlib.h>
struct link
{
char info[20];
struct link *next;
};
struct link *p,*node,*new1,*start=NULL;
struct link *insert(struct link *nn)
{
node = nn;
p=NULL;
new1 = (struct link* ) malloc(sizeof(struct link));
while(node)
{
 node=node->next;
 p=p->next;
 }
 p->next=new1;
 new1->next=node;
 fflush(stdin);
printf("\n Input the node value: ");
gets(new1->info);
node=start->next;
return node;
}
void display(struct link *node)
{
    while (node)
{
printf(" %s", node->info);
node = node->next;
}
}
struct link *del(struct link *nn)
{
node=nn;
if(node==NULL)
printf("\nDeletion not possible.");
else
{
p=node->next;
free(node);
node=p;
}
return node;
}
void main()
{
struct link *start=NULL;
int k = 0;
char choice;
clrscr();
do
{
printf("\nInsert->i Delete->d Quit->q:");
printf("\nInput the choice : ");
do
{
choice = getchar();
choice = tolower(choice);
}while(strchr("idq",choice)==NULL);
printf("Your choice is: %c ", choice);
switch(choice)
{
case 'i' :
{
 start=insert(start);
printf("\nQueue after inserting ");
display(start);
break;
}
case 'd' :
{
start=del(start);
printf("\nQueue content after deleteion is as follows:\n");
display(start);
break;
 }
case 'q':
{
k = 1;
}
}
} while(!k);
getch();
}

Use of array as a queue in C Data Structure


A Queue can be defined as an ordered collection of items  where items are added at one end and items are deleted at the other end. Hence the first item added in a Queue is the first item to be deleted .For this reason Queue is called fifo list. The end from which items are deleted in a Queue is called the front of the Queue and the other end is called the rear of the Queue.

Items can be inserted, at the rear end of the queue , items can be deleted from the front end of the queue. Item insertion has no limit as the rear end can grow but deletion of items from a queue is possible only when there is item in the queue.

Here is the program on queue

# include< stdio.h>
# include< string.h>
# define size 10
int rear, front;
int ch;
int x;
int q[size];
/* This array can work as a queue with maximum ten elements */
int rear = -1;
/* this variable will keep track of the elements inserted*/
int front = -1;
/*front will keep track of the elements deleted*/
void  insert()
{
printf("\n Input the element :");
scanf("%d", &ch);
if(rear< size)
{
/* if block means insertion is possible.*/
rear ++;
q[rear] = ch ;
if(front == -1)
front = 0;
/* after inserting the first element, front becomes 0 .So we can delete elements. */
}
else
printf("\n Can't grow more.");
}
void del()
{
x=0;
if (front == -1)
{
printf("\n Underflow");
}
else
{
ch = q[front];
x=1;
printf("\n Element deleted : %d", ch);
}
if(front == rear)
{
/* front and rear will become same when all the inserted elements are deleted */
front =-1;
rear = -1;
}
else
front = front + 1;
/* after deleting an element front is incremented by 1 to access the next element.*/
}
void display()
{
int i;
if (front == -1)
return;
for(i = front ; i <= rear; i++)
printf(" %d ", q[i]);
}
void main()
{
int k = 0;
char choice;
clrscr();
do
{
printf("\nInsert->i Delete->d Quit->q:");
printf("\nInput the choice : ");
do
{
choice = getchar();
choice = tolower(choice);
}while(strchr("idq",choice)==NULL);
printf("Your choice is: %c ", choice);
switch(choice)
{
case 'i' :
{
insert();
printf("\nQueue after inserting ");
display();
break;
}
case 'd' :
{
del();
if(x==1)
{
printf("\nQueue content after deletion is as follows:\n");
display();
}
break;
}
case 'q':
{
k = 1;
 }
 }
} while(!k);
getch();
}

Subscribe via email

Enter your email address:

Delivered by FeedBurner