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

Chapter 4 Data Structures

Chapter 4 discusses data structures, defining key concepts such as primitive and non-primitive data structures, linear and non-linear data structures, and various operations like traversing, searching, sorting, inserting, and deleting. It also covers specific data structures including arrays, stacks, queues, linked lists, trees, and graphs, along with their properties and memory representations. Additionally, the chapter includes algorithms for operations on stacks and queues, as well as advantages and disadvantages of arrays.

Uploaded by

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

Chapter 4 Data Structures

Chapter 4 discusses data structures, defining key concepts such as primitive and non-primitive data structures, linear and non-linear data structures, and various operations like traversing, searching, sorting, inserting, and deleting. It also covers specific data structures including arrays, stacks, queues, linked lists, trees, and graphs, along with their properties and memory representations. Additionally, the chapter includes algorithms for operations on stacks and queues, as well as advantages and disadvantages of arrays.

Uploaded by

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

CHAPTER 4 - DATA STRUCTURES

CHAPTER 4 DATA STRUCTURES

One mark questions:


1. What is data structure? (U)
ANS : Data Structure is the way of collecting and organizing the data in such a way
that we can perform operation on these data in an effective way.
2. What is primitive data structure? (U)
ANS : Data structures that are directly operated upon the machine-level instructions
are known as primitive data structures.
3. Give an example for primitive data structure. (U)
ANS : integers, float, character data, pointers
4. What is non-primitive data structure? (U)
ANS : The Data structures that are derived from the primitive data structures are
called Non-primitive data structure.
5. Give an example for non-primitive data structure. (U)
ANS : Stack, Queue ,Linked Lists, trees , graph
6. What is linear data structure? (U)
ANS : Linear Data structures are kind of data structure that has homogeneous
elements.
7. Give an example for linear data structure. (U)
ANS : Stack, Queue and Linked Lists.
8. Define traversing an array. (U)
ANS : The processing of accessing each element exactly once to perform some
operation is called traversing.
9. Define searching. (U)
ANS : The process of finding the location of a data element in the given collection of
data elements is called as searching.

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 1


CHAPTER 4 - DATA STRUCTURES

10. Define sorting. (U)


ANS : The process of arrangement of data elements in ascending or descending
order is called sorting.
11. Define inserting. (U)
ANS : The process of adding a new element into the given collection of data
elements is called insertion.
12. Define deleting. (U)
ANS : The process of removing an existing data element from the given collection of
data elements is called deletion.
13. What is an array? (U)
ANS : An array is an ordered collection of elements of same data type that share
common name.
14. What is a stack? (U)
ANS : A stack is an ordered collection of items in which an element may be inserted
or deleted only at same end.
15. What is push in stack? (U)
ANS : The process of adding one element or item to the stack.
16. What is pop in stack? (U)
ANS : The process of deleting one element or item from the stack.
17. Which order stack follows? (U)
ANS : LIFO (Last In First Out).
18. Give an example for static memory representation. (U)
ANS : Array
19. Give an example for dynamic memory representation. (U)
ANS : Linked List
20. What is a queue? (U)
ANS : 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”.

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 2


CHAPTER 4 - DATA STRUCTURES

21. Which order does the queue data structure follow? (U)
ANS : FIFO ( First-In First-Out).
22. What is enqueue? (U)
ANS : Enqueue is an operation of Queue which adds a new item to the rear of the
queue.
23. What is dequeue? (U)
ANS : Dequeue is an operation of Queue which removes the front item from the
queue.
24. What is linked list? (U)
ANS : A linked list is a linear collection of data elements called nodes.
25. How do we establish linearity in linked list? (U)
ANS : By storing the next node address to the link in the previous node
26. Name the type of memory allocation use by the linked list. (U)
ANS : Dynamic memory allocation
27. What is non-linear data structure? (U)
ANS : A Non-Linear Data structures is a data structure in which data item is
connected to several other data items.
28. Give an example for non-linear data structure. (U)
ANS : trees and graphs
29. What is a node? (U)
A node is a fundamental part of tree. Each element of a tree is called a node of the
tree.
30. What is parent node? (U)
ANS : parent node is an immediate predecessor a node.
31. What is a child node? (U)
ANS : child node successor of parent node is called the child node.

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 3


