Sunteți pe pagina 1din 11

EDWARDES COLLEGE PESHAWAR

DATA STRUCTURE
& ALGORITHMS
HND COMPUTING 3rd Module

NAJAM U SAQUIB
11/24/2010

ABOUT ASSIGNMENT
This assignment is aimed to develop students ability to select, evaluate and
implement the suitable data structure. Data structures play an important role in the
computers world. Students will know how to select an appropriate data structure and its
implementation.
INTRODUCTION
[1]

A data structure is a particular way of storing and organizing data in

a computer so that it can be used efficiently. The implementation of a data structure usually
requires writing a set of procedures that create and manipulate instances of that structure. The
efficiency of a data structure cannot be analyzed separately from those operations. There are
two types of data structures. They are:
1) Linear Data Structure
2) Non-Linear Data Structure
[2]
Linear Data Structure
A linear data structure is one in which elements form a sequence. e.g.
Linked List, Array, Stack, and Queue.
Non-Linear Data Structure
A non-linear data structure is one in which elements do not form a
sequence e.g. Tree, Graph etc

P1: Describe in detail how basic operations are implemented on the data structures. For
example traversing, searching, insertion, deletion.

SELECTED DATASTRUCTURE
The data structure I have selected to fulfill my assignment tasks is
Queue. Queue is a linear data structure that permits insertion of new element at one end and
deletion of an element at the other end. The end at which the deletion of an element take
place is called front, and the end at which insertion of a new element can take place is called
rear. The deletion or insertion of elements can take place only at the front or rear end of the
11

list respectively. The first element that gets added into the queue is the first one to get
removed from the list. Hence, queue is also referred to as First-In-First-Out list (FIFO).
Queues can be represented using both arrays as well as linked lists.
ARRAY IMPLEMENTATION OF QUEUE
[3]
If queue is implemented using arrays, the size of the array should be
fixed maximum allowing the queue to expand or shrink.
Operations on a Queue
There are two common operations one in a queue. They are addition of an
element to the queue and deletion of an element from the queue. Two variables front and rear
are used to point to the ends of the queue. The front points to the front end of the queue
where deletion takes place and rear points to the rear end of the queue, where the addition of
elements takes place. Initially, when the queue is full, the front and rear is equal to -1.
Insertion (x)
An element can be added to the queue only at the rear end of the queue. Before adding an
element in the queue, it is checked whether queue is full. If the queue is full, then addition
cannot take place. Otherwise, the element is added to the end of the list at the rear side.

Algorithm for insertion (front, rear, max)


This algorithm will insert a value in array at rear.
STEP 1: Increment the 'rear' by 1
STEP 2: Check for the 'queue overflow' condition. If queue not overflowing then go to step-3
else say "queue overflow" and quit
STEP 3: Put the new element at the position pointed by 'rear'

Deletion ( )
The deletion operation deletes the element from the front of the queue. Before deleting and
element, it is checked if the queue is empty. If not the element pointed by front is deleted
from the queue and front is now made to point to the next element in the queue.

11

Algorithm for deletion (front, rear)


This algorithm will delete value in array from front.
STEP 1: If the queue is empty then quit else go to step-2
STEP 2: Put a NULL value at the position pointed by 'front'. Increment the 'front' by 1.
LINKED LIST IMPLEMENTATION OF QUEUE
Queue can be represented using a linked list. Linked lists do not have any
restrictions on the number of elements it can hold. Space for the elements in a linked list is
allocated dynamically; hence it can grow as long as there is enough memory available for
dynamic allocation. The queue represented using linked list would be represented as shown.
The front pointer points to the front of the queue and rear pointer points to the rear of the
queue.

Insertion(x)
In linked list representation of queue, the addition of new element to the queue takes place at
the rear end. It is the normal operation of adding a node at the end of a list.
Algorithm for insertion (front, rear)
This algorithm will insert a value in node at rear.
STEP 1: Create the new node
STEP 2: If the Queue is empty go to step-3; or else go to step-4
STEP 3: Make the 'front', 'rear' and 'queue' pointers to point to it. Quit.
STEP 4: Add it to the end of the queue. Shift 'rear' pointer to point to this newly added
element. Quit.
Deletion of Queue
The deletion operation deletes the first element from the front end of the queue. Initially it is
checked, if the queue is empty. If it is not empty, then return the value in the node pointed by
front, and moves the front pointer to the next node.

11

Algorithm for deletion (front, rear)


This algorithm will delete a value in node at front.
STEP 1: Check for the "queue empty" condition. If it is then go to step-2 or else step-3
STEP 2: Give away a "queue empty" message and quit
STEP 3: Delete the element. If this was the last element to be deleted then go to step-4 or else
step-5
STEP 4: Make our 'front', 'rear' and 'queue' pointers NULL
STEP 5: Shift your 'front' and 'queue' pointer ahead to point to the next element in
the queue
P2: Describe a typical application where such a data structure may be found. For
example, find the area where such a data structure is implemented.
[4]
APPLICATION OF QUEUE
The major application of queue is in the operating system and in
networks. All the processes that are submitted to your processor or to CPU are first taken in a
queue which is then processed as various algorithms. Similarly on a network if a number of
users want to access a resource their request is taken in a queue and then processed. [5]
There are many applications of queue. Some of them are following:

