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

Data Structured and Algorithm

The document is an examination paper for the Higher National Diploma in Information Technology at the Sri Lanka Institute of Advanced Technological Education, focusing on Data Structures and Algorithms. It includes questions on defining data structures, comparing linked lists, implementing stack and queue operations, and tree data structures, along with C++ programming tasks. Candidates are instructed to answer four out of five questions within a two-hour timeframe.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Data Structured and Algorithm

The document is an examination paper for the Higher National Diploma in Information Technology at the Sri Lanka Institute of Advanced Technological Education, focusing on Data Structures and Algorithms. It includes questions on defining data structures, comparing linked lists, implementing stack and queue operations, and tree data structures, along with C++ programming tasks. Candidates are instructed to answer four out of five questions within a two-hour timeframe.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

All Rights Reserved]

SRI LANKA INSTITUTE OF ADVANCED TECHNOLOGICAL EDUCATION


(Established in the Ministry of Higher Education, vide in Act No. 29 of 1995)

Higher National Diploma in Information Technology


First Year, Second Semester Examination-2019
HNDIT1211: Data Structures and Algorithms

Instructions for Candidates: No. of questions : 05


Answer four (04) questions only. No. of pages : 05
Time : Two hours

Marking Scheme

Question 01
a. Define Data Structure? (02 Marks)
Data structure is the arrangement of data in a computer memory/storage.

b. Briefly describe primitive and abstract data types? give one example for each. (04 Marks)
Primitive Data Types:
Primitive Data Types are basic data types of a programming language. These refer to
two things: a data item with certain characteristics and the permissible operations on that
data. Ex: integer and float.
Abstract Data Types:
ADT is a specification of a mathematical set of data and the set of operations that can be
performed on the data. Ex: Stack, Queue, LinkedList, Tree

c. Briefly explain the following terms. (06 Marks)


i. Best Case Efficiency
Best case efficiency is the minimum number of steps that an algorithm can take any
collection of data values.

ii. Worst Case Efficiency


Worst case efficiency is the maximum number of steps that an algorithm can take for any
collection of data values.

HNDIT 1 Data Structures and Algorithms 1


iii. Average Case Efficiency
• Is the efficiency averaged on all possible inputs
• Must assume a distribution of the input
• Is normally assumed for uniform distribution (all keys are equally probable)

d. Write C++ code to create the following array and display it on the screen. (Hint: consider the
pattern of the data set when creating it) (06 Marks)

2 4 6 8 10 12 14

Void main()
{
int new _array[10]; (01 Mark)
for(int i= 0; i<7;i++) (01 Mark)
new_array[i] = 2i+2; (02 Mark)
for(int i=0;i<7;i++) (01 Mark)
cout<<”The array values are\n”<<new_array[i]; (01 Mark)
}
e. Write a C++ program that stores the given marks of the student and display the average mark
of each student. Use 2D array to store the subject marks.
Mathematics Science English
75 62 88
85 82 90
55 63 73
70 61 65
59 63 70

(07 Marks)

HNDIT 1 Data Structures and Algorithms 2


Void main ()
{
int average;
int marks[5][3]={{75,62,88},{85,82,90},{55,63,73},{70,61,65},{59,63,70}};
for(int i= 0; i <5; i++){
average = 0;
for(int j=0; j< 3; j++){
average = average + marks[i][j];
cout<<"Average mark for student" <<(i+1) <<" is:"<<average;
cout<<endl;
}
}
}
NOTE
 2 Marks for marks array
 1 Marks for correct loops
 2 Marks for calculating correct Average value
 2 Mark for displaying result

Question 02
a. Give single answer to a pointer? (02 Marks)
Pointer contains memory address of a particular type of data. In C++, we use * to declare
a pointer.
Ex: int* iptr; char* cptr

b. Creating a structure to store ID number, Name, and Gender of an Employee (05 Marks)
struct Employee{
int ID;
char name[20];
char gender;
};

HNDIT 1 Data Structures and Algorithms 2


c. Compare and contrast Singly Linked List and Doubly Linked List with suitable diagrams
(01 Marks for each explanation; 02 marks for each diagram; total=06 Marks)

Singly Linked List allows to traverse elements in one direction only.

Doubly Linked List allows to traverse elements in both directions.

d. Implementing the following Linked List operations using structural diagram (06 Marks)

i. initializeList()

ii. insertFirstElement(25)

iii. insertAtEnd(30)

HNDIT 1 Data Structures and Algorithms 3


iv. insertAfter(25,50)

v. deleteElt(25)

vi. insertAtFront(42)

e. Write the following C++ method which is used to insert an element at the end of a linked
list. Assume that the list nodes are created using Struct listNode
(06 Marks)
void LinkedList::insertAtEnd(int elt) {
}

HNDIT 1 Data Structures and Algorithms 4


void LinkedList::insertAtEnd(int elt) {
listNode *newNode, *curNode;
newNode=new listNode;
newNode->data=elt;
newNode->next=NULL;
if (!head) head=newNode;
else {
curNode=head;
while (curNode->next != NULL)
curNode = curNode->next;
curNode->next = newNode;
}
}

Question 03
a. Compare Stack and Queue data structures? (04 Marks)
Stack uses LIFO method to insert and retrieve data
Queue uses FIFO method.
Stack use top to find the location
Queue use fron and rear both to find the location
b. Briefly describe the two (02) different implementations of Stack using suitable diagrams?
(04 Marks)
Array based implementation Linked List based implementation
Top= 4 Top
arr[4] 50
arr[3] 40
arr[2] 30
arr[1] 20
arr[0] 10

