SHREE DEVI INSTITUTE OF TECHNOLOGY
Kenjar, Mangalore-574142
Department of Information Science & Engineering
1st Internal Test - November 2024
Sem/Sec: 3rd Semester ISE Max. Marks: 25
Course Name:Data Structures and Application Duration: 1 Hour
Course Code:BCS304 Date:08/11/2024
Question Marks RBT CO
Number Note: Answer any one full question from each part Level
PART A
1 a Define Data Structures.Explain the operations of Data 5M L2 CO1
Structures
b Define Stack.Explain different operations that can be 4M L2 CO1
performed on stacks.
c Convert the following Infix expression to Postfix 4M L3 CO2
Expression = A+B*C/D-F+A^E
OR
2 a What is Structure? Differentiate Structures and Unions. 5M L2 CO1
b Write c function for 8M L3 CO2
a. Circularqueue insert(),
b. Circularqueue delete(),
Explain the main drawback of a linear queue. How does a
circular queue address this issue?
PART B
3 a Write c function to add and attach two polynomials.Show 8M L3 CO3
the linked list representation and its addition.
P1 : 15X3+4X+8
P2 : -8X3+3X2+1
b Explain four dynamic memory allocation with syntax. 4M L2 CO1
OR
4 a Define Sparse matrix.Represent the following sparse matrix 4M L3 CO3
in linked list format.
b Define binary search tree.Construct Binary Search Tree for 8M L3 CO3
the given set of values
14,13,10,25,12,3,18,60,80,9,78,52,65,17,29
Also perform inorder,preorder and postorder traversal of the
obtained tree.
Subject Faculty Department IA Coordinator Head of the Deparment
SCHEME OF EVALUATION
Sem:III Date:08-11-2024
Course Name/Code:Data Structures and Application/BCS304 Max.Marks:25
Q.No Answer in Breief Marks
PART A
1a. Define Data Structures.Explain the operations of Data Structures 5M
(Definition = 1M,Any 4 operations = 4M)
Data Structure can be defined as the group of data elements which provides
an efficient way of storing and organising data in the computer so that it can
be used efficiently.
OR
Data can be organised in many different ways. The logical or
mathematical model of a organisation of data is called a Data Structure.
Ex : arrays, Linked List, Stack, Queue, etc.
Operations:
1. Insertion:
Insertion means inserting or adding new data elements to the collection.
For example, we can use the insertion operation to add the details of a new
employee the company has recently hired.
Ex :
int myArray[5];
myArray[0] = 10;
myArray[1] = 20;
// ...
2. Deletion:
Deletion means to remove or delete a specific data element from the given
list of data
elements. For example, we can use the deleting operation to delete the
name of an employee
who has left the job.
Ex:
int myArray[5] = {10, 20, 30, 40, 50};
// Delete the element at index 2 (30)
for (int i = 2; i < 4; i++) {
myArray[i] = myArray[i + 1];
}
Search:
Search is another data structure operation which means to find the location
of one or
more data elements that meet certain constraints. Such a data element may
or may not be
present in the given set of data elements. For example, we can use the
search operation to find
the names of all the employees who have the experience of more than 5
years.
Ex:
int myArray[5] = {10, 20, 30, 40, 50};
int target = 30;
for (int i = 0; i < 5; i++) {
if (myArray[i] == target) {
// Element found at index i
break;
}
}
4. Traversal:
Traversing a data structure means accessing each data element exactly
once so it can
be administered. For example,traversing is required while printing the
names of all the
employees in a department.
Ex:
int myArray[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
// Process each element (e.g., print or perform some operation)
}
5. Accessing:
Retrieving data at a specific location within a data structure.
Ex:
int myArray[5] = {10, 20, 30, 40, 50};
int element = myArray[2]; // Accessing the element at index 2 (30)
6. Update:
Modifying data within a data structure. The Update operation allows us to
update or
modify the data in the data structure. We can also update any particular
data by specifying some
conditions inside the loop, like the Selection operation.
Ex:
int myArray[5] = {10, 20, 30, 40, 50};
myArray[2] = 35; // Updating the element at index 2 to 35
7. Sorting:
Rearranging elements in a specific order within a data structure. Sorting
means to
arrange the data elements in either Ascending or Descending order
depending on the type of
application. For example, we can use the sorting operation to arrange the
names of employees
in a department in alphabetical order.
Ex:
int myArray[5] = {50, 10, 40, 20, 30};
// Perform bubble sort
// After sorting, the array will be {10, 20, 30, 40, 50}
b Define Stack.Explain different operations that can be performed on 4M
stacks.
(Definition = 1M,Any 4 operations = 4M)
“A stack is an ordered list in which insertions (pushes) and deletions
(pops) are made at one end called the top.”
Given a stack S= (a0, ... ,an-1), where a0 is the bottom element, an-1 is the
top element, and ai is on top of element a i-1, 0 < i < n.
Since the last element inserted into a stack is the first element removed, a
stack is also known as a Last-In-First-Out (LIFO) list
1. Stack Create
Stack CreateS(maxStackSize )::=
#define MAX_STACK_SIZE 100 /* maximum stack size*/
typedef struct {
int key;
/* other fields */
} element;
element stack[MAX_STACK_SIZE];
int top = -1;
2. Boolean IsEmpty(Stack)::= top < 0;
3. Boolean IsFull(Stack)::= top >= MAX_STACK_SIZE-1;
The IsEmpty and IsFull operations are simple, and is implemented directly
in the program push
and pop functions. Each of these functions assumes that the variables stack
and top are global.
4. Push( )
Function push checks whether stack is full. If it is, it calls stackFull(
), which prints an error message and terminates execution. When the stack is
not full, increment top and assign item to stack [top].
void push(element item)
{ /* add an item to the global stack */
if (top >= MAX_STACK_SIZE-1)
stackFull();
stack[++top] = item;
}
5. Pop( )
Deleting an element from the stack is called pop operation.The
element is deleted only from the top of the stack and only one element is
deleted at a time.
element pop ( )
{ /*delete and return the top element from the stack */
if (top == -1)
return stackEmpty(); /*returns an error key */
return stack[top--];
}
6. stackFull( )
The stackFull which prints an error message and terminates execution.
void stackFull()
{
fprintf(stderr, "Stack is full, cannot add element");
exit(EXIT_FAILURE);
}
c Convert the following Infix expression to Postfix 4M
Expression = A+B*C/D-F+A^E
(Marks are given based on correct steps-overall 4M)
OR
2 What is Structure? Differentiate Structures and Unions. 5M
a (Definition = 1M,Any 4 operations = 4M)
• A structure is a collection of data items,where each item is identified as to its
type and name.
• Ex:
struct{
char name[10];
int age;
float salary;
}person;
This creates a variable whose name is person and that has 3 fields.
b Write c function for 8M
a. Circularqueue insert(),
b. Circularqueue delete(),
Explain the main drawback of a linear queue. How does a circular queue
address this issue?
(Function = 2*2=4M,Drawback=4M)
Program: Add to a circular queue
void addq(element item)
{ /* add an item to the queue */
rear = (rear +1) % MAX_QUEUE_SIZE;
if (front == rear)
queueFull(); /* print error and exit */
queue [rear] = item;
}
Program: Delete from a circular queue
element deleteq()
{ /* remove front element from the queue */
element item;
if (front == rear)
return queueEmpty( ); /* return an error key */
front = (front+1)% MAX_QUEUE_SIZE;
return queue[front];
}
Drawback of Queue
• When item enters and deleted from the queue, the queue gradually
shifts to the right as shown in figure.
• In this above situation, when we try to insert another item, which
shows that the queue is full. This means that the rear index equals
to MAX_QUEUE_SIZE -1. But even if the space is available at the
front end, rear insertion cannot be done.
CIRCULAR QUEUES
• It is “The queue which wrap around the end of the array.” The array
positions are arranged in a circle as shown in figure.
• In this convention the variable front is changed. front variable points
one position counterclockwise from the location of the front element
in the queue. The convention for rear is unchanged.
PART B
3a Write c function to add and attach two polynomials.Show the linked 8M
list representation and its addition.
P1 : 15X3+4X+8
P2 : -8X3+3X2+1
(Function=4M,Representation and addition=4M)
b Explain four dynamic memory allocation with syntax. 4M
(Each carries 1M)
The concept of dynamic memory allocation in c language enables the C
programmer to allocate memory at runtime. Dynamic memory allocation in
c language is possible by 4 functions of stdlib.h header file.
1. malloc() : allocates single block of requested memory. It doesn't
initialize memory at execution time , so it has garbage value
initially.It returns NULL if memory is not sufficient.
syntax : ptr=(cast-type*)malloc(byte-size)
2. calloc() : allocates multiple block of requested memory.
syntax : ptr=(cast-type*)calloc(number, byte-size)
3. realloc() : reallocates the memory occupied by malloc() or calloc()
functions.
syntax : ptr=realloc(ptr, new-size)
4. free() : frees the dynamically allocated memory. The memory
occupied by malloc() or calloc() functions must be released by calling free()
function. Otherwise, it will consume memory until program exit.
syntax : free(ptr)
OR
4a Define Sparse matrix.Represent the following sparse matrix in linked 4M
list format.
(Definition = 1M,Represent= 3M)
• What is Sparse Matrix?
A matrix which contains many zero entries or very few non-zero entries is
called as Sparse matrix
b Define binary search tree.Construct Binary Search Tree for the 8M
given set of values
14,13,10,25,12,3,18,60,80,9,78,52,65,17,29
Also perform inorder,preorder and postorder traversal of the obtained
tree.
(Definition=2M,Construction=3M,Traversal=3M)
A Binary Search Tree (or BST) is a data structure used in computer
science for organizing and storing data in a sorted manner. Each node in
a Binary Search Tree has at most two children, a left child and
a right child, with the left child containing values less than the parent
node and the right child containing values greater than the parent node
14
/ \
13 25
/ / \
10 18 60
/ \ / / \
3 12 17 52 80
\ / /
9 29 78
/
65
Preorder :14, 13, 10, 3, 9, 12, 25, 18, 17, 60, 52, 29,80, 78,65
Inorder :3, 9, 10, 12, 13, 14, 17, 18, 25, 29, 52, 60, 65, 78, 80
Postorder : 9, 3, 12, 10, 13, 17, 18, 29, 52, 65, 78, 80, 60, 25, 14
Faculty Incharge HOD