Abstract Data Types (Arrays and Queues)
Abstract Data Types (Arrays and Queues)
Array Representation
Arrays can be declared in various ways in different languages. For illustration, let's take C array
declaration.
Arrays can be declared in various ways in different languages. For illustration, let's take C array
declaration.
As per the above illustration, following are the important points to be considered.
• Index starts with 0.
• Array length is 10 which means it can store 10 elements.
• Each element can be accessed via its index. For example, we can fetch an element at
index 6 as 9.
Basic Operations
bool false
char 0
int 0
float 0.0
double 0.0f
void
wchar_t 0
Traverse Operation
Insert operation is to insert one or more data elements into an array. Based on the requirement, a new
element can be added at the beginning, end, or any given index of array.
Algorithm
Example
Result
Let GEORGE be a Linear Array unordered with N elements and K is a positive integer such that
K<=N. Below is the algorithm where ITEM is inserted into the Kth position of GEORGE −
1. Start
2. Set J=N
3. Repeat steps 4 and 5 while J >= K
4. Set GEORGE[J+1] = GEORGE[J]
5. Set J = J-1
6. Set GEORGE[K] = ITEM
7. Reset N = N+1
8. Stop
Example
OUTPUT:
Deletion Operation
Deletion refers to removing an existing element from the array and re-organizing all elements of an
array.
Algorithm
Consider GEORGE is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to delete an element available at the Kth position of GEORGE.
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set GEORGE[J-1] = GEORGE[J]
5. Set J = J+1
6. Set N = N-1
7. Stop
#include <stdio.h>
void main()
{
int GEORGE[] = {1,3,5,7,8};
int k = 3, n = 5;
int i, j;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
{
printf("GEORGE[%d] = %d \n", i, GEORGE[i]);
}
j = k;
while( j < n)
{
GEORGE[j-1] = GEORGE[j];
j = j + 1;
}
n = n -1;
printf("The array elements after deletion :\n");
for(i = 0; i<n; i++)
{
printf("GEORGE[%d] = %d \n", i, GEORGE[i]);
}
}
OUTPUT
You can perform a search for an array element based on its value or its index.
Algorithm
Consider GEORGE is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to find an element with a value of ITEM using sequential search.
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF GEORGE[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Example
Following is the implementation of the above algorithm −
#include <stdio.h>
void main()
{
int GEORGE[] = {1,3,5,7,8};
int item = 5, n = 5;
int i = 0, j = 0;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
{
printf("GEORGE[%d] = %d \n", i, GEORGE[i]);
}
while( j < n)
{
if( GEORGE[j] == item )
{
break;
}
j = j + 1;
}
printf("Found element %d at position %d\n", item, j+1);
}
Update Operation
Update operation refers to updating an existing element from the array at a given index.
Algorithm
Consider GEORGE is a linear array with N elements and K is a positive integer such that K<=N.
Following is the algorithm to update an element available at the Kth position of GEORGE.
1. Start
2. Set GEORGE[K-1] = ITEM
3. Stop
Example
Following is the implementation of the above algorithm −
#include <stdio.h>
void main()
{
int GEORGE[] = {1,3,5,7,8};
int k = 3, n = 5, item = 10;
int i;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
{
printf("GEORGE[%d] = %d \n", i, GEORGE[i]);
}
GEORGE[k-1] = item;
printf("The array elements after updation :\n");
for(i = 0; i<n; i++)
{
printf("GEORGE[%d] = %d \n", i, GEORGE[i]);
}
}
Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open
at both its ends. One end is always used to insert data (enqueue) and the other is used to
remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item
stored first will be accessed first.
Like stacks, Queues can also be represented in an array: In this representation, the Queue is
implemented using the array. Variables used in this case are
• Queue: the name of the array storing queue elements.
• Front: the index where the first element is stored in the array representing the queue.
• Rear: the index where the last element is stored in an array representing the queue.
Basic Operations
Queue operations may involve initializing or defining the queue, utilizing it, and then completely
erasing it from the memory. Here we shall try to understand the basic operations associated with
queues −
• enqueue() − add (store) an item to the queue.
• dequeue() − remove (access) an item from the queue.
Few more functions are required to make the above-mentioned queue operation efficient. These are
−
• peek() − Gets the element at the front of the queue without removing it.
• isfull() − Checks if the queue is full.
• isempty() − Checks if the queue is empty.
In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or storing)
data in the queue we take help of rear pointer.
Let's first learn about supportive functions of a queue −
peek()
This function helps to see the data at the front of the queue. The algorithm of peek() function is as
follows −
Algorithm
begin procedure peek
return queue[front]
end procedure
Implementation of peek() function in C programming language −
Example
int peek() {
return queue[front];
}
isfull()
As we are using single dimension array to implement queue, we just check for the rear pointer to reach
at MAXSIZE to determine that the queue is full. In case we maintain the queue in a circular linked-
list, the algorithm will differ. Algorithm of isfull() function −
end procedure
Implementation of isfull() function in C programming language −
Example
bool isfull() {
if(rear == MAXSIZE - 1)
return true;
else
return false;
}
isempty()
Algorithm of isempty() function −
Algorithm
begin procedure isempty
end procedure
If the value of front is less than MIN or 0, it tells that the queue is not yet initialized, hence empty.
Here's the C programming code −
Example
bool isempty() {
if(front < 0 || front > rear)
return true;
else
return false;
}
Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively
difficult to implement than that of stacks.
The following steps should be taken to enqueue (insert) data into a queue −
• Step 1 − Check if the queue is full.
• Step 2 − If the queue is full, produce overflow error and exit.
• Step 3 − If the queue is not full, increment rear pointer to point the next empty space.
• Step 4 − Add data element to the queue location, where the rear is pointing.
• Step 5 − return success.
Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseen situations.
Algorithm for enqueue operation
procedure enqueue(data)
if queue is full
return overflow
endif
rear ← rear + 1
queue[rear] ← data
return true
end procedure
2. If the queue is full, then print overflow error and exit the program.
3. If the queue is not full, then increment the tail and add the element.
rear = rear + 1;
queue[rear] = data;
return 1;
end procedure
Dequeue 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 dequeue operation −
• Step 1 − Check if the queue is empty.
• Step 2 − If the queue is empty, produce underflow error and exit.
• Step 3 − If the queue is not empty, access the data where front is pointing.
• Step 4 − Increment front pointer to point to the next available data element.
• Step 5 − Return success.
if queue is empty
return underflow
end if
data = queue[front]
front ← front + 1
return true
end procedure
2. If the queue is empty, then print underflow error and exit the program.
3. If the queue is not empty, then print the element at the head and increment the head.
Example
int dequeue() {
if(isempty())
return 0;
return data;
}
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
void insert();
void delete();
void display();
int GEORGE[MAX];
int rear = - 1;
int front = - 1;
int main()
{
int choice;
while (1)
{
printf("\n 1.Insert element to queue GEORGE");
printf("\n 2.Delete element from queue GEORGE");
printf("\n 3.Display all elements of queue GEORGE");
printf("\n 4.Quit Queue GEORGE");
printf("\n Enter your choice = : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
void insert()
{
int item;
if(rear == MAX - 1)
printf("Queue Overflow");
else
{
if(front== - 1)
front = 0;
printf("Insert the element in queue : ");
scanf("%d", &item);
rear = rear + 1;
GEORGE[rear] = item;
}
}
void delete()
{
if(front == - 1 || front > rear)
{
printf("Queue Underflow");
return;
}
else
{
printf("Element deleted from queue is : %d \t", GEORGE[front]);
front = front + 1;
}
}
void display()
{
int i;
if(front == - 1)
printf("Queue is empty");
else
{
printf("Queue is : \t");
for(i = front; i <= rear; i++)
printf("%d ", GEORGE[i]);
}
}
Advantages of Queue:
• A large amount of data can be managed efficiently with ease.
• Operations such as insertion and deletion can be performed with ease as it follows the
first in first out rule.
• Queues are useful when a particular service is used by multiple consumers.
• Queues are fast in speed for data inter-process communication.
• Queues can be used in the implementation of other data structures.
Disadvantages of Queue:
• The operations such as insertion and deletion of elements from the middle are time
consuming.
• Limited Space.
• In a classical queue, a new element can only be inserted when the existing elements are
deleted from the queue.
• Searching an element takes O(N) time.
• Maximum size of a queue must be defined prior.