03 Queue
03 Queue
1
Queue - What?
Let’s Refresh our Mind
Refresh your mind
3. Printer
4. Function calls
5. Backtracking
6. Delimiter Checking
7. All of mentioned
Queue- What
20 Add (20)
50 Add( 5)
-3
30 Add(30)
Delete
Delete
Add(0)
Add( -3)
Queue
OPERATIONS PERFORMED ON
QUEUE
#define MAX 10
typedef char EntryType;
typedef struct {
int front;
int rear;
EntryType entry[MAX];
} QueueType;
Queue Implementation
D C B A
• A queue
Max rear front
• After A leaves, D C B
rear front
How can we insert a new element?!!
Queue Implementation
After A leaves, D C B
A better solution is a
circular Queue
Circular Queue
rear front
front
rear
#define MAX 10
typedef char EntryType;
typedef struct {
int front;
int rear;
int size;
EntryType entry[MAX];
} QueueType;
Queue Implementation
Create operation:
Pre: None.
Post: The queue is initialized to be empty.
void CreateQueue(QueueType *q){
q->front= 0;
q->rear = max -1;
q->size = 0;
}
Queue Implementation
Enqueue operation:
Pre: The queue is initialized and is not full.
Post: Item is added to the end of the queue.
void Enqueue(EntryType item, QueueType *q){
q->rear ++; It is for linear not circular
q->rear = (q->rear+1)%Max; // to circulate the queue
q->entry[q->rear] = item;
q->Size ++;
}
Queue Implementation
Dequeue operation:
Pre: The Queue is initialized and is not empty.
Post: The front element of the Queue is removed from it and is
assigned to item.
void dequeue (Entry *item, QueueType *q){
*item = q->entry[ q->front];
q->front = (q->front +1)% MAX
q->Size--;
}
Exercise
…
TraverseQueue(&q, &Print);
}
Excercise2
We (as a user for QueueADT) have two filled queues; the first
queue holds section code while the other holds group code
(where number of groups inside the section is maximum 10).
Merge those numbers (section code*10+group code) in a
newly created queue.
Exercises3