DATA STRUCTURES LAB – LINE BY LINE CODE
EXPLANATION (VIVA READY)
PROGRAM 2: STRING PATTERN MATCHING – LINE BY LINE
#include
Includes standard input-output functions.
char str[50], pat[20], rep[20], res[50];
Declares character arrays for main string, pattern, replacement string, and result.
int i, j, k, found = 0;
Loop variables and a flag to check whether pattern is found.
void stringmatch()
Function to perform pattern matching and replacement.
for (i = 0, j = 0; str[i]; i++)
Traverses the main string character by character.
for (k = 0; pat[k] && str[i + k] == pat[k]; k++);
Compares pattern with main string from current position.
if (!pat[k])
Checks whether full pattern is matched.
found = 1;
Sets flag indicating pattern found.
for (k = 0; rep[k]; k++)
Copies replacement string to result string.
res[j++] = rep[k];
Stores replacement characters into result.
i += k - 1;
Skips already matched pattern characters.
else
If pattern does not match.
res[j++] = str[i];
Copies original character into result.
res[j] = '\0';
Ends the result string.
PROGRAM 3: STACK & PALINDROME – LINE BY LINE
#define MAX 3
Defines maximum size of stack.
int s[MAX], top = -1;
Stack array and top pointer.
void push(int item)
Function to insert element into stack.
if (top == MAX - 1)
Checks stack overflow.
s[++top] = item;
Inserts element after incrementing top.
int pop()
Function to remove element from stack.
if (top == -1)
Checks stack underflow.
return s[top--];
Returns top element and decrements top.
void palindrome()
Function to check palindrome.
for (int i = 0; i <= top / 2; i++)
Loops through half of stack.
if (s[i] != s[top - i])
Compares symmetric elements.
flag = 0;
Sets flag false if mismatch occurs.
PROGRAM 4: INFIX TO POSTFIX – LINE BY LINE
char infix[30], postfix[30], stack[30];
Arrays for infix, postfix, and stack.
int prec(char ch)
Returns precedence of operators.
push('#');
Pushes stack bottom marker.
if (isalnum(ch))
Checks if character is operand.
postfix[j++] = ch;
Adds operand to postfix expression.
else if (ch == '(')
Pushes opening bracket.
else if (ch == ')')
Pops until opening bracket.
while (prec(stack[top]) >= prec(ch))
Pops higher precedence operators.
postfix[j] = '\0';
Terminates postfix string.
PROGRAM 5: POSTFIX EVALUATION – LINE BY LINE
int s[20], top = -1;
Stack declaration.
if (isdigit(p[i]))
Checks operand.
push(p[i] - '0');
Converts char digit to integer.
int b = pop(), a = pop();
Pops operands.
push(a + b);
Performs operation and pushes result.
printf("Result = %d", pop());
Displays final result.
PROGRAM 5 (ALT): TOWER OF HANOI – LINE BY LINE
void tower(int n, char s, char t, char d)
Recursive function definition.
if (!n) return;
Base condition.
tower(n - 1, s, d, t);
Moves n-1 disks.
printf("Move disc %d from %c to %c", n, s, d);
Moves largest disk.
tower(n - 1, t, s, d);
Moves remaining disks.
PROGRAM 6: SINGLY LINKED LIST – LINE BY LINE
struct node
Defines node structure.
struct node *link;
Pointer to next node.
NODE start = NULL;
Initializes linked list as empty.
malloc(sizeof(struct node));
Allocates memory dynamically.
temp->link = start;
Links new node at front.
free(temp);
Frees deleted node memory.
PROGRAM 7: CIRCULAR QUEUE – LINE BY LINE
#define MAX 5
Defines queue size.
(front + 1) % MAX
Implements circular movement.
rear = (rear + 1) % MAX;
Updates rear circularly.
front = (front + 1) % MAX;
Updates front circularly.
PROGRAM 11: BFS & DFS – LINE BY LINE
int a[50][50];
Adjacency matrix.
visited[v] = 1;
Marks vertex visited.
q[++rear] = v;
Enqueues vertex.
dfs(i);
Recursive DFS call.
PROGRAM 12: HASHING – LINE BY LINE
index = key % m;
Hash function.
while (ht[index] != -1)
Collision detection.
index = (index + 1) % m;
Linear probing.
ht[index] = key;
Inserts key.