From d36df77bb16d0094c367492fb67acf77f61c4fdc Mon Sep 17 00:00:00 2001 From: Manoj HV Date: Mon, 8 Dec 2025 23:41:37 +0530 Subject: [PATCH] rest of the codes --- BST_Traversal.c | 92 ++++++++++++++++++++++++++++++++ CircQueue.c | 124 +++++++++++++++++++++++++++++++++++++++++++ DoublyLL.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++ Graph.c | 115 ++++++++++++++++++++++++++++++++++++++++ InfixToPosfix.c | 92 ++++++++++++++++++++++++++++++++ MaxHeap.c | 123 +++++++++++++++++++++++++++++++++++++++++++ MergeSort.c | 60 +++++++++++++++++++++ PostfixEval.c | 76 +++++++++++++++++++++++++++ QueueInLL.c | 128 +++++++++++++++++++++++++++++++++++++++++++++ StackInLL.c | 101 +++++++++++++++++++++++++++++++++++ transpose.c | 112 +++++++++++++++++++++++++++++++++++++++ 11 files changed, 1159 insertions(+) create mode 100644 BST_Traversal.c create mode 100644 CircQueue.c create mode 100644 DoublyLL.c create mode 100644 Graph.c create mode 100644 InfixToPosfix.c create mode 100644 MaxHeap.c create mode 100644 MergeSort.c create mode 100644 PostfixEval.c create mode 100644 QueueInLL.c create mode 100644 StackInLL.c create mode 100644 transpose.c diff --git a/BST_Traversal.c b/BST_Traversal.c new file mode 100644 index 0000000..80466a6 --- /dev/null +++ b/BST_Traversal.c @@ -0,0 +1,92 @@ +#include +#include + +// Node structure for BST +typedef struct node { + int data; + struct node *left; + struct node *right; +} NODE; + +// Function to create a new node +NODE* createNode(int value) { + NODE *newNode = (NODE*)malloc(sizeof(NODE)); + if (!newNode) { + printf("Memory allocation failed\n"); + exit(1); + } + newNode->data = value; + newNode->left = newNode->right = NULL; + return newNode; +} + +// Insert a value into BST +NODE* insertBST(NODE *root, int value) { + if (root == NULL) { + root = createNode(value); + } else if (value < root->data) { + root->left = insertBST(root->left, value); + } else if (value > root->data) { + root->right = insertBST(root->right, value); + } else { + // duplicate values are ignored (you can change this if needed) + printf("Duplicate value %d ignored.\n", value); + } + return root; +} + +// In-order traversal (Left, Root, Right) +void inorder(NODE *root) { + if (root != NULL) { + inorder(root->left); + printf("%d ", root->data); + inorder(root->right); + } +} + +// Pre-order traversal (Root, Left, Right) +void preorder(NODE *root) { + if (root != NULL) { + printf("%d ", root->data); + preorder(root->left); + preorder(root->right); + } +} + +// Post-order traversal (Left, Right, Root) +void postorder(NODE *root) { + if (root != NULL) { + postorder(root->left); + postorder(root->right); + printf("%d ", root->data); + } +} + +int main() { + NODE *root = NULL; + int n, i, value; + + printf("=== BINARY SEARCH TREE CONSTRUCTION & TRAVERSAL ===\n"); + + printf("Enter number of nodes to insert: "); + scanf("%d", &n); + + printf("Enter %d values:\n", n); + for (i = 0; i < n; i++) { + scanf("%d", &value); + root = insertBST(root, value); + } + + printf("\nIn-order Traversal : "); + inorder(root); + + printf("\nPre-order Traversal : "); + preorder(root); + + printf("\nPost-order Traversal : "); + postorder(root); + + printf("\n"); + + return 0; +} diff --git a/CircQueue.c b/CircQueue.c new file mode 100644 index 0000000..94080a9 --- /dev/null +++ b/CircQueue.c @@ -0,0 +1,124 @@ +#include +#include + +#define MALLOC(x,size,type) (x = (type*)malloc((size) * sizeof(type))) + +typedef struct +{ + int n; +} element; + +int front = 0, rear = 0, capacity; +element *queue; + +void copy(element* start, element* end, element* newQueue) +{ + element* j; + element* i; + i = newQueue; + j = start; + for (; j < end; j++, i++) + { + *i = *j; + } +} + +void queueFull() +{ + element* newQueue; + MALLOC(newQueue, capacity * 2, element); + int start = (front + 1) % capacity; + + if (start < 2) // either 1 or 0, 1 when front at 0, 0 when front at capacity - 1 + copy(queue + start, queue + start + capacity - 1, newQueue); + else + { + copy(queue + start, queue + capacity, newQueue); + copy(queue, queue + rear + 1, newQueue + capacity - start); + } + + front = 2 * capacity - 1; + rear = capacity - 1; + capacity *= 2; + free(queue); + queue = newQueue; +} + +void addq(element item) +{ + rear = (rear + 1) % capacity; + if (front == rear) + queueFull(); + queue[rear] = item; +} + +element deleteq() +{ + element item; + if (front == rear) + { + item.n = -1; + return item; + } + front = (front + 1) % capacity; + return queue[front]; +} + +void displayq() +{ + int i; + if (front == rear) + { + printf("Queue Empty\n"); + return; + } + for (i = (front + 1) % capacity; i != rear; i = (i + 1) % capacity) + printf("%d\t", queue[i].n); + printf("%d\n", queue[i].n); + // printf("Front: %d Rear: %d\n", front, rear); +} + +int main() +{ + int choice; + element item; + + printf("Enter initial size: "); + scanf("%d", &capacity); + MALLOC(queue, capacity, element); + + while (1) + { + printf("\n1. Add\n2. Delete\n3. Display\n4. Exit\n"); + scanf("%d", &choice); + + switch (choice) + { + case 1: + printf("Enter item to add: "); + scanf("%d", &item.n); + addq(item); + break; + + case 2: + item = deleteq(); + if (item.n == -1) + printf("Queue Empty\n"); + else + printf("Item deleted: %d\n", item.n); + break; + + case 3: + displayq(); + break; + + case 4: + exit(0); + + default: + printf("Invalid choice\n"); + } + } + + return 0; +} diff --git a/DoublyLL.c b/DoublyLL.c new file mode 100644 index 0000000..db4d758 --- /dev/null +++ b/DoublyLL.c @@ -0,0 +1,136 @@ +#include +#include +typedef struct element { + int key; +} element; + +typedef struct node *nodeptr; + +typedef struct node { + nodeptr llink; + element item; + nodeptr rlink; +} node; + +void insert(nodeptr head, int data); +void delete(nodeptr head, nodeptr deleted); +void display(nodeptr head); +nodeptr search(nodeptr head, int key); + +int main() { + int choice, data; + nodeptr node_to_delete; + + nodeptr head = (nodeptr)malloc(sizeof(node)); + if (head == NULL) { + fprintf(stderr, "Memory allocation failed for head node.\n"); + exit(1); + } + head->llink = head; + head->rlink = head; + + while (1) { + printf("\n--- Doubly Linked List Menu ---\n"); + printf("1. Insert (at front)\n"); + printf("2. Delete (by key)\n"); + printf("3. Display List\n"); + printf("4. Exit\n"); + printf("Enter your choice: "); + + scanf("%d", &choice); + + switch (choice) { + case 1: + printf("Enter number to insert: "); + scanf("%d", &data); + insert(head, data); + printf("Item %d inserted.\n", data); + break; + + case 2: + printf("Enter number to delete: "); + scanf("%d", &data); + + node_to_delete = search(head, data); + + if (node_to_delete == NULL) { + printf("Item %d not found in the list.\n", data); + } else { + + delete(head, node_to_delete); + printf("Item %d deleted.\n", data); + } + break; + + case 3: + display(head); + break; + + case 4: + printf("Exiting program.\n"); + free(head); + exit(0); + + default: + printf("Invalid choice. Please try again.\n"); + } + } + + return 0; +} + + +void insert(nodeptr head, int data) { + nodeptr newnode = (nodeptr)malloc(sizeof(node)); + if (newnode == NULL) { + fprintf(stderr, "Memory allocation failed for new node!\n"); + exit(1); + } + newnode->item.key = data; + newnode->llink = head; + newnode->rlink = head->rlink; + head->rlink->llink = newnode; + head->rlink = newnode; +} + + +void delete(nodeptr head, nodeptr deleted) { + if (head == deleted) { + printf("Deletion of the head node not permitted!\n"); + } else { + deleted->llink->rlink = deleted->rlink; + deleted->rlink->llink = deleted->llink; + free(deleted); + } +} + + +nodeptr search(nodeptr head, int key) { + nodeptr temp = head->rlink; + + + while (temp != head) { + if (temp->item.key == key) { + return temp; + } + temp = temp->rlink; + } + return NULL; +} + + +void display(nodeptr head) { + nodeptr temp = head->rlink; + + if (temp == head) { + printf("List is empty.\n"); + return; + } + + printf("List: "); + while (temp != head) { + printf("%d ", temp->item.key); + temp = temp->rlink; + } + printf("\n"); +} diff --git a/Graph.c b/Graph.c new file mode 100644 index 0000000..fee6261 --- /dev/null +++ b/Graph.c @@ -0,0 +1,115 @@ +#include +#include + +#define MAX 20 + +// Node for adjacency list +typedef struct node { + int vertex; + struct node *next; +} NODE; + +NODE *G[MAX]; // adjacency list heads +int visited[MAX]; // for DFS, BFS +int n; // number of vertices + +// Create new node +NODE* createNode(int v) { + NODE *newNode = (NODE*)malloc(sizeof(NODE)); + newNode->vertex = v; + newNode->next = NULL; + return newNode; +} + +// Insert edge vi --> vj +void insertEdge(int vi, int vj) { + NODE *temp = createNode(vj); + + if (G[vi] == NULL) { + G[vi] = temp; + } else { + NODE *ptr = G[vi]; + while (ptr->next != NULL) + ptr = ptr->next; + ptr->next = temp; + } +} + +// DFS recursive +void DFS(int v) { + visited[v] = 1; + printf("%d ", v); + + NODE *ptr = G[v]; + while (ptr != NULL) { + if (!visited[ptr->vertex]) + DFS(ptr->vertex); + ptr = ptr->next; + } +} + +// BFS iterative +void BFS(int start) { + int queue[MAX]; + int front = 0, rear = -1; + + for (int i = 0; i < n; i++) + visited[i] = 0; + + visited[start] = 1; + queue[++rear] = start; + + printf("BFS Traversal: "); + + while (front <= rear) { + int v = queue[front++]; + printf("%d ", v); + + NODE *ptr = G[v]; + while (ptr != NULL) { + if (!visited[ptr->vertex]) { + visited[ptr->vertex] = 1; + queue[++rear] = ptr->vertex; + } + ptr = ptr->next; + } + } + printf("\n"); +} + +// MAIN PROGRAM — CONSTRUCTION + DFS + BFS +int main() { + int edges, u, v, start; + + printf("Enter number of vertices: "); + scanf("%d", &n); + + for (int i = 0; i < n; i++) + G[i] = NULL; + + printf("Enter number of edges: "); + scanf("%d", &edges); + + printf("Enter edges (u v) as 0-based vertices:\n"); + for (int i = 0; i < edges; i++) { + scanf("%d %d", &u, &v); + insertEdge(u, v); + insertEdge(v, u); // undirected graph + } + + printf("\nEnter starting node for traversal: "); + scanf("%d", &start); + + // DFS + for (int i = 0; i < n; i++) + visited[i] = 0; + + printf("DFS Traversal: "); + DFS(start); + printf("\n"); + + // BFS + BFS(start); + + return 0; +} diff --git a/InfixToPosfix.c b/InfixToPosfix.c new file mode 100644 index 0000000..7d5a406 --- /dev/null +++ b/InfixToPosfix.c @@ -0,0 +1,92 @@ +#include +#define MAX 20 + +typedef enum { lparen, rparen, plus, minus, times, divide, mod, eos, operand } precedence; + +precedence stack[30]; +int top = -1; +char EXPR[MAX]; + +int isp[] = { 0, 19, 12, 12, 13, 13, 13, 0 }; // in-stack precedence +int icp[] = { 20, 19, 12, 12, 13, 13, 13, 0 }; // incoming precedence + +void push(precedence token) +{ + stack[++top] = token; +} + +precedence pop() +{ + return stack[top--]; +} + +precedence get_token(char *symbol, int *n) +{ + *symbol = EXPR[(*n)++]; + switch (*symbol) + { + case '(': return lparen; + case ')': return rparen; + case '+': return plus; + case '-': return minus; + case '*': return times; + case '/': return divide; + case '%': return mod; + case '\0': return eos; + default: return operand; + } +} + +void print_token(precedence token) +{ + switch (token) + { + case plus: printf("+"); break; + case minus: printf("-"); break; + case times: printf("*"); break; + case divide: printf("/"); break; + case mod: printf("%%"); break; + default: break; + } +} + +void postfix() +{ + char symbol; + precedence token; + int n = 0; + + top = 0; + stack[0] = eos; + + for (token = get_token(&symbol, &n); token != eos; token = get_token(&symbol, &n)) + { + if (token == operand) + printf("%c", symbol); + else if (token == rparen) + { + while (stack[top] != lparen) + print_token(pop()); + pop(); // pop '(' + } + else + { + while (isp[stack[top]] >= icp[token]) + print_token(pop()); + push(token); + } + } + + while ((token = pop()) != eos) + print_token(token); + + printf("\n"); +} + +int main() +{ + printf("\nEnter the infix expression\n"); + scanf("%s", EXPR); + postfix(); + return 0; +} diff --git a/MaxHeap.c b/MaxHeap.c new file mode 100644 index 0000000..4042829 --- /dev/null +++ b/MaxHeap.c @@ -0,0 +1,123 @@ +#include +#include + +#define MAX 50 + +int heap[MAX]; +int heapSize = 0; + +// Function to insert an item into max heap +void insert(int item) { + int i; + + if (heapSize == MAX - 1) { + printf("Heap is full. Cannot insert.\n"); + return; + } + + // First insert at last position + heapSize++; + i = heapSize; + + // Percolate up + while (i > 1 && item > heap[i / 2]) { + heap[i] = heap[i / 2]; // move parent down + i = i / 2; // move up + } + + heap[i] = item; + printf("Inserted %d into heap.\n", item); +} + +// Function to delete max (root) from heap +int deleteMax() { + int i, child; + int item, last; + + if (heapSize == 0) { + printf("Heap is empty. Cannot delete.\n"); + return -1; + } + + item = heap[1]; // max element + last = heap[heapSize]; // last element + heapSize--; + + i = 1; + + // Percolate down + while (2 * i <= heapSize) { + // left child + child = 2 * i; + + // choose larger child if right child exists + if (child < heapSize && heap[child + 1] > heap[child]) + child++; + + if (last >= heap[child]) + break; + + heap[i] = heap[child]; // move child up + i = child; + } + + heap[i] = last; + + return item; +} + +// Display heap contents (array form) +void display() { + int i; + + if (heapSize == 0) { + printf("Heap is empty.\n"); + return; + } + + printf("Heap contents (array form):\n"); + for (i = 1; i <= heapSize; i++) + printf("%d ", heap[i]); + printf("\n"); +} + +int main() { + int choice, item, deleted; + + while (1) { + printf("\n=== MAX HEAP (PRIORITY QUEUE) MENU ===\n"); + printf("1. Insert\n"); + printf("2. Delete Max\n"); + printf("3. Display Heap\n"); + printf("4. Exit\n"); + printf("Enter choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + printf("Enter item to insert: "); + scanf("%d", &item); + insert(item); + break; + + case 2: + deleted = deleteMax(); + if (deleted != -1) + printf("Deleted item (max): %d\n", deleted); + break; + + case 3: + display(); + break; + + case 4: + printf("Exiting...\n"); + exit(0); + + default: + printf("Invalid choice. Try again.\n"); + } + } + + return 0; +} diff --git a/MergeSort.c b/MergeSort.c new file mode 100644 index 0000000..ba71f77 --- /dev/null +++ b/MergeSort.c @@ -0,0 +1,60 @@ +#include + +// Function to merge two halves +void merge(int a[], int left, int mid, int right) { + int i = left, j = mid + 1, k = 0; + int temp[right - left + 1]; + + // Merge elements into temp[] + while (i <= mid && j <= right) { + if (a[i] <= a[j]) + temp[k++] = a[i++]; + else + temp[k++] = a[j++]; + } + + // Copy remaining elements from left half + while (i <= mid) + temp[k++] = a[i++]; + + // Copy remaining elements from right half + while (j <= right) + temp[k++] = a[j++]; + + // Copy temp[] back to original array + for (i = left, k = 0; i <= right; i++, k++) + a[i] = temp[k]; +} + +// Recursive merge sort +void mergeSort(int a[], int left, int right) { + if (left < right) { + int mid = (left + right) / 2; + + mergeSort(a, left, mid); + mergeSort(a, mid + 1, right); + + merge(a, left, mid, right); + } +} + +int main() { + int n, i; + + printf("Enter number of elements: "); + scanf("%d", &n); + + int a[n]; + printf("Enter %d integers:\n", n); + for (i = 0; i < n; i++) + scanf("%d", &a[i]); + + mergeSort(a, 0, n - 1); + + printf("Sorted list (Ascending Order):\n"); + for (i = 0; i < n; i++) + printf("%d ", a[i]); + + printf("\n"); + return 0; +} diff --git a/PostfixEval.c b/PostfixEval.c new file mode 100644 index 0000000..9f50a23 --- /dev/null +++ b/PostfixEval.c @@ -0,0 +1,76 @@ +#include +#define MAX 40 + +typedef enum { lparen, rparen, plus, minus, times, divide, mod, eos, operand } precedence; + +char EXPR[MAX]; +int stack[20]; +int top = -1; + +precedence get_token(char *symbol, int *n) +{ + *symbol = EXPR[(*n)++]; + switch (*symbol) + { + case '(': return lparen; + case ')': return rparen; + case '+': return plus; + case '-': return minus; + case '*': return times; + case '/': return divide; + case '%': return mod; + case '\0': return eos; + default: return operand; + } +} + +void push(int num) +{ + stack[++top] = num; +} + +int pop() +{ + return stack[top--]; +} + +int eval() +{ + precedence token; + char symbol; + int op1, op2, n = 0; + + token = get_token(&symbol, &n); + + while (token != eos) + { + if (token == operand) + push(symbol - '0'); // assumes single-digit operands + else + { + op2 = pop(); + op1 = pop(); + switch (token) + { + case plus: push(op1 + op2); break; + case minus: push(op1 - op2); break; + case times: push(op1 * op2); break; + case divide: push(op1 / op2); break; + case mod: push(op1 % op2); break; + default: break; + } + } + token = get_token(&symbol, &n); + } + return pop(); +} + +int main() +{ + int res; + printf("\nEnter the postfix expression\n"); + scanf("%s", EXPR); + res = eval(); + printf("\nAfter evaluation:\t%d\n", res); + return 0; +} diff --git a/QueueInLL.c b/QueueInLL.c new file mode 100644 index 0000000..7a81672 --- /dev/null +++ b/QueueInLL.c @@ -0,0 +1,128 @@ +#include +#include + + +typedef int element; +typedef struct queue *queueptr; + + +typedef struct queue { + element item; + queueptr link; +} queue; + + +queueptr front = NULL; +queueptr rear = NULL; + +int ISEMPTY(); +void addq(element item); +element deleteq(); +void display(); + + +int main() { + int choice, item; + + while (1) { + printf("\n--- Single Queue Menu ---\n"); + printf("1. Add to Queue\n"); + printf("2. Delete from Queue\n"); + printf("3. Display Queue\n"); + printf("4. Exit\n"); + printf("Enter your choice: "); + + scanf("%d", &choice); + + + switch (choice) { + case 1: + printf("Enter item to add: "); + scanf("%d", &item); + addq(item); + printf("Item %d added to queue.\n", item); + break; + + case 2: + + item = deleteq(); + printf("Deleted item %d from queue.\n", item); + break; + + case 3: + display(); + break; + + case 4: + printf("Exiting program.\n"); + exit(0); + + default: + printf("Invalid choice. Please try again.\n"); + } + } + + return 0; +} + +int ISEMPTY() { + return (front == NULL); +} + + +void addq(element item) { + + queueptr temp = (queueptr)malloc(sizeof(queue)); + + + if (temp == NULL) { + fprintf(stderr, "Memory allocation failed. The queue is full!\n"); + exit(1); + } + + temp->item = item; + temp->link = NULL; + + if (ISEMPTY()) { + front = temp; + } else { + rear->link = temp; + } + rear = temp; +} + + +element deleteq() { + queueptr temp = front; + element item; + + if (ISEMPTY()) { + fprintf(stderr, "The queue is empty, cannot delete.\n"); + exit(1); + } + + item = temp->item; + front = temp->link; + free(temp); + if (ISEMPTY()) { + rear = NULL; + } + return item; +} + + +void display() { + queueptr temp = front; + + if (ISEMPTY()) { + printf("Queue is empty.\n"); + return; + } + + printf("Queue contents (front to rear): "); + while (temp != NULL) { + printf("%d ", temp->item); + temp = temp->link; + } + printf("\n"); +} diff --git a/StackInLL.c b/StackInLL.c new file mode 100644 index 0000000..fb62f7c --- /dev/null +++ b/StackInLL.c @@ -0,0 +1,101 @@ +#include +#include + +// Node structure +typedef struct node { + int data; + struct node *next; +} NODE; + +NODE *top = NULL; + +// Push operation +void push(int value) { + NODE *newNode = (NODE*)malloc(sizeof(NODE)); + + if (!newNode) { + printf("Memory allocation failed!\n"); + return; + } + + newNode->data = value; + newNode->next = top; + top = newNode; + + printf("Pushed %d onto stack\n", value); +} + +// Pop operation +int pop() { + NODE *temp; + int value; + + if (top == NULL) { + printf("Stack Underflow! Stack is empty.\n"); + return -1; + } + + temp = top; + value = temp->data; + top = top->next; + free(temp); + + return value; +} + +// Display stack contents +void display() { + NODE *temp = top; + + if (top == NULL) { + printf("Stack is empty.\n"); + return; + } + + printf("Stack contents:\n"); + while (temp != NULL) { + printf("%d -> ", temp->data); + temp = temp->next; + } + printf("NULL\n"); +} + +// Main menu +int main() { + int choice, value; + + while (1) { + printf("\n--- STACK MENU ---\n"); + printf("1. Push\n"); + printf("2. Pop\n"); + printf("3. Display\n"); + printf("4. Exit\n"); + printf("Enter choice: "); + scanf("%d", &choice); + + switch (choice) { + case 1: + printf("Enter value to push: "); + scanf("%d", &value); + push(value); + break; + + case 2: + value = pop(); + if (value != -1) + printf("Popped value: %d\n", value); + break; + + case 3: + display(); + break; + + case 4: + printf("Exiting...\n"); + exit(0); + + default: + printf("Invalid choice. Try again.\n"); + } + } +} diff --git a/transpose.c b/transpose.c new file mode 100644 index 0000000..0fa323c --- /dev/null +++ b/transpose.c @@ -0,0 +1,112 @@ +#include + +typedef struct +{ + int r, c, v; +} term; + +/* Simple Transpose Method */ +void simpleTranspose(term a[], term t[]) +{ + int i, j, k = 1; + int numrows = a[0].r; + int numcols = a[0].c; + int numterms = a[0].v; + + t[0].r = numcols; + t[0].c = numrows; + t[0].v = numterms; + + if (numterms > 0) + { + for (i = 0; i < numcols; i++) + { + for (j = 1; j <= numterms; j++) + { + if (a[j].c == i) + { + t[k].r = a[j].c; + t[k].c = a[j].r; + t[k].v = a[j].v; + k++; + } + } + } + } + + printf("\n=== SIMPLE TRANSPOSE RESULT ===\n"); + printf("Row\tCol\tVal\n"); + for (i = 1; i <= t[0].v; i++) + printf("%d\t%d\t%d\n", t[i].r, t[i].c, t[i].v); +} + +/* Fast Transpose Method */ +void fastTranspose(term a[], term t[]) +{ + int rt[20], sp[20]; + int i, j; + int numcols = a[0].c; + int numterms = a[0].v; + + t[0].r = numcols; + t[0].c = a[0].r; + t[0].v = numterms; + + if (numterms > 0) + { + for (i = 0; i < numcols; i++) + rt[i] = 0; + + for (i = 1; i <= numterms; i++) + rt[a[i].c]++; + + sp[0] = 1; + for (i = 1; i < numcols; i++) + sp[i] = sp[i - 1] + rt[i - 1]; + + for (i = 1; i <= numterms; i++) + { + j = sp[a[i].c]++; + t[j].r = a[i].c; + t[j].c = a[i].r; + t[j].v = a[i].v; + } + } + + printf("\n=== FAST TRANSPOSE RESULT ===\n"); + printf("Row\tCol\tVal\n"); + for (i = 1; i <= t[0].v; i++) + printf("%d\t%d\t%d\n", t[i].r, t[i].c, t[i].v); +} + +int main() +{ + term a[20], ts[20], tf[20]; + int i; + + printf("=== SPARSE MATRIX TRANSPOSE ===\n"); + + printf("\nEnter number of rows and columns: "); + scanf("%d%d", &a[0].r, &a[0].c); + + printf("Enter number of non-zero values: "); + scanf("%d", &a[0].v); + + for (i = 1; i <= a[0].v; i++) + { + printf("Enter %dth (row col value): ", i); + scanf("%d%d%d", &a[i].r, &a[i].c, &a[i].v); + } + + printf("\n=== ORIGINAL SPARSE MATRIX ===\n"); + printf("Row\tCol\tVal\n"); + for (i = 1; i <= a[0].v; i++) + printf("%d\t%d\t%d\n", a[i].r, a[i].c, a[i].v); + + /* Simple method output */ + simpleTranspose(a, ts); + + /* Fast method output */ + fastTranspose(a, tf); + return 0; +}