Topic 5 & 6
Topic 5 & 6
Array is a container which can hold a fix number of items and these items should be of the same type.
Most of the data structures make use of arrays to implement their algorithms. Following are the
important terms to understand the concept of Array.
• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical index, which is used
to identify the element.
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
• In C, when an array is initialized with size, then it assigns defaults values to its elements
in following order.
Data Type Default Value
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] = GEORGE[J+1]
5. Set J = J+1
6. Set N = N-1
7. Stop
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;
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
Search Operation
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); }
OUTPUT:
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]);
}
}
OUTPUT:
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.
FIFO Principle of Queue:
• A Queue is like a line waiting to purchase tickets, where the first person in line is the
first person served. (i.e. First come first serve).
• Position of the entry in a queue ready to be served, that is, the first entry that will be
removed from the queue, is called the front of the queue(sometimes, head of the
queue), similarly, the position of the last entry in the queue, that is, the one most recently
added, is called the rear (or the tail) of the queue.
Characteristics of Queue:
• Queue can handle multiple data.
• We can access both ends.
• They are fast and flexible.
Queue Representation:
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 linkedlist,
the algorithm will differ. Algorithm of isfull() function −
Algorithm
begin procedure isfull
if rear equals to
MAXSIZE return true
else
return false
endif
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;
}
Enqueue Operation
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.
Algorithm for dequeue operation
procedure dequeue
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.
Implementation of dequeue() in C programming language − Example
int dequeue() {
if(isempty())
return 0;
return data; }
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]);
}
}
OUTPUT
Queue is used when things don’t have to be processed immediately, but have to be processed in
First In First Out order.
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.