0% found this document useful (0 votes)
30 views

4. data-structure

Uploaded by

Irine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

4. data-structure

Uploaded by

Irine
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

ONE MARK QUESTIONS:

1. What are data structures?


Data structure is a specialized format for organizing and storing data in a
computer. Or
Data structure provides the means to manage large amount of data
efficiently.

2. What is primitive data structures?


Data structure that are directly operated upon by machine level instructions
are known as primitive data structures.

3. Give an example for primitive data structures.


int

4. What are non-primitive data structures?


The data structures which are derived from primitive data structures are
called non primitive data structures.

5. Give an example for non-primitive data structures.


arrays

6. What is linear data structure?


Linear data is a kind of data structure that has homogenous
elements. Data elements of linear structures will have linear
relationship between data elements.

7. Give an example for linear data structure?


arrays

8. Define traversing an array.


The process of accessing each data item exactly once to perform
some operation is called traversing.

9. Define searching.
The process of finding the location of a data item in the
given collection of data items is called as searching.

10. Define sorting.


The process of arrangement of data items in ascending or
descending order is called sorting.

11. Define insertion/inserting


The process of adding a new data item into the given collection of
data items is called insertion.

12. Define deletion/deleting.


The process of removing an existing data item from the given
collection of data items is called deletion.

13. What is an array?


An array is a collection of homogeneous elements with unique name and each the
elements occupies adjacent memory locations.

14. What is stack?


Stack is an ordered collection of items where the addition of new items and
removal of existing items always take place at the same end called top of the
stack.

15. What is push in stack?


Push is a stack operation which adds a new item to the top of stack.
16. What is pop in stack?
Pop is a stack operation which removes an item from the top of stack.

17. Which order stack follows?


Stack follows LIFO (Last In First Out) order.

18. Give an example for static memory representation.


Arrays

19. Give an example for dynamic memory representation.


Linked list

20. What is a queue?


A queue is an ordered collection of items where an item is inserted at one
end called the “rear,” and an existing item is removed at the other end,
called the “front.”
(OR)
A queue is an ordered collection of items where the addition of new
items and the removal of existing items always take place at
different ends called rear and front of the queue.

21. Which order does the queue data structure follow?


Queue follows First-In First Out (FIFO) order.

22. What is enqueue?


Enqueue is an operation which adds a new item to the rear of the
queue

23. What is dequeue?


Dequeue is an operation which deletes an item from front of the
queue.

24.Name the data structure whose relation between data


element is by means of links.
Linked Lists

25. What is linked list?


A linked list is a linear collection of data elements called nodes and
the linear order is given by means of pointers.

26. How do we establish linearity in linked list?


The linearity in linked list is established by means of pointers

27. Name the type of memory allocation use by the linked list.
Dynamic memory allocation.

28. What do you mean by traversal in data structure?


The process of accessing each data items exactly once to perform some
operations

29. What is non-linear data structure?


A non-linear data structure is a data structure in which a data item is
connected to several other data items.

30.Give an example for non-linear data structure.


Trees and graphs

31. What is a node in a tree?


A node is a structure which may contain a value, a condition, or
represent part of the structure.
32. What is parent node?
A node in a tree that has a child node

33. What is a child node?


A node which is descendant of any node is called as child node.

34. What is the height of a tree?


Height of a tree is height of the root node

36. What is the depth of a tree?


The length of the path from a node to its root is called depth of a
tree.

37. What is a root node?


The top most node in a tree is called root node.

38. What is internal node?


An internal node or inner node is any node of a tree that has child
nodes.

39. What is binary tree?


A tree in which each node has at most two descendants.

40. What is a complete tree?


Tree in which each leaf is at the same distance from the root i.e., all
the nodes have maximum two subtrees.

41. What is a graph?


A graph is a collection of nodes called vertices, and the connections
between them, called edges.

42. Name the data structure that is called LIFO list.


Stack

43. Name the data structure that is called FIFO list.


Queue

44. Which operator is used to allocate the memory dynamically?


new

45. What do you mean by depth of tree?


The depth of the node in tree is the length of the path to its root.

TWO MARKS QUESTIONS

1.What are the two types of data structures?


Primitive and non-primitive are the two types of data structure.

2. Mention the different operations performed on primitive data structure.


