Chirag Saraswat 289 (DSA Lab)
Chirag Saraswat 289 (DSA Lab)
#include <stdio.h>
int main()
{
int a[4][4], b[4][4], c[4][4], d[4][4], i, j;
// clrscr();
printf("Enter element of first matrix :\n"); for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("Enter matrix element :");
scanf("%d", &a[i][j]);
}
}
printf("\nEnter the elementof second matrix :\n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("Enter matrix element :");
scanf("%d", &b[i][j]);
}
}
// Addition of matrix : /
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
c[i][j] = a[i][j] + b[i][j];
}
printf("\naddition of 1 and 2 matrix : \n\n"); for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
printf("%d\t", c[i][j]);
printf("\n");
}
}
Output:-
Size of word:-
#include <stdio.h>
int main()
{
char str[20];
int i, count = 0;
// clrser();
Question -2: Simulate a stack, queue, circular queue and dequeue using a one-
dimensional array as a storage element. The program should implement the
basic addition, deletion and traversal operations.
Solution:-
Stack:-
#include <stdio.h> #include <stdlib.h>
#include<cstring>
#include<cstdlib>
#include <limits.h> // For INT_MIN
int main()
{
int choice, data;
while (1)
{
/* Menu */
printf(" \n"); printf(" STACK IMPLEMENTATION PROGRAM \n");
printf(" \n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Size\n");
printf("4. Exit\n");
printf(" \n"); printf("Enter your choice: ");
scanf("%d", &choice); switch (choice)
{
case 1:
printf("Enter data to push into stack: ");
scanf("%d", &data);
// Push element to stack
push(data); break;
case 2:
data = pop();
case 3:
printf("Stack size: %d\n", top + 1); break;
case 4:
printf("Exiting from app.\n");
exit(0);
break;
default:
printf("Invalid choice, please try again.\n");
}
printf("\n\n");
}
return 0;
}
/**
* Functiont to push a new element in stack.
*/
void push(int element)
{
// Check stack overflow
if (top >= SIZE)
{
printf("Stack Overflow, can't add more element element to stack.\n");
return;
}
/**
* Function to pop element from top of stack.
*/
int pop()
{
// Check stack underflow
if (top < 0)
{
printf("Stack is empty.\n");
Output:-
Queue:-
#include <stdio.h> #include <stdlib.h> #define MAX 50
void delete()
{
if (front == -1 || front > rear)
{
printf("Queue Underflow \n"); return;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */
void display()
{
int i;
if (front == -1)
printf("Queue is empty \n"); else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
Output:-
Circular Queue:-
#include <stdio.h> #define MAX 5
int cqueue_arr[MAX]; int front = -1;
int rear = -1;
void insert(int item)
{
if ((front == 0 && rear == MAX - 1) || (front == rear + 1))
{
printf("Queue Overflow n"); return;
}
if (front == -1)
{
front = 0;
rear = 0;
}
else
{
if (rear == MAX - 1) rear = 0;
else
rear = rear + 1;
}
cqueue_arr[rear] = item;
}
void deletion()
{
if (front == -1)
{
printf("Queue Underflown"); return;
}
printf("Element deleted from queue is : %dn", cqueue_arr[front]); if (front
== rear)
{
front = -1;
rear = -1;
}
else
{
if (front == MAX - 1) front = 0;
else
front = front + 1;
}
}
void display()
{
int front_pos = front, rear_pos = rear; if (front == -1)
{
printf("Queue is emptyn"); return;
}
printf("Queue elements :n"); if (front_pos <= rear_pos)
while (front_pos <= rear_pos)
{
}
else
{
Dequeue:-
#include <stdio.h> #define MAX 5
int deque_arr[MAX]; int left = -1;
int right = -1; void insert_right()
{
int added_item;
if ((left == 0 && right == MAX - 1) || (left == right + 1))
{
printf("Queue Overflow\n"); return;
}
if (left == -1) /* if queue is initially empty */
{
left = 0;
right = 0;
}
else if (right == MAX - 1) /*right is at last position of queue */
right = 0; else
right = right + 1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item); deque_arr[right] = added_item;
}
void insert_left()
{
int added_item;
if ((left == 0 && right == MAX - 1) || (left == right + 1))
{
printf("Queue Overflow \n"); return;
}
if (left == -1) /*If queue is initially empty*/
{
left = 0;
right = 0;
}
else if (left == 0) left = MAX - 1;
else
left = left - 1;
printf("Input the element for adding in queue : ");
scanf("%d", &added_item); deque_arr[left] = added_item;
}
void delete_left()
{
if (left == -1)
{
printf("Queue Underflow\n"); return;
}
printf("Element deleted from queue is : %d\n", deque_arr[left]); if (left ==
right) /*Queue has only one element */
{
left = -1;
right = -1;
}
else if (left == MAX - 1) left = 0;
else
left = left + 1;
}
void delete_right()
{
if (left == -1)
{
printf("Queue Underflow\n"); return;
}
printf("Element deleted from queue is : %d\n", deque_arr[right]); if (left ==
right) /*queue has only one element*/
{
left = -1;
right = -1;
}
else if (right == 0) right = MAX - 1;
else
right = right - 1;
}
void display_queue()
{
int front_pos = left, rear_pos = right; if (left == -1)
{
printf("Queue is empty\n"); return;
}
printf("Queue elements :\n"); if (front_pos <= rear_pos)
{
while (front_pos <= rear_pos)
{
printf("%d ", deque_arr[front_pos]); front_pos++;
}
}
else
{
while (front_pos <= MAX - 1)
{
printf("%d ", deque_arr[front_pos]); front_pos++;
}
front_pos = 0;
while (front_pos <= rear_pos)
{
printf("%d ", deque_arr[front_pos]); front_pos++;
}
}
printf("\n");
}
void input_que()
{
int choice; do
{
printf("1.Insert at right\n"); printf("2.Delete from left\n");
printf("3.Delete from right\n"); printf("4.Display\n"); printf("5.Quit\n");
printf("Enter your choice : "); scanf("%d", &choice);
switch (choice)
{
case 1:
insert_right(); break;
case 2:
delete_left(); break;
case 3:
delete_right(); break;
case 4:
display_queue(); break;
case 5:
break; default:
printf("Wrong choice\n");
}
} while (choice != 5);
}
void output_que()
{
int choice; do
{
printf("1.Insert at right\n"); printf("2.Insert at left\n");
printf("3.Delete from left\n"); printf("4.Display\n"); printf("5.Quit\n");
printf("Enter your choice : "); scanf("%d", &choice);
switch (choice)
{
case 1:
insert_right(); break;
case 2:
insert_left(); break;
case 3:
delete_left(); break;
case 4:
display_queue(); break;
case 5:
break; default:
printf("Wrong choice\n");
}
} while (choice != 5);
}
int main()
{
int choice;
printf("1.Input restricted dequeue\n"); printf("2.Output restricted dequeue\n");
printf("Enter your choice : "); scanf("%d", &choice);
switch (choice)
{
case 1:
input_que(); break;
case 2:
output_que(); break;
default:
printf("Wrong choice\n");
}
}
Output:-
int main(){
int poly_first[10], poly_second[10], poly_sum[10], first_term,
second_terms, a, m, n, y, b;
printf("\nAdding Polynomial using arrays\n");
printf("\nNumber of terms for the first polynomial:");
scanf("%d", &first_term);
printf("\nEnter terms and coefficient for the first polynomial:\n");
for (n = 0; n < 2 * first_term; n++)
scanf("%d", &poly_first[n]); printf("\n\The first polynomial:\n"); m = 0;
if (poly_first[m + 1] == 1)
printf("x^%d", poly_first[m]); else
printf("%dx^%d", poly_first[m + 1], poly_first[m]); m += 2;
while (m < n)
{
printf("+%dx^%d", poly_first[m + 1], poly_first[m]); m += 2;
}
printf("\nTotal terms for the second polynomial:\n");
scanf("%d", &second_terms);
printf("\nEnter terms and coefficient for the second polynomial:\n");
for (y = 0; y < 2 * second_terms; y++)
scanf("%d", &poly_second[y]); printf("The Second polynomial:\n"); m = 0;
if (poly_second[m + 1] == 1)
printf("x^%d", poly_second[m]); else
printf("%dx^%d", poly_second[m + 1], poly_second[m]); m += 2;
while (m < 2 * second_terms)
{
printf("+%dx^%d", poly_second[m + 1], poly_second[m]); m += 2;
}
n = 0;
y = 0;
a = 0;
Output:-
int size = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
if (sparseMatrix[i][j] != 0) size++;
int compactMatrix[3][size]; int k = 0;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 5; j++)
if (sparseMatrix[i][j] != 0)
{
compactMatrix[0][k] = i; compactMatrix[1][k] = j;
compactMatrix[2][k] = sparseMatrix[i][j]; k++;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < size; j++)
printf("%d ", compactMatrix[i][j]);
printf("\n");
}
return 0;
}
Output:-
if (*head_ref == NULL)
{
*head_ref = new_node; return;
}
// Delete a node
void deleteNode(struct Node **head_ref, int key)
{
struct Node *temp = *head_ref, *prev;
free(temp);
}
// Search a node
if (head_ref == NULL)
{
return;
}
else
{
while (current != NULL)
{
// index points to the node next to current
index = current->next;
Output:-
Doubly:-
#include <stdio.h>
#include <stdlib.h>
return;
}
getchar(); return 0;
}
Output:-
Circularly:-
#include <stdio.h>
#include <stdlib.h>
// Input data
printf("\nEnter data to be " "inserted: \n");
scanf("%d", &data);
return 0;
}
Output:-
Output:-
Question-7: Depth first and breadth first traversal of graph represented using
adjacency matrix and list.
Solution:-
Depth First Transversal
// C code to implement above approach
#include <stdio.h>
#include <stdlib.h>
return G;
}
Output:-
Output:-