HNDIT 1 Data Structures and Algorithms 5


c. Show the graphical implementation of following Stack operations
(1.5*4 = 6)
i. initializeStack()

ii. push(15)

iii. y = topElement( )
y=15

iv. x= pop()
x=15

HNDIT 1 Data Structures and Algorithms 6


d. Following C++ code represent the Static implementation of Stack. Write the relevant
C++ code segment for the given blanks. (06 Marks)
#define STK_SIZE 20
class Stack {
int top;
int stack_array[…..(i)….];
public:
…(ii)… ();
void push(…(iii)….);
…(iv)… pop();
….(v)… isEmpyt();
int isFull();
void displayStack();
};
…vi….::Stack()
{
top = 0;
}

void Stack::push(int value)


{
if (top == STK_SIZE -1)
cout<<”Stack ……(vii)……..”;
else
stack_array[……(viii)……]=……(ix)……. ;
}
void Stack::pop()
{
if (top == ….(x)…)
cout<<”Stack Underflow”;
….(xi)….

HNDIT 1 Data Structures and Algorithms 7


cout<<stack_array[…(xii)…]<<”Is deleted”;
}

(i) STK_SIZE (vii) Overflow


(ii) Stack (viii) top ++
(iii) int Value (ix) Value
(iv) void (x) 0
(v) int (xi) else
(vi) Stack (xii) (top --)

e. Write C++ code for the displayStack() method mentioned in the (Question(02) part d).
(05 Marks)
Void displayStack()
{
Cout<<”Stack\n”;
Cout<<”---------------”;
for(int i=top-1;i>=0;i--)
{
Cout<<”|”<<stack_array[i]<<”|”;
Cout<<endl;
}
}

Question 04
a. Define Tree data structure. (02 Marks)
A tree is a data structure that emulates a hierarchical tree structure with a set of
linked nodes.

b. Draw a Tree diagram using the following list of information. (04 Marks)
Root node: A
Leaf nodes: L, M, N, O
Level one nodes: E, F, G

HNDIT 1 Data Structures and Algorithms 8


Children’s of node E: L, M
Parent of N and O: G

c. Define the following terms related to tree data structure. (02*3=06 Marks)
i. Path between two nodes
Path between two nodes in a tree is a sequence of edges which connect those
nodes
ii. Height of a Tree
The height of a tree is the largest depth of any of its nodes
iii. Degree of a Node
The degree of a node is the number of its children

d. Differentiate the Binary Tree and Binary Search Tree using suitable diagrams (06 Marks)
Binary Tree Binary Search Tree
Binary Tree is a specialized form of tree which Binary Search Tree is a type of binary tree which
represents hierarchical data in a tree structure. keeps the keys in a sorted order for fast lookup.
Each node must have at the most two child nodes The value of the nodes in the left subtree are less
with each node being connected from exactly than or equal to the value of the root node, and
one other node by a directed edge. the nodes to the right subtree have values
greater than or equal to the value of the root
node.
There is no relative order to how the nodes It follows a definitive order to how the nodes
should be organized. should be organized in a tree.
It’s basically a hierarchical data structure that is a It’s a variant of the binary tree in which the nodes
collection of elements called nodes. are arranged in a relative order.
It is used for fast and efficient lookup of data and It is mainly used for insertion, deletion, and
information in a tree structure. searching of elements.

HNDIT 1 Data Structures and Algorithms 9


Binary Tree Binary Search Tree

e. Insert the following data set to the Binary Search Tree. Show the implementation using
diagrams. (07 Marks)
30,12,9,18,33,45,16
1 mark for root
2 marks for level by level (2*3 = 6 marks)

Question 05
a. Briefly describe the enqueue and dequeue operations of the queue data structure using
diagrams (04 Marks)

The following steps should be taken to enqueue (insert) data into a queue −

• Step 1 − Check if the queue is full.

HNDIT 1 Data Structures and Algorithms 10


• 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.

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.

b. Sort the following data set using Bubble Sort Algorithm. Clearly mention your steps.
9,11,85,3,48,71,15,8 (1Mark per each pass (Pass1-Pass6) - 06 Marks)
Pass 0 (Original)

HNDIT 1 Data Structures and Algorithms 11


9 11 85 3 48 71 15 8
Pass1
9 11 3 48 71 15 8 85
Pass2
9 3 11 48 15 8 71 85
Pass3
3 9 11 15 8 48 71 85
Pass4
3 9 11 8 15 48 71 85
Pass5
3 9 8 11 15 48 71 85
Pass 6
3 8 9 11 15 48 71 85
Pass7 (Sorted)
3 8 9 11 15 48 71 85

c. Write a pseudo code for Sequential Search algorithm. (06 Marks)


Pseudo code:
//It returns the location of the target t in the array a[] with n elements.
int sequentialSearch(a[],n,t)
for i = 0 to n-1
if (a[i]=t) return i;
next i
return -1;

d. State best case and worst-case efficiency for insertion and selection sort algorithms.
(04 marks)
Selection Sort
• Worst case performance: O(n2)
• Best case performance: O(n2)
Insertion Sort
• Worst case performance: O(n2)
• Best case performance: O(n)

HNDIT 1 Data Structures and Algorithms 12


e. Mention the steps to search the number 29 of the following array using Binary search
algorithm. (05 marks)
12 15 18 21 27 29 32

Find middle position – (0 + 6)/2 = 3


3rd element = 21
21 > 29
Check the upper half of the array
Find middle position– (4 + 6)/2 = 5
5th element = 29
29 = 29
So element found

HNDIT 1 Data Structures and Algorithms 13

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy