Lech 7
Lech 7
Lech 7
Queue
Is a linear data structure in which each item can be input to it from the back end called rear and
any item can be deleting from front end called front as shown in the figure below:
Rear Front
C B A
The item A represent the first element in the queue it is pointed by the front of the queue and the
last element of the queue is C it is pointed by the rear of the queue an item can be insert to the queue
it must be after C and the item A can be deleted from the queue as shown below:
Rear Front
D C B
Queue work in (first in first out way FIFO) the first item insert to the queue also the first item that
removed from the queue.
Insert Operation:
1- A new item can be inserted into the queue from the rear of the queue if rear=size-1 this means the
queue is full so there is no chance to add new items to the queue.
2- If the queue is not full a new item can be added by increment rear by one the insert the new item.
1
Data Structure Lect. 7
Delete Operation:
1- The item that pointed by front can be deleted from the queue, if front= -1, this means the queue is
empty and there is no item to be deleted.
2- If the queue is not empty, the delete the new item and decrement front by one.
insert Operation
The following steps should be taken to insert data into a queue: -
Step 1 − Checks if the queue is full.
Step 2 − If the queue is full, print a warning message and halt execution.
Step 3 − If the queue is not full, do steps 4 & 5
Step 5 − Add data element to the queue location, where the rear is pointing.
2
Data Structure Lect. 7
delet Operation
Accessing data from the queue is a process of two tasks − access the data where front is pointing
and remove the data after access. The following steps are taken to perform delet operation: -
3
Data Structure Lect. 7
#include "stdafx.h"
#include<iostream>
using namespace std;
#define qsize 5
void main()
{
int queue[qsize], front = -1, rear = -1, value;
4
Data Structure Lect. 7
#define queuezis 10
struct queue
{
int data[queuezis];
int rear;
int front;
};
5
Data Structure Lect. 7
- Main function:
#define queuezis 4
struct queue
{
int data[queuezis];
int rear;
int front;
};
void insert(queue *que, int item);
int delet(queue *que);