Академический Документы
Профессиональный Документы
Культура Документы
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 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.
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
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
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.
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 ( )
11
//addition of value
//if condition
queue[rear] = new;
else
printf("Queue overflow");
getch( )
// 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 ( )
int delete( )
{
int del_val =0; //holds the deleted value
if(front > rear)
printf("Queue 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
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)
{
queue = new;
front = rear = queue;
}
else
{
rear->next = new;
rear = new;
getch ( ) //to get character
}
}
//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( )
{
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)
printf("queue empty");
else
{
delval = front->value;
if(front->next == NULL)
{
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