CHAPTER 4 - DATA STRUCTURES

32. What is the height of a tree? (U)


ANS : The height or depth of a tree is defined to be the maximum number of nodes
in a branch of tree.
33. What is the depth of a tree? (U)
ANS : The height or depth of a tree is defined to be the maximum number of nodes
in a branch of tree.
34. What is a root node? (U)
ANS : The topmost node of the tree is called root node.
35. What is internal node? (U)
ANS : A node has one or more children is called an internal node.
36. What is binary tree? (U)
ANS : A binary tree is an ordered tree in which each internal node can have
maximum of two child nodes connected to it.
37. What is a complete tree? (U)
ANS : A Complete binary tree is a binary tree in which each leaf is at the same
distance from the root ( all the nodes have maximum two subtrees).
38. What is a graph? (U)
ANS : A graph is a collection of nodes called vertices and the connection between
them called edges.
39. Name the data structure that is called LIFO list. (A)
ANS : Stack
40. Name the data structure that is called FIFO list. (A)
ANS : Queue
41. Which operator is used to allocate the memory dynamically? (A)
ANS : new
Two marks questions:
1. What are the two types of data structures? (U)
ANS : a. Primitive

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 4


CHAPTER 4 - DATA STRUCTURES

b. Non Primitive
i. Linear
ii. Non Linear
2. Mention the different operations performed on primitive data structure. (U)
ANS : Refer in other section
3. What is linear and non-linear data structure? (U)
ANS : i. Linear Data structures:
Linear Data structures are kind of data structure that has homogeneous elements.
ii. Non-Linear Data structures:
A Non-Linear Data structures is a data structure in which data item is connected to
several other data items.
4. What is an array? Mention the different types of arrays. (U)
ANS : An array is an ordered collection of elements of same data type that share
common name.

i. One-dimensional Array
ii. Two-dimensional Array
iii. Multi-dimensional Array
5. Mention two types of searching techniques. (U)
ANS : Linear Search and Binary Search
6. Write any two applications of arrays. (A)
ANS : 1. Used to perform matrix operation
2. Used to store the values of same type.
7. What are PUSH and POP operations on stacks? (U)
ANS : push(item): It adds a new item to the top of the stack.
pop( ): It removes the top item from the stack.
8. Write the memory representation of queues using arrays. (A)
9. What is the purpose of new and delete operators? (U)

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 5


CHAPTER 4 - DATA STRUCTURES

ANS : new-Allocating memory dynamically


delete – Deleting memory created by new operator
10. Mention the various operations performed on stacks. (A)
11. Mention the various operations performed on queues. (A)
12. What are enqueue and dequeue in queues? (A)
ANS : enqueue(item): It adds a new item to the rear of the queue.
dequeue( ): It removes the front item from the queue.
13. What are the different types of linked lists? (A)
Three marks questions:
1. Give the memory representation of one-dimensional array. (A)
ANS : Datatype Array_Name [Size];

o Datatype : Type of value it can store (Example: int, char, float)


o Array_Name: To identify the array.
o Size : The maximum number of elements that the array can hold.
Example: int a[5];
Address Content Location
1000 A A[0] Lower Bound(LB)
1001 B A[1]
1002 C A[2]
1003 D A[3]
1004 E A[4] Upper Bound(UB)

Calculating the length of the array:


o A[n], the number of n is called the size or length of the array.
o The length of the array can be calculated by:
L = UB – LB + 1
o Here, UB is the largest Index and LB is the smallest index
Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 6
CHAPTER 4 - DATA STRUCTURES

o Example: If an array A has values 10, 20, 30, 40, 50, stored in location 0,1, 2, 3, 4
the UB= 4 and LB=0
o Size of the array L = 4 – 0 + 1 = 5

2. Write an algorithm for traversing an array. (A)


ANS : ALGORITHM: Traversal (A, LB, UB) A is an array with Lower Bound LB and
Upper Bound UB.
Step 1: for LOC = LB to UB do
Step 2: PROCESS A [LOC]
[End of for loop]
Step 3: Exit

3. Write the memory representation arrays in row-major order. (A)


