Presentation is loading. Please wait.

Presentation is loading. Please wait.

Today’s Agenda  Access Restricted Lists Queue. Examples.

Similar presentations

Presentation on theme: "Today’s Agenda  Access Restricted Lists Queue. Examples."— Presentation transcript:

1 Today’s Agenda  Access Restricted Lists Queue

2 Examples

3 Implementation

4 queueDef.h typedef int Element; typedef struct queueNode qNode; typedef struct queueNode *Link; typedef enum{TRUE=1,FALSE=0}boolean; struct queueNode { Element e; Link next; }; typedef struct { Link front; Link rear; int size; }Queue;

5 queueOps.h #include"queueDef.h" extern Queue createQueue(); extern Link createNode(Element e); extern boolean isEmpty(Queue q); extern Queue enQueue(Queue q,Element e); extern Queue dQueue(Queue q); extern Element getElement(Queue q);

6 queueOps.c  #include"queueDef.h"  #include

7 queueOps.c (contd) Queue createQueue() { Queue q; q.front = NULL; q.rear = NULL; q.size = 0; return q; }

8 queueOps.c (contd) Link createNode(Element e) { Link newNode; newNode = (Link)malloc(sizeof(qNode)); if(newNode == NULL) { printf("insufficient memory!........\n"); exit(0); } newNode->e = e; newNode->next = NULL; return newNode; }

9 queueOps.c (contd) boolean isEmpty(Queue q) { if(q.front == NULL) return TRUE; else return FALSE; }

10 queueOps.c (contd) Queue enQueue(Queue q,Element e) { Link newNode; newNode = createNode(e); if(isEmpty(q) == TRUE) { q.rear = newNode; q.front = q.rear; } else { q.rear->next = newNode; q.rear = q.rear->next; } q.size++; return q; }

11 queueOps.c (contd) Queue dQueue(Queue q) { Link temp; if(isEmpty(q)== TRUE) printf("Queue is Empty!....\n"); else { temp = q.front; q.front = q.front->next; free(temp); q.size--; } return q; }

12 queueOps.c (contd) Element getElement(Queue q) //return element at the front { return (q.front->e); }

13 main.c #include"queueOps.h" #include int main() { Queue q; int i; int a[5] = {11,22,33,44,55}; q = createQueue(); for(i=0;i<5;i++) q = enQueue(q,a[i]); q = dQueue(q); printf("Element at the front is %d\n",getElement(q)); return 0; }

14 Exercise Problem 1 Write a program to implement a queue using linked list for storing passenger names. Use multiple files to write the program.

15 Exercise Problem 2  Consider a queue at a movie ticket counter. A person can buy at most 2 tickets at a time. If a person want to buy more than 2 tickets then he/she will have to again enter the queue buy the ticket.  Implement the above scenario using queues. Use linked list representation.

Download ppt "Today’s Agenda  Access Restricted Lists Queue. Examples."

Similar presentations

Ads by Google