Operating systems often maintain a queue of processes that are ready to execute or
that are waiting for a particular event to occur.

Computer systems must often provide a holding area for messages between two
processes, two programs, or even two systems. This holding area is usually called a
buffer and is often implemented as a queue.

In operating systems, for controlling access to shared system resources such as


printers, files, communication lines, disks and tapes.

11

In the situation where there are multiple users or a networked computer system, you
probably share a printer with other users. When you request to print a file, your
request is added to the print queue. When your request reaches the front of the print
queue, your file is printed. This ensures that only one person at a time has access to
the printer and that this access is given on a first-come, first-served basis.

For simulation of real-world situations. For instance, a new bank may want to know
how many tellers to install. The goal is to service each customer within a "reasonable"
wait time, but not have too many tellers for the number of customers. To find out a
good number of tellers, they can run a computer simulation of typical customer
transactions using queues to represent the waiting customers.

When placed on hold for telephone operators. For example, when you phone the tollfree number for your bank, you may get a recording that says, "Thank you for calling
A-1 Bank. Your call will be answered by the next available operator. Please wait."
This is a queuing system.

Queue is also used in Disk Drivers. It maintains the queue of disk input/output
requests.

Old Missile Command System computers used queues to queue up missiles that were
going to attack the ships, and then launch them at the targets during the next cycle.

P3: Write code to implement a selection of data structure. For example implement the
selected data structure in any language you know.
[6]
ARRAY CODE OF INSERTION FOR QUEUE IN C
#include<stdio.h)

//Header file

#include<iostream.h>

//Header file

#include<math.h>

//Header file

Void main ( )

//main body of the program starts inside this body

static int queue[max];


int front = 0, rear = -1; //when the queue is empty

11

insert (int new)


{
rear = rear + 1;

//addition of value

if(rear < max)

//if condition

queue[rear] = new;
else
printf("Queue overflow");
getch( )

//displaying the message QUEUE OVERFLOW

// to get character

}
ARRAY CODE OF DELETION FOR QUEUE IN C
#include<stdio.h)

//Header file

#include<iostream.h>

//Header file

#include<math.h>

//Header file

Void main ( )

//main body of the program starts inside this body

static int queue[max];


int front = 0, rear = -1;

//when the queue is empty

int delete( )
{
int del_val =0; //holds the deleted value
if(front > rear)
printf("Queue empty");

//IF condition applies

//Here a message is displayed" Queue is empty"

else
{
del_val = queue[front];
queue[front] = NULL; //this represents that the element has been deleted

11

front = front + 1;

//increment of value

}
return(del_val);
getch( ) // to get character
}
LINK LIST CODE OF INSERTION FOR QUEUE IN C
#include<stdio.h)

//Header file

#include<iostream.h>

//Header file

#include<math.h>

//Header file

struct node
{
int value;

//declaration of variable

struct node *next;


}
struct node *queue, *front, *rear;
main( ) //main body of the program starts inside this body
{
queue = front = rear = NULL; //queue is initially empty
insert(new_value);

//insertion of new item

insert(new_value);
}
insert(int value)
{
struct node *new;
new = (struct node *)malloc(sizeof(node));

11

new->value = value;
new->next = NULL;
if(front == NULL)

//IF condition starts

{
queue = new;
front = rear = queue;
}
else
{
rear->next = new;
rear = new;
getch ( ) //to get character
}
}

LINK LIST CODE OF DELETION FOR QUEUE IN C


#include<stdio.h)

//Header file

#include<iostream.h>

//Header file

#include<math.h>

//Header file

struct node
{
int value;
struct node *next;
}
struct node *queue, *front, *rear;

11

main( )

//main body of the program starts inside this body

{
queue = front = rear = NULL; //queue is initially empty
del_value = delete( );

//Delete item

del_value = delete( );
}
int delete( )

//Declaration of variable

{
int delval = 0;
if(front == NULL)

//IF condition applies

printf("queue empty");

//Display an output "QUEUE EMPTY"

else
{
delval = front->value;
if(front->next == NULL)

//2nd IF condition applies

{
free(front);
queue = front = rear = NULL;
}
else
{
front = front->next;
free(queue);
queue = front;
getch ( )

11

}
}
}

REFERENCES

[1]

WIKIPEDIA

http://en.wikipedia.org/wiki/Data_structure

[2]

SAWAL IBIBO

http://sawaal.ibibo.com/computers-and-technology/what-linear-nonlinear-data-structures237280.html

[3]

SCRIBD

http://www.scribd.com/doc/31428457/Linear-Data-Structures

[4]

ITGO

http://datastructures.itgo.com/staque/queues/listimple/insert.htm

[5]

JUNIATA

http://www.juniata.edu/faculty/kruse/cs240/queues.htm

[6]

UREGINA

http://www.cs.uregina.ca/Links/class-info/210/Queue/

11

S-ar putea să vă placă și