ANS : Row-Major Method: All the first-row elements are stored in sequential
memory locations and then all the second-row elements are stored and so on.
1001 1 A[0][0]
1002 7 A[0][1]
1003 4 A[0][2]
1004 2 A[1][0]
1005 8 A[1][1]
1006 5 A[1][2]
1007 4 A[2][0]
1008 5 A[2][1]
1009 6 A[2][2]

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 7


CHAPTER 4 - DATA STRUCTURES

4. Write the memory representation arrays in column-major order. (A)


ANS : Column-Major Method: All the first column elements are stored in
sequential memory locations and then all the second-column elements are stored
and so on.
1001 1 A[0][0]
1002 2 A[1][0]
1003 3 A[2][0]
1004 7 A[0][1]
1005 8 A[1][1]
1006 9 A[2][1]
1007 3 A[0][2]
1008 9 A[1][2]
1009 6 A[2][2]

5. 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. (A)
ANS : LOC ( A[ i ][ j] ) = base (A) +W [ n (i – LB) + (j – LB)]
Given Base (A)=2000 , m=25 , n=4 , LB=0 , W=1 , i=12 , j=3
LOC ( A[ 12 ][ 3] ) = 2000 +1 [4 (12-0) + (3-0)
=2000 + 4 (12) + 3
=2000 + 48 +3
=2051
6. 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. (A)
ANS : LOC ( A[ i ][ j] ) = base (A) +W [ (i – LB) + m (j – LB)]
Given Base (A)=2000 , m=25 , n=4 , LB=0 , W=1 , i=12 , j=3
LOC ( A[ 12 ][ 3] ) = 2000 +1 [ (12-0) + 25 (3-0)
` =2000 + 1 (12) + 75
Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 8
CHAPTER 4 - DATA STRUCTURES

=2000 + 87
=2087
7. What are the advantages of arrays? (A)
ANS : Advantages of Array:
It is used to represent multiple data items of same type by using single name.
It can be used to implement other data structures like linked lists, stacks, queues,
tree, graphs etc.
Two-dimensional arrays are used to represent matrices.
Many databases include one-dimensional arrays whose elements are records.

8. What are the disadvantages of arrays? (U)


ANS : Disadvantages of Array:
are to be stored in array.

allocated to array cannot be increased or decreased.

space will be wasted.

and deletion are very difficult and time consuming.

9. Explain the memory representation stacks using array. (U)


ANS : Representation of Stacks using Array:
-dimensional array.

the memory block.

ns the maximum number of element that can be stored


in the stack.

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 9


CHAPTER 4 - DATA STRUCTURES

stack overflow condition. Similarly, you cannot remove elements beyond the base
of the stack. If such is the case, we will reach a stack underflow condition.
TOP = MAXSTX indicates that the stack is full and TOP = NULL
indicates that stack is empty.

10. Write an algorithm for push operation. (U)


ANS : ALGORITHM: PUSH (STACK, TOP, ITEM) STACK is the array with N
elements. TOP is the
pointer to the top of the element of the array. ITEM to be inserted.
Step 1: if TOP = N-1 then [Check Overflow]
PRINT “ STACK is Full or Overflow”
Exit
[End if]
Step 2: TOP = TOP + 1 [Increment the TOP]
Step 3: STACK[TOP] = ITEM [Insert the ITEM]
Step 4: Return

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 10


CHAPTER 4 - DATA STRUCTURES

11. Write an algorithm for POP operation. (U)


ANS : ALGORITHM: POP (STACK, TOP, ITEM) STACK is the array with N
elements. TOP is the pointer to the top of the element of the array. ITEM to be
inserted.
Step 1: if TOP = NULL then [Check Underflow]
PRINT “ STACK is Empty or Underflow”
Exit
[End if]
Step 2: ITEM = STACK[TOP] [copy the TOP Element]
Step 3: TOP = TOP - 1 [Decrement the TOP]
Step 4: Return

12. Write any three applications of stacks. (A)


ANS : Refer below section
13. Write any three applications of queues. (A)
ANS : Application of Queue:
Simulation
Various features of Operating system
Multi-programming platform systems.
Different types of scheduling algorithms
Round robin technique algorithms
Printer server routines
Various application software’s is also based on queue data structure.
14. Explain the memory representation of queues using array. (U)
ANS : Memory Representation of a queue using array:
Queue is represented in memory using linear array.
Let QUEUE is a array, two pointer variables called FRONT and REAR are
maintained.

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 11


CHAPTER 4 - DATA STRUCTURES

The pointer variable FRONT contains the location of the element to be removed or
deleted.
The pointer variable REAR contains location of the last element inserted.
The condition FRONT = NULL indicates that queue is empty.
The condition REAR = N-1 indicates that queue is full.

15. Explain types of linked list. (U)


ANS : Refer below section
16. Define the following: a. Tree b. Graph c. Root node. (U)
ANS : Trees:
A tree is a data structure consisting of nodes organized as a hierarchy.
GRAPH
A graph is a collection of nodes called vertices and the connection between them
called edges.
Root: The topmost node of the tree.

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 12


CHAPTER 4 - DATA STRUCTURES

Five marks questions:


1. What is primitive data structure? Explain the different operations performed on
primitive data structure. (U)
ANS : Primitive Data structures:
Data structures that are directly operated upon the machine-level instructions are
known as primitive data structures.
The integers, float, character data, pointers are primitive data structures.
Operations on Primitive Data structures:
Create: Create operation is used to create a new data structure. This operation
reserves memory space for the program elements. It may be carried out at compile
time and run time.
Example: int n=15; // memory spaced to be created for n.
Select: This operation is used programmers to access the data within the data
structure. This operation updates or alters data.
Example: cin>>x;
Update: This operation is used to change data of data structures. An assignment
operation is a good example.
Example: int n = 5; //modifies the value of n to store the new value 5 in it.
Destroy: This operation is used to destroy or remove the data structure from the
memory space. In C++ one can destroy data structure by using the function called
delete.
2. Explain the different operations performed on linear data structure. (U)
ANS : Refer below section
3. Write an algorithm for searching an element using linear search method. (A)
ANS :
ALGORITHM: Linear_Search (A, N, Element) A is an array with N elements.
Element is the being searched in the array.
Step 1: LOC = -1 [Assume the element does not exist]

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 13


CHAPTER 4 - DATA STRUCTURES

Step 2: for I = 0 to N-1 do


if ( Element = A [ I ] ) then
LOC = I
Goto Step 3
[End if]
[End of for loop]
Step 3: if ( LOC >= 0 ) then
Print Element, “Found in Location”, LOC
else
Print Element, “Not Found”
Step 4: Exit

4. Write an algorithm for searching an element using binary search method. (A)
ANS :
ALGORITHM: Binary_Search (BEG, END, MID ELE) A is an array with N
elements. Let BEG, END, MID denote Beginning, end and middle location of the
array
Step 1: Set BEG=LB, END=UB LOC = -1
Step 2: While(BEG <= END)
MID = (BEG+END)/2
if ( ELE = A [ MID ] ) then
LOC = MID
Goto Step 3
else
if( ELE < A[MID])
END=MID-1;
else
BEG=MID+1;

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 14


CHAPTER 4 - DATA STRUCTURES

[End if]
[End if]
[End of While loop]
Step 3: if ( LOC >= 0 ) then
Print Element, “Found in Location”, LOC
else
Print Element, “Search is Unsuccessful”
Step 4: Exit

5. Write an algorithm for inserting an element into the array. (A)


ANS : ALGORITHM: Insert (A, N, ITEM, Pos) A is an array with N elements. ITEM is
the element to be inserted in the position Pos.
Step 1: for I = N-1 down to Pos
A[ I + 1] = A[I]
[End of for loop]
Step 2: A [Pos] = ITEM
Step 3: N = N+1
Step 4: Exit

6. Write an algorithm for deleting an element from the array. (A)


ANS : ALGORITHM: Delete (A, N, ITEM, Pos) A is an array with N elements. ITEM
is the element to be deleted in the position Pos and it is stored into variable Item.
Step 1: ITEM = A [Pos]
Step 2: for I = Pos down to N-1
A[ I ] = A[I+1]
[End of for loop]
Step 3: N = N-1
Step 4: Exit

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 15


CHAPTER 4 - DATA STRUCTURES

7. Explain the different operations performed on stacks. (A)


ANS : Operation on Stacks:
Stack( ): It creates a new stack that is empty. It needs no parameter and returns an
empty stack.
push(item): It adds a new item to the top of the stack.
pop( ): It removes the top item from the stack.
peek( ): It returns the top item from the stack but does not remove it.
isEmpty( ): It tests whether the stack is empty.
size( ): It returns the number of items on the stack.

8. Write the applications of stacks. (A)


ANS : Application of Stacks:
– letter by letter and
then pop letter from the stack.

element in a series of elements. Once you reach a dead end, you must backtrack.

by using stack.

postfix.

9. What is a queue? Explain different types of queues. (U)


ANS : 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”.

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 16


CHAPTER 4 - DATA STRUCTURES

FIFO list i.e. First-In First-Out.


Types of Queues:

o Simple Queue
o Circular Queue
o Priority Queue
o Dequeue ( Double Ended Queue)
Simple Queue: In simple queue insertion occurs at the rear end of the list and
deletion occurs at the front end of the list.

Circular Queue: A circular queue is a queue in which all nodes are


treated as circular such that the last node follows the first node.

Priority Queue: A priority queue is a


queue that contains items that have some
present priority. An element can be inserted
or removed from any position depending upon some priority.

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 17


CHAPTER 4 - DATA STRUCTURES

Dequeue: It is a queue in which insertion and deletion takes place


at the both ends.

10. Explain the different operations performed on queues. (U)


ANS : Operation on Queues:
Queue( ): It creates a new queue that is empty.
enqueue(item): It adds a new item to the rear of the queue.
dequeue( ): It removes the front item from the queue.
isEmpty( ): It tests to see whether the queue is empty.
size( ): It returns the number of items in the queue.

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 18


CHAPTER 4 - DATA STRUCTURES

11. Write an algorithm to insert an item into the queue. (A)


ANS : ALGORITHM: ENQUEUE (QUEUE, REAR, FRONT, ITEM) QUEUE is the
array with 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 [Check Overflow]
PRINT “QUEUE is Full or Overflow”
Exit
[End if]
Step 2: if FRONT = NULL then [Check Whether Queue is empty]
FRONT = 0
REAR = 0
else
REAR = REAR + 1 [Increment REAR Pointer]
Step 3: QUEUE[REAR] = ITEM [Copy ITEM to REAR position]
Step 4: Return
12. Write an algorithm to delete an item from the queue. (A)
ANS : ALGORITHM: DEQUEUE (QUEUE, REAR, FRONT, ITEM) QUEUE is the
array with 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 deleted.
Step 1: if FRONT = NULL then [Check Whether Queue is empty]
PRINT “QUEUE is Empty or Underflow”
Exit
[End if]
Step 2: ITEM = QUEUE[FRONT]
Step 3: if FRONT = REAR then [if QUEUE has only one element]
FRONT = NULL

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 19


CHAPTER 4 - DATA STRUCTURES

REAR = NULL
else
FRONT = FRONT + 1 [Increment FRONT pointer]
Step 4: Return
13. Write the applications of queues. (A)
ANS :

-programming platform systems.

14. What are the operations performed on the linked list? (U)
ANS :
A linked list is a linear collection of data elements called nodes.

o The first part contains the information of the element.


o The second part contains the memory address of the next node in the list. Also
called Link part.
Operation on Linked List:

o Creating a linked list


o Traversing a linked list
o Inserting an item into a linked list.
o Deleting an item from the linked list.
o Searching an item in the linked list
o Merging two or more linked lists.

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 20


CHAPTER 4 - DATA STRUCTURES

15. Define the following : a. Root Node b. Leaf Node c. Height d. Depth e. Internal
node. (U)
ANS :

Root: The topmost node of the tree.

Leaf or terminal node: Nodes that do not have any children are called leaf node. (J,
K, L)
Height: the height or depth of a tree is defined to be the maximum number of nodes
in a branch of tree. ( The height of tree is 5)
Internal Node: A node has one or more children is called an internal node. ( B, C, D,
H)

Prepared by : Sanil Abraham, Deeksha Mahalakshmipuram Page 21

You might also like