The various operations that can be performed on primitive data structure
are:
Create: used to create a new data structure. This operation reserves
memory space for the program elements. Ex: float x;
Destroy: is used to destroy or remove the data structure from the memory
space.
Select: operation is used by programmers to access the data within data
structure. This operation updates or alters the data.
Update operation: is used to change data of the data structure.
Example: a=a+10; (Answer any two)
3. What is linear and non-linear data structure?
Linear Data Structure: is a kind of data structure that has
homogenous elements. Each element is referred to by an index or a
link
Example: arrays, Stack, Queue and Linked List.
Non- linear data structure: A non-linear data structure is a data structure
in which a data item is connected to several other data items.
Example: Trees and graphs

4. What is an array? Mention the different types of arrays.


An array is a collection of homogeneous elements with unique name and each the
elements occupies adjacent memory locations.
Different types of Arrays.
There are three types of arrays:
 One dimensional Array
 Two-dimensional Array
 Multi-dimensional array

5. Mention two types of searching techniques.


Linear search and Binary search

6. Write any two applications and arrays.


1.Arrays are used to implement mathematical vectors and matrices.
2. Arrays are used to implement queues, stacks and strings

7. What are PUSH and POP operations on stacks?


Push operation adds a new item to the top of the stack. It needs the item to
be added as parameter and returns nothing.
Pop operations is removes the top item from the stack. It needs no
parameter and returns the item.

8. Write the memory representation of queues using arrays.


Two pointer variables called FRONT and REAR are maintained.
The FRONT contains the location of the element to be removed and
the REAR contains location of the last element inserted.
The condition FRONT = -1 indicates that the queue is empty and
the condition REAR = N-1 indicates that the queue is full.

FRONT REAR

A B C D E
0 1 2 3 4 5 6 7 8
FRONT = 1 REAR = 5

9. What is the purpose of new and delete operators?


new: Dynamic memory is allocated using operator new.
new is followed by a data type specifier. Ex: p= new int;
delete: Dynamic memory is deallocated using operator delete.
Ex: delete p;
(releases the memory of a single element allocated using delete)

10. Mention any two operations performed on stacks.


Operations of Stack:
Stack (): creates a new stack that is empty. It needs no parameters and
returns an empty stack.
Push(item): adds a new item to the top of the stack. It takes the item to be
inserted as parameter and returns nothing.
11. What are enqueue and dequeue in queues?
enqueue operation adds a new item to the rear of the queue. It takes
the item to be inserted as parameter and returns nothing.
dequeue operation removes the front item form the queue. It needs
no parameters and returns the item.

12. What are the different types of linked lists?


There are three types of linked lists.
1. Single linked list (SLL)
2. Doubly linked list (DLL)
3. Circular linked list (CLL)

13. How do you find the length of the array?


Length= (UB – LB) + 1
UB means Upper bound, LB means Lower bound
Egg: UB=5, LB=0, Length =5-0+1 =Length =6

THREE MARKS QUESTIONS:

1. Explain classification of Data Structure

2. Give the memory representation of one-dimensional array.


 Elements of an array are stored in consecutive memory locations.
 Formula for calculating address of any element A[i] of an array A is given by
LOC(A[P]) =BASE(A)+W(P-LB)
 Where W is the number of words per memory cell. P is position and LB is the
lower bound of the array
 Now the address of element S[3] can be calculated as follows
where W=1, LB=0, P=3, BASE[A]=1000
Address (S [3]) =Base(S)+W(P-LB)
= 1000+1(3-0)
= 1000+1(3)
=1000 + 3
= 1003

3. Write an algorithm for traversing an array.


[Let 'A' is an array, LB and UB the lower bound and upper bound, LOC represents
array subscript and PROCESS represents the operation]
STEP 1: FOR LOC = LB TO UB
PROCESS A[LOC]
END FOR
STEP 2: EXIT
4. Write the memory representation arrays in row-major order.
Row-major order:
 In row-major representation all the elements of a two-dimensional array
are stored row-wise in memory.

 Example: Let A be an array of order 2x3 with base address 3000,


and word size=4 then its row-major representation is as shown
below.

 The memory address of any element A[I][J] can be obtained by the


formula
LOC (A [I, J]) = Base(A) + W[(I-LB) *n+(J-LB)]
Where LB lower bound, W=Word size per array cell

