Module 1 — Concise Answers (10 marks style)
Q1. Define data structures. Explain different types with examples.
A data structure stores and organizes data to allow efficient access and modification. Types: 1. Linear:
Array, Linked List, Stack, Queue (elements in sequence). 2. Non-linear: Tree, Graph, Hash Table
(hierarchical or networked). Conceptual schematics: Array -> contiguous boxes; Linked List -> nodes with
data and next pointer; Tree -> nodes with child pointers; Graph -> nodes with adjacency lists/matrix.
Q2. Different operations performed on data structures.
Common operations: Create/Initialize, Insert, Delete, Search, Update, Traverse, Sort, Merge, Copy,
Destroy. Specialized: Push/Pop (stack), Enqueue/Dequeue (queue), Insert/Delete in tree/graph.
Q3. Different functions of dynamic memory allocation.
malloc(size): allocate uninitialized block of bytes. calloc(n, size): allocate and zero-initialize n elements.
realloc(ptr, newsize): resize previously allocated block; may move it. free(ptr): release memory back to
heap.
Q4. Write a C program to illustrate dynamic memory functions.
Example demonstrating malloc, calloc, realloc and free:
/* demo_malloc_calloc_realloc_free.c */
#include <stdio.h>
#include <stdlib.h>
int main(){
int *a = (int*) malloc(5 * sizeof(int));
if(!a){ perror("malloc"); return 1; }
for(int i=0;i<5;i++) a[i]=i+1;
int *b = (int*) calloc(5, sizeof(int)); // zero-initialized
if(!b){ perror("calloc"); free(a); return 1; }
int *r = (int*) realloc(a, 8 * sizeof(int)); // resize to 8
if(!r){ perror("realloc"); free(a); free(b); return 1; }
a = r; for(int i=5;i<8;i++) a[i]=i+1;
free(a); free(b);
return 0;
}
Q5. What is pointer? Declaration and example.
A pointer stores the memory address of another variable. Declaration: int *p; Example: int x = 5; int *p =
&x; // p points to x; *p yields 5.
Q6. Explain one-dimensional and two-dimensional arrays with
declaration and example.
1D array: int a[5]; example: int a[5] = {1,2,3,4,5}. 2D array: int m[3][4]; example: int m[2][3] =
{{1,2,3},{4,5,6}}; access m[i][j].
Q7. Write a C program for dynamic allocation of array.
Example dynamic allocation for an integer array:
/* dynamic_array.c */
#include <stdio.h>
#include <stdlib.h>
int main(){
int n;
printf("Enter n: ");
if(scanf("%d", &n)!=1) return 1;
int *arr = (int*) malloc(n * sizeof(int));
if(!arr){ perror("malloc"); return 1; }
for(int i=0;i<n;i++) arr[i]=i*2;
for(int i=0;i<n;i++) printf("%d ", arr[i]);
printf("\n");
free(arr);
return 0;
}
Q8. What is structure? Declaration and types with example.
A structure groups heterogeneous members. Example declaration: struct Student { int id; char name[50];
float marks; }; Types: nested structures (struct within struct), array of structures, pointer to struct. Used to
model records.
Q9. What is Union? Declaration and types with example.
A union stores different member types in the same memory location; only one member is valid at a time.
Declaration: union Data { int i; float f; char str[20]; }; Variants: simple union, union inside struct (tagged
union) where an enum indicates active member.
Q10. Differences between Structures and Unions.
Structure: each member has its own memory; size is sum of member sizes (plus padding); can access
multiple members concurrently. Union: members share memory; size equals largest member; only one
member valid at a time; memory-efficient.
Q11. ADT for Polynomials and representation in C.
ADT: Create, InsertTerm, Add, Subtract, Multiply, Evaluate, Display, Destroy. Representation: linked list of
terms or array of (coeff,exp) pairs. Example term struct: struct Term { int coeff; int exp; struct Term *next; };
Q12. What is sparse matrix? ADT and representation in C.
Sparse matrix: most elements are zero. ADT: Create, Insert(i,j,val), Get(i,j), Add, Transpose, Multiply,
Display, Destroy. Representation: list of triples (row,col,value), array of linked-lists per row, or compressed
row storage (CRS).
Q13. ADT for Strings and representation in C.
ADT: Create, Length, Concatenate, Compare, Substring, Insert, Delete, Search, Copy, Destroy.
Representation in C: null-terminated char arrays (char s[] or char* with allocated memory).
Q14. Define stack and explain Push(), Pop(), Display().
Stack (LIFO). Push: add element at top (check overflow). Pop: remove and return top element (check
underflow). Display: show elements from top to bottom. Implement using array (use top index) or linked list
(insert/remove at head).
Q15. Convert infix to postfix expressions.
i) a+b*c-d -> a b c * + d - ii) (a+b)*(c-d) -> a b + c d - * iii) ((a+(b-c)+d)^e+f) -> a b c - + d + e ^ f + iv)
(a*(b*c+d*e)+f) -> a b c * d e * + * f + v) (a+(b+c)/(d-e)) -> a b c + d e - / +
Q16. Algorithm to convert infix to postfix (stack-based).
1. Initialize empty operator stack and empty output list. 2. For each token: - If operand: append to output. -
If '(': push it. - If ')': pop operators to output until '('. Pop '('. - If operator op: while stack top has operator with
greater or equal precedence (and op is left-assoc), pop it to output. Push op. 3. After processing, pop
remaining operators to output. Example: a+b*c-d -> a b c * + d -.
Q17. Write a C program to implement stack using array.
Simple array-based stack implementation:
/* stack_array.c */
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int stack[MAX], top=-1;
void push(int x){ if(top==MAX-1){ printf("Overflow\n"); return;} stack[++top]=x; }
int pop(){ if(top==-1){ printf("Underflow\n"); return -1;} return stack[top--]; }
void display(){ for(int i=top;i>=0;i--) printf("%d ", stack[i]); printf("\n"); }
int main(){ push(10); push(20); display(); printf("popped=%d\n", pop()); display(); return 0; }
Q18. Evaluate the postfix expressions.
i) 62/3-42*+ => Stepwise (integer division): 6 2 / =3; stack:3; push 3; '-' => 3-3=0; 4 2 * =8; '+' => 0+8 = 8.
Result: 8 ii) 632-5*+1$7+ => Interpret $ as exponent. Steps: 3 2 - =1; 1*5=5; 6+5=11; 1^7=1; 11+1=12.
Result: 12 iii) 623+-382/+*2$3+ => Complex; evaluate with standard postfix algorithm (use a stack),
respecting $ as power. Compute step-by-step in exam.
Q19. Algorithm to evaluate postfix expression.
1. Create empty stack. 2. For each token: if operand push; if operator pop required operands (b then a),
compute a op b, push result. 3. After tokens, result is at stack top. Complexity O(n).
End of concise answers.