State Algorithm for Inserting an Element in an Array. - Computer Science 1

Advertisements
Advertisements

State algorithm for inserting an element in an Array.

Advertisements

Solution

(1) Inserting element to an array is the process of adding an element to the existing elements of array.
(2) The element can be easily inserted at the end of an array. But for insertion in the middle of an array it is required to move the elements one byte forward.
(3) The following algorithm inserts a data element in an array:-

Algorithm for inserting element into a linear array
INSERT [LA, N, K, ITEM]
LA = Linear array
N = Total no. of elements in the array
K = Any positive integer, K<=N
This algorithm inserts an element ITEM into the Kth position in LA.

1.[Initialize Counter]                                   Set J: = N
2.Repeat steps 3 and 4 while J ≥ K
3.[Move Jth element downward] Set LA [J+1]: = LA [J]
4.[Decrease Counter]                                 Set J: = J-1
[End of step 2 loop]
5.[Insert element] Set LA [K]: = ITEM
6.[Reset N]                                               Set N: = N+1
7.Exit

Number 9 is inserted 4 is deleted
3 3 3
4 4 9
5 9 5
6 5 6
8 6 8
  8  
Original Array Addition of element in
middle array
Deletion of element from array
Concept: Basic Data Structures (Stack, Queue, Dequeue)
  Is there an error in this question or solution?
2015-2016 (March)

APPEARS IN

RELATED QUESTIONS

Write an algorithm for Binary Search Method. Explain algorithm with suitable example.


What is Linked List ? How they can be represented in Memory?


Explain with flowchart the following control structure : Iteration Logic


Define the following terms with reference to Tree : Leaf


Data items are divided into sub-item is called as ________.

(i) Group Item

(ii) Elementary Item

(iii) Nodes

(iv) Arrays


With suitable example show the relationship between Total Number of Nodes and Depth of Binary Tree.


 ___________ is very useful in situation when data is to be stored and retrieved in reverse order.


 Explain Binary Scarch algorithm with a suitable example.


Write two features of each of doto structures:
1)  Record
2) Linear array 
3) Linked list 


 Explain pointer Array with example. 


The keyword used by a class to acquire the properties of an interface is:


State the principle by which the stack data structure works.


A Queue is a linear data structure in which the operations are performed based on FIFO (First In First Out).

Define a class Queue with the following details:

Class name Queue
Data member/instance variable:
dat[ ] array to hold the integer elements
cap stores the maximum capacity of the queue
front to point the index of the front
rear to point the index of the rear
Member functions/methods:
Queue(int max) constructor to initialize the data member cap = max, front = rear = 0 and create the integer array
void add_dat(int v) to add integers from the rear index if possible else display the message(“Queue full”)
int pop_dat( ) to remove and return elements from front, if any, else returns -999
void display() to display elements of the queue

Specify the class Queue giving the details of void add_dat(int) and int pop_dat( ). Assume that the other functions have been defined.

The main( ) function and algorithm need NOT be written.


A double ended queue is a linear data structure which enables the user to add and remove integers from either ends i.e., from front or rear.

The details for the class deQueue are given below:

Class name deQueue

Data members/instance variables:

Qrr[] array to hold integer elements
lim maximum capacity of the dequeue
front to point the index of the front end
rear  to point the index of the rear end

Methods/Member functions:

deQueue(int 1) constructor to initialise lim = 1, front = 0 and rear =0
void addFront(int v) to add integers in the dequeue at the front end if possible, otherwise display the message “OVERFLOW FROM FRONT”
void addRear(int v) to add integers in the dequeue at the rear end if possible, otherwise, display the message “OVERFLOW FROM REAR”
int popFront() removes and returns the integers from the front end of the dequeue if any, else returns — 999
int popRear ( )  removes and returns the integers from the rear end of the dequeue if any, else returns — 999
void show( ) displays the elements of the dequeue

Specify the class deQueue giving details of the function void addFront(int) and int popFront(). Assume that the other functions have been defined. The main() function and algorithm need NOT be written.


Differentiate between a stack and a queue.


Share
Notifications



      Forgot password?
Use app×