5. Write the memory representation arrays in column-major order.


 In column-major representation all the elements of a two-dimensional
array are stored column-wise in memory.
 Example: Let A be an array of order 2x3 with base address 3000,
and word size=4 then its column-major representation is as
shown below.

Base(A)=3000, W=4
 The formula for finding location of an element A[I][J] in array A with lower
bound LB, base address Base(A) and word size W per array cell is given by

LOC(A[I][J]) = Base (A)+W[(I-LB) +m*(J-LB)]


6. Consider the array A of order 25 x 4 with base value 2000 and one word per
memory location. Find the memory address of A [12][3] in row-major order.
Solution:
m=25, n=4, BASE(A)=2000, W=1, I=12, J=3, LB=0(assumption)

LOC(A[I][J]) = Base(A) + W(I-LB) *n+(J-LB)


A [12][3] = 2000+1[(12-0) *4+(3-0)]
=2000+1[12*4+3]
=2000+ [48+3]
=2051

7. Consider the array A of order 25x4 with base value 2000 and one word per
memory location. Find the address of A [12][3] in column-major order.
Solution:
Base(A)=2000, W=1, M=25, N=4, I=12, J=3, LB=0(assumption)
LOC(A[I][J]) = Base (A)+ W[(I-LB) +m*(J-LB)]
LOC (A [12][3] = 2000+1[12-0) +25*(3-0)
= 2000+1[12+25*(3)]
= 2000+1[87]
= 2087

8.What are the disadvantages of arrays?


1.We must know in advance that how many elements are to be
stored in an array.
2.Array is static structure. It means that array is of fixed size.
3.The memory which is allocated to array cannot be increased or
reduced.
4.The elements of array are stored in consecutive memory
locations. So insertions and deletions are very difficult and time
consuming.

9. Explain the memory representation stacks using array.


 Stack can be represented using a one-dimensional array.
 The items into the stack are stored in a sequential order from
the first location of the array.
 A pointer TOP contains the location of the top element of the stack.
 A variable MAX-1 contains the maximum number of elements
that can be stored in the stack.
 If TOP = MAX-1 indicates that the stack is full.
 if TOP = -1 indicates that the stack empty.
 It is useful for fixed sized stacks.
 Dynamic creation of a stack is not possible using arrays.
0 1 2 Subscript
1 2 3

Top Stack size increases in this direction

10. Write an algorithm for push operation.


Stack is the array with N elements. TOP is the variable to the top of the
element of the array. ITEM is a variable which to be inserted.
STEP1: IF TOP=N-1 THEN
PRINT ”STACK IS FULL”
EXIT
[END OF IF]
STEP2: TOP=TOP+1
STEP3: STACK[TOP]=ITEM
STEP4: RETURN
11. Write an algorithm for POP operation.
Stack is the array with N elements. TOP is the variable to the top of the
element of the array. ITEM is a variable which to be inserted.
STEP1: IF TOP = -1 THEN
PRINT "STACK IS EMPTY"
EXIT
[END OF IF]
STEP2: ITEM = STACK[TOP]
STEP3: TOP = TOP- 1
STEP4: RETURN

12. Write any three applications of stacks.


 reverse a word.
 "undo" mechanism in text editors.
 Backtracking
 Recursion
 Quick sort.
 Run time memory management.

13. Write any three applications of queues.


 Simulation
 Various features of operating system
 Multi-programming platform systems
 Printer Server Routine

14. Explain the memory representation of queues using array.


 Queue is represented in memory linear array. Let QUEUE
linear queue.
 Two pointer variables called FRONT and REAR are
maintained.
 The pointer variable FRONT contains the location of the
element to be removed and the pointer variable REAR contains
location of the last element inserted.
 The condition FRONT = -1 indicates that the queue is empty
and the condition REAR = N-1 indicates that the queue is full.

15. Explain types of linked list.


a. Single linked list (SLL)
b. Doubly linked list (DLL)
c. Circular linked list (CLL)
a. Single linked list:
 A singly linked list contains two field in each node - the data
field and link field.
 The data field contains the data of that node while the link
field contains address of the next node is called as singly
linked list.
Start

50 40 70 20 Null

Disadvantage:
 In a singly linked list, a pointer is available to access all the
succeeding nodes, but not preceding nodes. In the singly
linked lists, the link field of the last node contains NULL

b. Circular linked lists:


Start

1 2 3 4

 The link field of the last node contains the address of the first
node is called as circular linked list.
 In circular linked list is possible to reach any node from any
other.

c. Doubly linked lists:


 Doubly linked list is a linked list in which each node is points
both to the next node and also to the previous node.

INFO

 In doubly linked list each node contains three parts - FORW,


BACK and INFO
 Where FORW: It is a pointer field that contains the address of the
next node.
 BACK: It is a pointer field containing the address of the previous
node.
 INFO: It contains the actual data.

16. Define the following: a. Trees b. Graph c. Root node.


a. Trees: A tree is a data structure consisting of nodes organized as a
hierarchy.
b. Graphs: A graph is a collection of nodes called vertices, and the
connections between them, called edges.
c. Root node: The top most node in a tree is called the root node.

FIVE MARKS QUESTIONS:

1. What is primitive data structure? Explain the different operations


performed on primitive data structure.
Data structure that are directly operated upon by machine level instructions
are known as primitive data structures.
Example: int, float, bool, char, pointers and reference.
The various operations that can be performed on primitive data
structure are:
Create: used to create a new data structure. This operation reserves
memory space for the program elements. Ex: float x;
Destroy: is used to destroy or remove the data structure from the memory
space. When the program execution ends, the data structure is
automatically destroyed destructor member function de-allocates memory.
Select: operation is used by programmers to access the data within data
structure. This operation updates or alters the data.
Update operation: is used to change data of the data structure.
Example: a=a+10;

2. What is non-primitive data structure? Explain the different operations


performed on non-primitive data structure.
Non-primitive data structures are derived from primitive data structure.
Different operations performed on non-primitive data structure.
a. Traversal: The process of accessing each data item exactly
once to perform some operation is called traversing.
b. Insertion: The process of adding a new data item into the
given collection of data items is called insertion.
c. Deletion: The process of removing an existing data item from
the given collection of data items is called deletion.
d. Searching: The process of finding the location of a data item
in the given collection of data items is called as searching.
e. Sorting: The process of arrangement of data items in
ascending or descending order is called sorting.
f. Merging: The process of combining the data items of two
structures to form a single structure is called merging.

3.Explain the different basic operations on one-dimensional


arrays
The operations performed on one dimensional array are:
a. Traversing: Accessing each element of the array exactly once
to do some operation.
b. Searching: Finding the location of an element in the array.
c. Sorting: Arranging the elements of the array in some order either in
ascending or descending order.
d. Insertion: Inserting an element into an array.
e. Deletion: Removing an element from an array.
f. Merging: Combining one or more arrays to form a single array.

4. Write an algorithm for searching an element using linear search method.


Algorithm: A is the name of the array with N elements. ELE is the element to be
searched. This algorithm finds the location LOC where the search ELE element is
stored.
STEP 1: LOC = -1
STEP 2: FOR I = 0 TO N-1 DO
IF (A[I] = ELE)
LOC = I
GOTO STEP 3
[END OF IF]
[END OF FOR]
STEP 3: IF (LOC >= 0)
PRINT LOC
ELSE
PRINT “SEARCH IS UNSUCCESSFUL”
[END OF IF]
STEP 4: EXIT
5. Write an algorithm for searching an element using binary search method.

Algorithm: A is the sorted array with LB as lower bound and UB as the


upper bound respectively. Let B, E, M denote beginning, end and middle
locations of the segments of A.

STEP 1: B=0, E=N-1, LOC= -1


STEP 2: WHILE (B<=E)
M=INT(B+E)/2
IF (ELE=A[M])
LOC =M
GOTO STEP 3
ELSE IF (ELE<A[M])
E= M-1
ELSE
B=M+1
[END OF IF]
[END OF WHILE]
STEP 3: IF (LOC >= 0)
OUTPUT LOC
ELSE
OUTPUT “SEARCH IS UNSUCCESSFUL”
[ END OF IF]
STEP 6: EXIT

6. Write an algorithm Sort N number of elements using insertion sort


method.

Algorithm: Let A be an array with N unsorted elements.


The following algorithm sorts the elements in order.
STEP 1: FOR I = 1 TO N-1
STEP 2: J=I
WHILE (J >= 1)
IF(A[J] < A[J-1])
TEMP = A[J]
A[J] = A[J-1]
A[J-1] = TEMP
[END OF IF]
J=J-1
[END OF WHILE]
[END OF FOR]
STEP 3: EXIT
7.Write an algorithm to insert an element into array.

Algorithm: A is the array with N elements. ITEM is the element to be inserted in the
position P.

STEP 1: FOR I = N-1 DOWNTO P


A[I+1] = A[I]
[END FOR]
STEP 2: A[P] = ITEM
STEP 3: N = N+1
STEP 4: EXIT

8.Write an algorithm for deleting an element from the array.

Algorithm: A is the array with N elements. ITEM is the element to be deleted


in the position P and it is stored into the variable Item

STEP 1: ITEM = A[P]


STEP 2: FOR I = P TO N-1
A[I] = A[I+1]
[END OF FOR]
STEP 3: N = N-1
STEP 4: EXIT

9.Explain the different operations performed on stacks.


Operations of Stack:
a. Stack (): creates a new stack that is empty. It needs no parameters and
returns an empty stack.
b. Push(item): adds a new item to the top of the stack. It needs the item to
be added as parameter and returns nothing.
c. Pop (): removes the top item from the stack. It needs no parameters and
returns the item. The stack is modified.
d. Peek (): returns the top item, from the stack but does not remove it. It
needs no parameter. The stack is not modified
e. isEmpty (): tests whether the stack is empty. It needs no parameter.
Returns a Boolean value.
f. Size (): returns the number of items on the stack. It needs no parameters
and returns an integer.

10.Write the applications of stacks.


 to reverse a word.
 Undo mechanism in text editors
 Backtracking
 Support for recursion
 Conversion of decimal number into binary.
 Quick sort.
 Run time memory management.
11.Write an algorithm to insert an item into the queue.

Algorithm: Let QUEUE be the linear array consisting of N elements. FRONT is


the pointer that contains the location of the element to be deleted and REAR
contains the location of the inserted element. ITEM is the element to be
inserted.

STEP 1: IF REAR =N-1 THEN


PRINT “OVERFLOW”
EXIT
STEP 2: IF FRONT = -1 THEN
FRONT = 0
REAR = 0
ELSE
REAR=REAR+1
STEP 3: QUEUE[REAR] = ITEM
STEP 4: RETURN

12. Write an algorithm to delete an item from the queue.

Algorithm: Let QUEUE is the linear array consisting of N elements.


FRONT is the pointer that contains the location of the element to be
deleted and REAR contains the location of the inserted element. This
algorithm deletes the element at FRONT position.
STEP 1: IF FRONT = -1 THEN
PRINT "UNDERFLOW"
EXIT
STEP 2: ITEM = QUEUE[FRONT]
STEP 3: IF FRONT = REAR THEN
FRONT = -1
REAR = -1
ELSE
FRONT = FRONT + 1
STEP4: RETURN

13. Mention any two operations performed on queues.


a. queue () creates a new queue that is empty. It needs no parameters
and returns an empty queue.
b. enqueue(item) adds a new item to the rear of the queue. It needs
the item to be added as parameter and returns nothing.
c. dequeue () removes the front item from the queue. It needs no
parameters and returns the item. The queue is modified. This
operation is same as pop.
d. isEmpty () tests to see whether the queue is empty. It needs no
parameters and returns a Boolean value.
e. size () returns the number of items in the queue. It needs no
parameters and returns an integer.
14. Write the applications of queues.
 Simulation
 Various features of operating system
 Multi-programming platform systems
 Different type of scheduling algorithm
 Printer Server Routine

15. What are the operations performed on the linked list?


The operations performed on linked lists are:
1. Creating a linked list
2. Traversing a linked list
3. Inserting an item into a linked list
4. Deleting an item from the linked list
5. Searching an item in the linked list
6. Merging two or more linked lists

16. Define the following: a. Root Node b. Leaf Node c. Height d. Depth e.
Internal node.
a) Root node: The top most node in a tree
b) Leaf node: Nodes that do not have children.
c) Height: Number of nodes which must be traversed from the root
to reach a leaf of a tree.
d) Depth: It is the number of ancestors of a node excluding
itself i.e., the length of the path to its root.
e) Internal node: An internal node or inner node is any node of a tree that
has child nodes.

You might also like