C PROG
C PROG
Object 10
28
27
26
25
24
23
22
21
20
19
18
17
16
15
14
13
12
11
9
8
7
6
4
3
2
1
This node is useful for getting the starting address of the linked list. As there is only a forward link present in
the linked list, for accessing the entire linked list it is necessary to obtain the starting address of the linked list.
Q. 15 What is advantage of an ADT ? AU: May-14, Dec.-18
Ans. : The advantages of ADT are
1. Change: The implementation of the ADT can be changed without making changes bruin the client program
that uses the ADT.
2. Understandability: ADT specifies what is to be done and does not specify the implementation details.
Hence code becomes easy to understand due to ADT.
3. Reusability: The ADT can be reused by some program in future.
4. Flexibility: Different implementations are possible for the same ADT.
Q. 16 Should arrays of linked lists be used for the following type of applications. Justify your Answer.
a) Many search operations in sorted list.
b) Many search operations in unsorted list. AU: May-14
Ans. :
a) If the list is sorted then using linked list the desired element can be searched, simply by moving forward
using 'next' pointer.
b) If a list is not sorted then using arrays the desired element can be searched. The arrays facilitates the access to
random element.
Q. 17 What are abstract data type? AU: Dec.-14
OR Define ADT AU: May-15
Ans. : Refer Q. 5.
Q. 18 What is static linked list? State any two applications of it. AU: May-15
Ans. The linked list structure which can be represented using arrays is called static linked list.
1. It is easy to implement, hence for creation of small databases, it is useful.
2. The searching of any record is efficient, hence the applications in which the record need to be searched
quickly, the static linked lists are used.
Q. 19 Write syntax of calloc() and relloc() and mention its applications in the linked list AU: May-15
Ans. : calloc() is for allocating the required amount of memory. The syntax is void *calloc(size_t nitems, size_t
size)
This function returns the pointer to the allocated memory. The calloc function is just similar to malloc but it
initializes the allocated memory to zero and malloc does not.
The realloc() function modifies allocated memory size by malloc() and calloc() functions to new size. The
syntax is
void *realloc (void *ptr, size_t size)
If insufficient memory exists then to expand the memory block, realloc() is used.
Q. 20 What are the disadvantages of linked list over arrays. AU: Dec.-18
Ans. 1) In linked list, we can access elements in sequence. So it is very difficult and time consuming to access
element randomly.
2) Some amount of memory gets wasted in storing the next node's address in each nodes.
Q. 1Give an example that shows how a stack is used by a computer system.
Ans. : In computer system the memory model consists of stack memory stores types of variables that have a
fixed lifetime based on how C programs run. It is a section of RAM that grows and shrinks as the program runs.
When you call a function, its parameters and any variables you have defined in that function (which are not static)
are stored on the stack.
Q. 2 What do you understand by polish notation? Explain.
Ans. : This is also called as prefix notation. In this type of notation the operator followed by two operands. For
example if (a+b)*c is a given expression then its polish notation will be *+ abc.
Q. 3 What is a top pointer of a stack?
Ans. : The top denotes the only one end of the stack from which the element can be inserted or deleted. When
we push the element onto the stack, the top is incremented. When we pop the element from the stack the top is
decremented.
Q. 4 Write the postfix notation for following expression. (A+B)*C-(D-E)^F
Ans.: Following steps can be followed for computing the postfix expression -
Step 1: (AB+)*C-(D-E)^F
Step 2: (AB+C*)-(D-E)^F
Step 3: (AB+C*)-(DE-)^F
Step 4: (AB+C*)-(DE-F^)
Step 5: AB+C*DE-F^-
Q. 5 Write any two applications of stack. AU: Dec.-14, 18, 19
Ans. : The stack can be used for
1. Conversion of expression from one form to another. Various forms of expression are infix, prefix and postfix.
2. Evaluation of postfix expression.
3. Evaluation of recursive functions.
4. Reversing the string.
5. For checking the well formedness of parenthesis.
Q. 6 List the characteristics of stacks.
Ans. :
1. Insertion and deletion can be made by one end only. Hence the element inserted last will be first one to come
out. Hence sometimes it is called LIFO.
2. Stacks are useful for evaluating an expression.
3. Stacks can store the functions calls.
Q. 7 Write the role of stack in function call.
Ans. : The stack is an useful data structure for handling the recursive function calls. When a recursive call is
encountered, the status of call is pushed onto the stack. And at the return of the call the stack is popped off. Thus
execution of recursive statements is done with the help of stack.
Q. 8 What is Last-In-First-Out strategy? Which data structure follows this strategy?
Ans. : In the Last In First Out strategy we insert the element in the data structure lastly and while removing the
elements from this data structure, the element which is inserted lastly will get removed first.
The stack data structure makes use of Last In First Out(LIFO) data structure.
Q. 9 Write the steps to reverse the contents of the list with the help of stack data structure.
Ans. :
Step 1: Read each element from the list and push it onto the stack.
Step 2: Pop the element from the stack and store the popped element in a separate
Step 3: Read the array completely from left to write. This will be the reversed list of the elements.
Q. 10 Which data structure is used in handling the recursive function?
Ans. : The stack is used to handle the recursive function call.
Q. 11 Given the prefix for an expression write its postfix. AU: May-15
-*-+abc/ef-g/hi
Ans. :
-*-+abc/ef-g/hi
- * - T1 cef-g/hi T1 = ab+
-T2/ef-g/hi T2 = T1c-
- T2 T3 g/hi T3=ef /
- T4g-/hi T4 = T2T3*
- T4 g T5 T5 = hi/
-T4 T6 T6=g T5-
T7 T7= T4 T6
By backward substitution,
T7
T4 T6-
T2 T3 T6 -
T1 c T3 * T6 -
ab + c - T3 * T6 -
ab + c –ef/*t6-
ab + c -ef/* g T5--
ab + c - ef /* ghi/ -- is postfix expression
Q. 12 Give the infix for an expression, write its prefix a* b/c+d?
Ans. : The prefix expression is /* ab + cd.
Q. 13 Convert the following infix expression to postfix expression using stack.
AU: May - 19
Ans. :
Q.19 List the operations defined on binary trees data type with a suitable example.
Ans. :
Various operations that can be performed on binary trees are - 1. Insertion 2. Deletion 3. Traversal
The insertion operation is performed to insert a node at any desired position.
For example -
We can delete any desired node from the binary tree except root node.
For example -
Traversal means a walkover a binary tree. It can be preorder, postorder and inorder traversal.
Q.20 Write an algorithm to declare nodes of a tree structure.
Ans. Algorithm :
1. Declare a C structure for the node of a binary tree
typedef struct node
{
int data;
struct node *left;
struct node *right;
}bin;
2. Create a node by allocating memory by using the dynamic memory allocation.
New (bin*)malloc(sizeof(bin));
3. Initialize the Left and Right child pointers to NULL.
New->left=NULL;
New->right=NULL;
Thus a single node gets created. This is the first node of the tree, hence it is called as the root node.
root=New;
4. Attach the next subsequent nodes to either as a left child or a right child to the corresponding parent nodes
depending on the user's choice.
Q.21 Why it is said that the searching a node in a binary search tree is efficient than that of a simple binary
tree?
Ans. : In the binary search tree the nodes are arranged in such a way that the left node is having less data value
than root node value. And the right nodes are having larger value than that of root. Because of this while searching
any node the value of target node will be compared with the parent node and accordingly either left sub branch or
right sub branch will be searched. This procedure will be repeated if required. So one has to compare only particular
branches. Thus searching becomes efficient.
Q.22 If a binary tree with n nodes is represented sequentially, then for any node with index i (a) 1 ≤ i ≤n,
then left child (i) is at 2i, if 2i ≤n. (b) If 2i>n, then I has no left child. Name the type of binary tree which satisfies
both (a) and (b).
AU: May-11
Ans. : The complete binary tree.
Q.23 What is meant by equivalent binary tree?
AU : May-11
Ans. : The two binary trees are said to be equivalent if the sequence of there traversal is same for example -