Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lech 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Data Structure Lect.

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.

Implementing Queue using Arrays


Queue can be represented using array with type and size depending on the queue contents. Two integer
variables needed from where a new item can be deleted from or inserted into the queue.

Two functions needed:

1- insert: A function to insert a new item at the rear end


2- delet : A function to delete an item from front.

int queue[qsize], front = -1, rear = -1;

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

Step4- Increment rear by one to point the next empty space.

Step 5 − Add data element to the queue location, where the rear is pointing.

2
Data Structure Lect. 7

void insert(int queue[], int &front, int &rear, int value)


{
if (rear == qsize - 1)
cout << "Queue is FULL";
else
{
rear++;
queue[rear] = value;
if (front == -1)
front++;
}

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: -

Step 1 − Check if the queue is not empty.


Step 2 − increment front pointer to point to the next available data element.

Step 3 – Access the data where front is pointing

Step 4 −. Return the deleted item.

Step 5 – Otherwise, halt execution.

3
Data Structure Lect. 7

void delet(int queue[], int &front, int &rear)


{
if (front == -1)
cout << "queue is Empty";
else if (front<rear)
{
cout << queue[front];
front++;
}
else
{
cout << queue[front];
front=-1;
rear = -1;
}
}

#include "stdafx.h"
#include<iostream>
using namespace std;
#define qsize 5

void insert(int queue[], int &front, int &rear, int value);


void delet(int queue[], int &front, int &rear);

void main()
{
int queue[qsize], front = -1, rear = -1, value;

for (int i = 0; i < qsize; i++)


{
cout << "Enter a new data item";
cin >> value;
insert(queue, front, rear, value);
}

for (int i = 0; i < qsize; i++)


{
delet(queue, front, rear);
}
}

4
Data Structure Lect. 7

Implementing Queue using Structure:


Queue can be represented using structure with three members. First member is an array of actual data.
Second member is an integer variable from where a new item can be inserted into the queue. Third Member
is an integer variable from where a new item can be inserted into the queue.

#define queuezis 10
struct queue
{
int data[queuezis];
int rear;
int front;
};

void insert(queue *que, int item)


{
if (que->rear > queuezis - 1)
{
cout << "FULL QUEUE";
return;
}
else
{
que->rear++;
que->data[que->rear] = item;
if (que->front == -1)
que->front = 0;
}
}

5
Data Structure Lect. 7

int delet(queue *que)


{
int v;
if (que->front == -1)
return -1;
else
if (que->front < que->rear)
{
v = que->data[que->front];
que->front++;
return v;
}
else
if (que->front == que->rear)
{
v = que->data[que->front];
que->front = -1;
que->rear = -1;
return v;
}

- Main function:
#define queuezis 4

struct queue
{
int data[queuezis];
int rear;
int front;
};
void insert(queue *que, int item);
int delet(queue *que);

int _tmain(int argc, _TCHAR* argv[])


{
queue qu;
qu.rear = -1;
qu.front = -1;
int i,item, value;
for (i = 0; i < queuezis; i++)
{
cout << "ENTER NEW ITEM \n";
cin >> item;
insert(&qu, item);
}
for (i = 0; i < 5; i++)
{
value = delet(&qu);
if (value == -1)
cout << "EMPTY QUEUE";
else
cout << value;
}
cout << endl;
return 0;
}

You might also like