Q10
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
struct Node *top = NULL;
void push(int x) {
struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = x;
newNode->next = top;
top = newNode;
printf("%d pushed\n", x);
void pop() {
if (top == NULL)
printf("Stack Underflow\n");
else {
struct Node *temp = top;
printf("%d popped\n", top->data);
top = top->next;
free(temp);
}
}
void display() {
struct Node *temp = top;
if (temp == NULL)
printf("Stack is empty\n");
else {
printf("Stack: ");
while (temp) {
printf("%d ", temp->data);
temp = temp->next;
printf("\n");
int main() {
int ch, val;
do {
printf("\[Link] [Link] [Link] [Link]: ");
scanf("%d", &ch);
switch (ch) {
case 1: printf("Enter value: "); scanf("%d", &val); push(val); break;
case 2: pop(); break;
case 3: display(); break;
case 4: printf("Exiting...\n"); break;
default: printf("Invalid choice!\n");
}
} while (ch != 4);
return 0;
Q6
#include <stdio.h>
#define SIZE 5
Int stack[SIZE], top = -1;
Void push(int x) {
If (top == SIZE – 1)
Printf(“Stack Overflow\n”);
Else {
Stack[++top] = x;
Printf(“%d pushed\n”, x);
Void pop() {
If (top == -1)
Printf(“Stack Underflow\n”);
Else
Printf(“%d popped\n”, stack[top--]);
Void display() {
If (top == -1)
Printf(“Stack is empty\n”);
Else {
Printf(“Stack: “);
For (int I = top; I >= 0; i--)
Printf(“%d “, stack[i]);
Printf(“\n”);
Int main() {
Int ch, val;
Do {
Printf(“\[Link] [Link] [Link] [Link]: “);
Scanf(“%d”, &ch);
Switch (ch) {
Case 1: printf(“Enter value: “); scanf(“%d”, &val); push(val); break;
Case 2: pop(); break;
Case 3: display(); break;
Case 4: printf(“Exiting…\n”); break;
Default: printf(“Invalid choice!\n”);
} while (ch != 4);
Return 0;
Q11
#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
Struct Node *left, *right;
};
Struct Node* createNode(int value) {
Struct Node* newNode = malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
Struct Node* insert(struct Node* root, int value) {
If (root == NULL) return createNode(value);
If (value < root->data)
Root->left = insert(root->left, value);
Else
Root->right = insert(root->right, value);
Return root;
Void inorder(struct Node* root) {
If (root) {
Inorder(root->left);
Printf(“%d “, root->data);
Inorder(root->right);
Void preorder(struct Node* root) {
If (root) {
Printf(“%d “, root->data);
Preorder(root->left);
Preorder(root->right);
Void postorder(struct Node* root) {
If (root) {
Postorder(root->left);
Postorder(root->right);
Printf(“%d “, root->data);
Int main() {
Struct Node* root = NULL;
Int ch, val;
Do {
Printf(“\[Link] [Link] [Link] [Link] [Link]: “);
Scanf(“%d”, &ch);
Switch (ch) {
Case 1: printf(“Enter value: “); scanf(“%d”, &val); root = insert(root,
val); break;
Case 2: printf(“Inorder: “); inorder(root); printf(“\n”); break;
Case 3: printf(“Preorder: “); preorder(root); printf(“\n”); break;
Case 4: printf(“Postorder: “); postorder(root); printf(“\n”); break;
Case 5: printf(“Exiting…\n”); break;
Default: printf(“Invalid choice!\n”);
} while (ch != 5);
Return 0;
Q12
#include <stdio.h>
#define MAX 10
Int adj[MAX][MAX], visited[MAX], n;
Void bfs(int start) {
Int queue[MAX], front = 0, rear = 0;
Visited[start] = 1;
Queue[rear++] = start;
Printf(“BFS Traversal: “);
While (front < rear) {
Int node = queue[front++];
Printf(“%d “, node);
For (int I = 0; I < n; i++) {
If (adj[node][i] && !visited[i]) {
Queue[rear++] = I;
Visited[i] = 1;
Int main() {
Int edges, u, v, start;
Printf(“Enter number of vertices: “);
Scanf(“%d”, &n);
Printf(“Enter number of edges: “);
Scanf(“%d”, &edges);
For (int I = 0; I < edges; i++) {
Printf(“Enter edge (u v): “);
Scanf(“%d %d”, &u, &v);
Adj[u][v] = adj[v][u] = 1; // Undirected graph
Printf(“Enter starting vertex for BFS: “);
Scanf(“%d”, &start);
For (int I = 0; I < n; i++) visited[i] = 0;
Bfs(start);
Return 0;
Q8 9
#include <stdio.h>
#include <stdlib.h>
Struct Node {
Int data;
Struct Node *next;
};
Struct Node *head = NULL;
Void insertFront(int value) {
Struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = head;
head = newNode;
printf(“%d inserted at front\n”, value);
Void deleteEnd() {
If (head == NULL)
Printf(“List is empty\n”);
Else if (head->next == NULL) {
Printf(“%d deleted from end\n”, head->data);
Free(head);
Head = NULL;
} else {
Struct Node *temp = head;
While (temp->next->next != NULL)
Temp = temp->next;
Printf(“%d deleted from end\n”, temp->next->data);
Free(temp->next);
Temp->next = NULL;
Void display() {
If (head == NULL)
Printf(“List is empty\n”);
Else {
Struct Node *temp = head;
Printf(“List: “);
While (temp != NULL) {
Printf(“%d “, temp->data);
Temp = temp->next;
Printf(“\n”);
Int main() {
Int ch, val;
Do {
Printf(“\[Link] Front [Link] End [Link] [Link]: “);
Scanf(“%d”, &ch);
Switch (ch) {
Case 1: printf(“Enter value: “); scanf(“%d”, &val); insertFront(val);
break;
Case 2: deleteEnd(); break;
Case 3: display(); break;
Case 4: printf(“Exiting…\n”); break;
Default: printf(“Invalid choice!\n”);
} while (ch != 4);
Return 0;
Q7
#include <stdio.h>
#define SIZE 20
Int stack[SIZE], top = -1;
Void push(int x) {
If (top == SIZE – 1)
Printf(“Stack Overflow\n”);
Else
Stack[++top] = x;
}
Int pop() {
If (top == -1)
Return -1;
Else
Return stack[top--];
Int main() {
Int num, rem;
Printf(“Enter a decimal number: “);
Scanf(“%d”, &num);
While (num > 0) {
Rem = num % 2;
Push(rem);
Num = num / 2;
Printf(“Binary equivalent: “);
While (top != -1)
Printf(“%d”, pop());
Printf(“\n”);
Return 0;
Q5
#include <stdio.h>
#include <string.h>
#define MAX 50
Struct Student {
Int roll;
Char name[50];
Char dept[30];
Char subject[30];
Int marks;
};
Struct Student s[MAX];
Int count = 0;
Void addStudent() {
If (count >= MAX) {
Printf(“Database full!\n”);
Return;
Printf(“Enter Roll No: “);
Scanf(“%d”, &s[count].roll);
Printf(“Enter Name: “);
Scanf(“%s”, s[count].name);
Printf(“Enter Department: “);
Scanf(“%s”, s[count].dept);
Printf(“Enter Subject: “);
Scanf(“%s”, s[count].subject);
Printf(“Enter Marks: “);
Scanf(“%d”, &s[count].marks);
Count++;
Printf(“Student added successfully!\n”);
Void deleteStudent(int roll) {
Int found = 0;
For (int I = 0; I < count; i++) {
If (s[i].roll == roll) {
For (int j = I; j < count – 1; j++)
S[j] = s[j + 1];
Count--;
Found = 1;
Printf(“Student deleted successfully!\n”);
Break;
If (!found)
Printf(“Student not found!\n”);
Void searchStudent(int roll) {
For (int I = 0; I < count; i++) {
If (s[i].roll == roll) {
Printf(“\nStudent Found:\n”);
Printf(“Roll No: %d\nName: %s\nDept: %s\nSubject: %s\nMarks: %d\
n”,
S[i].roll, s[i].name, s[i].dept, s[i].subject, s[i].marks);
Return;
Printf(“Student not found!\n”);
Void displayAll() {
If (count == 0) {
Printf(“No records to display.\n”);
Return;
Printf(“\n--- Student Records ---\n”);
For (int I = 0; I < count; i++) {
Printf(“%d) Roll: %d | Name: %s | Dept: %s | Subject: %s | Marks: %d\n”,
I + 1, s[i].roll, s[i].name, s[i].dept, s[i].subject, s[i].marks);
Int main() {
Int ch, roll;
Do {
Printf(“\[Link] [Link] [Link] [Link] [Link]: “);
Scanf(“%d”, &ch);
Switch (ch) {
Case 1: addStudent(); break;
Case 2: printf(“Enter Roll No to delete: “); scanf(“%d”, &roll);
deleteStudent(roll); break;
Case 3: printf(“Enter Roll No to search: “); scanf(“%d”, &roll);
searchStudent(roll); break;
Case 4: displayAll(); break;
Case 5: printf(“Exiting…\n”); break;
Default: printf(“Invalid choice!\n”);
} while (ch != 5);
Return 0;
Q4
#include <stdio.h>
#define MAX 20
Void bubbleSort(float a[], int n) {
For (int I = 0; I < n – 1; i++)
For (int j = 0; j < n – I – 1; j++)
If (a[j] > a[j + 1]) {
Float temp = a[j];
A[j] = a[j + 1];
A[j + 1] = temp;
Printf(“Prices sorted using Bubble Sort:\n”);
For (int I = 0; I < n; i++)
Printf(“%.2f “, a[i]);
Printf(“\n”);
Void selectionSort(float a[], int n) {
For (int I = 0; I < n – 1; i++) {
Int min = I;
For (int j = I + 1; j < n; j++)
If (a[j] < a[min])
Min = j;
Float temp = a[i];
A[i] = a[min];
A[min] = temp;
Printf(“Prices sorted using Selection Sort:\n”);
For (int I = 0; I < n; i++)
Printf(“%.2f “, a[i]);
Printf(“\n”);
Int main() {
Float price[MAX];
Int n, ch;
Printf(“Enter number of products: “);
Scanf(“%d”, &n);
Printf(“Enter product prices:\n”);
For (int I = 0; I < n; i++)
Scanf(“%f”, &price[i]);
Do {
Printf(“\[Link] Sort [Link] Sort [Link]: “);
Scanf(“%d”, &ch);
Switch (ch) {
Case 1: bubbleSort(price, n); break;
Case 2: selectionSort(price, n); break;
Case 3: printf(“Exiting…\n”); break;
Default: printf(“Invalid choice!\n”);
} while (ch != 3);
Return 0;
Q3
#include <stdio.h>
#include <string.h>
#define MAX 50
Int binarySearch(char names[][50], int n, char key[]) {
Int low = 0, high = n – 1, mid;
While (low <= high) {
Mid = (low + high) / 2;
Int res = strcmp(names[mid], key);
If (res == 0)
Return mid;
Else if (res < 0)
Low = mid + 1;
Else
High = mid – 1;
Return -1;
Void bubbleSort(char names[][50], int n) {
Char temp[50];
For (int I = 0; I < n – 1; i++)
For (int j = 0; j < n – I – 1; j++)
If (strcmp(names[j], names[j + 1]) > 0) {
Strcpy(temp, names[j]);
Strcpy(names[j], names[j + 1]);
Strcpy(names[j + 1], temp);
Int main() {
Int n;
Char names[MAX][50], key[50];
Printf(“Enter number of contacts: “);
Scanf(“%d”, &n);
Printf(“Enter contact names:\n”);
For (int I = 0; I < n; i++)
Scanf(“%s”, names[i]);
bubbleSort(names, n);
printf(“\nSorted contact list:\n”);
for (int I = 0; I < n; i++)
printf(“%s\n”, names[i]);
printf(“\nEnter name to search: “);
scanf(“%s”, key);
int pos = binarySearch(names, n, key);
if (pos != -1)
printf(“Contact ‘%s’ found at position %d\n”, key, pos + 1);
else
printf(“Contact ‘%s’ not found!\n”, key);
return 0;
Q2
#include <stdio.h>
Void starPyramid(int n) {
For (int I = 1; I <= n; i++) {
For (int j = I; j < n; j++)
Printf(“ “);
For (int k = 1; k <= (2 * I – 1); k++)
Printf(“*”);
Printf(“\n”);
Void alphabetPyramid(int n) {
Char ch = ‘A’;
For (int I = 1; I <= n; i++) {
For (int j = I; j < n; j++)
Printf(“ “);
For (int k = 1; k <= (2 * I – 1); k++)
Printf(“%c”, ch);
Ch++;
Printf(“\n”);
Int main() {
Int n;
Printf(“Enter number of rows: “);
Scanf(“%d”, &n);
Printf(“\n(a) Pyramid with Asterisks:\n”);
starPyramid(n);
printf(“\n(b) Pyramid with Alphabets:\n”);
alphabetPyramid(n);
return 0;
Q1
#include <stdio.h>
Void rightAngleTriangle(int n) {
For (int I = 1; I <= n; i++) {
For (int j = 1; j <= I; j++)
Printf(“%d “, j);
Printf(“\n”);
Void diamondNumber(int n) {
// Upper half
For (int I = 1; I <= n; i++) {
For (int j = I; j < n; j++)
Printf(“ “);
For (int k = 1; k <= (2 * I – 1); k++)
Printf(“%d”, k);
Printf(“\n”);
}
// Lower half
For (int I = n – 1; I >= 1; i--) {
For (int j = n; j > I; j--)
Printf(“ “);
For (int k = 1; k <= (2 * I – 1); k++)
Printf(“%d”, k);
Printf(“\n”);
Int main() {
Int n;
Printf(“Enter number of rows: “);
Scanf(“%d”, &n);
Printf(“\n(a) Right-Angle Triangle with Numbers:\n”);
rightAngleTriangle(n);
printf(“\n(b) Diamond Shape with Numbers:\n”);
diamondNumber(n);
return 0;