Linked List Implementation of Queue in C

1,529 views

Published on

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,529
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
18
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Linked List Implementation of Queue in C

  1. 1. /*Linked List Implementation of the Queue in CAuthor: Kasun Ranga WijeweeraEmail: krw19870829@gmail.comDate: 20130514*/#include<stdio.h>#include<conio.h>#include<alloc.h>typedef struct queueNode *link;struct queueNode{int data;link next;};typedef struct{link front;link rear;}queue;
  2. 2. void initQueue(queue *q){q->front=NULL;q->rear=NULL;}int isEmpty(queue *q){if((q->front)==NULL){return 0;}else{return 1;}}void putQueue(queue *q,int x){link t=(link)malloc(sizeof(struct queueNode));t->data=x;t->next=NULL;
  3. 3. if(isEmpty(q)){q->rear->next=t;q->rear=t;}else{q->front=t;q->rear=t;}}int getQueue(queue *q){link t=q->front;int x=t->data;if((q->front)==(q->rear)){q->front=NULL;q->rear=NULL;}else{
  4. 4. q->front=t->next;}free(t);return x;}void printQueue(queue *q){link t=q->front;while(t!=NULL){printf("%d ",t->data);t=t->next;}}void main(){queue *q;int x;clrscr();
  5. 5. q=(queue*)malloc(sizeof(queue));initQueue(q);putQueue(q,10);putQueue(q,20);putQueue(q,30);putQueue(q,40);putQueue(q,50);printf("n");printQueue(q);if(isEmpty(q))x=getQueue(q);printf("n");printQueue(q);putQueue(q,60);putQueue(q,70);printf("n");printQueue(q);getch();
  6. 6. }

×