0% found this document useful (0 votes)
6 views8 pages

Kum DS Lab

Uploaded by

me.mack.mk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Kum DS Lab

Uploaded by

me.mack.mk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Experiment No. ………………… Date……………….

PROGRAM 4
WAP of array implement of SPARSE MATRIX.
#include <stdio.h>
#include <conio.h>
#define MAX 20

struct triplet {
int row;
int col;
int element;
} spmatrix[MAX];

void main () {
int n, i, j, row, col;
int k = 0;

printf("Programmed by KUMKUM KUMARI\n");

printf("Enter matrix dimensions (rows and columns): ");


scanf("%d%d", &row, &col);
printf("Enter number of non-zero elements in the sparse matrix: ");
scanf("%d", &n);

for (i = 0; i < n; i++) {


printf("Enter element row, column, and its value: ");
scanf("%d%d%d", &spmatrix[i].row, &spmatrix[i].col, &spmatrix[i].element);
}
printf("\nSparse matrix is:\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
if (k < n && spmatrix[k].row == (i + 1) && spmatrix[k].col == (j + 1)) {
printf("%d\t", spmatrix[k].element);
k++;
} else {
printf("0\t");
}
}
printf("\n");
}

getch();
}
Experiment No. ………………… Date……………….

OUTPUT: -
Experiment No. ………………… Date……………….

PROGRAM 5
WAP in C to perform push and pop operation on an element in stack.
#include <stdio.h>
#include <conio.h>
#define MAX 50

int stack [MAX + 1], top = 0;

void create ();


void traverse ();
void push ();
void pop ();

void main () {
printf("Programmed by KUMKUM KUMARI\n");

create ();
printf("\nStack is:\n");
traverse ();
push ();
printf("After pushing an element, the stack is:\n");
traverse ();
pop ();
printf("After popping an element, the stack is:\n");
traverse ();

getch();
}

void create () {
char ch;
do {
if (top == MAX) {
printf("Stack is full (overflow).\n");
return;
}
top++;
printf("Input element: ");
scanf("%d", &stack[top]);
printf("Press <y> to add more elements, any other key to stop: ");
ch = getch();
} while (ch == 'y');
}
Experiment No. ………………… Date……………….

void traverse () {
if (top == 0) {
printf("Stack is empty.\n");
return;
}
for (int i = top; i > 0; --i)
printf("%d\n", stack[i]);
}
void push () {
int m;
if (top == MAX) {
printf("Stack is full (overflow).\n");
return;
}
printf("Input new element to insert: ");
scanf("%d", &m);
top++;
stack[top] = m;
}
void pop() {
if (top == 0) {
printf("Stack is empty (underflow).\n");
return;
}
printf("Popped element: %d\n", stack[top]);
stack[top] = 0;
top--;
}
OUTPUT: -
Experiment No. ………………… Date……………….

PROGRAM 6

WAP in C for tower of honoi.


#include<stdio.h>
#include<conio.h>

void main() {
int n;
char A = 'A', B = 'B', C = 'C';
void hanoi(int, char, char, char);

printf("Programmed by KUMKUM KUMARI\n");


printf("Enter number of disks: ");
scanf("%d", &n);
printf("\n\nTower of Hanoi problem with %d disks\n", n);
printf("Sequence is:\n");
hanoi(n, A, B, C);
printf("\n");
getch();
}

void hanoi(int n, char A, char B, char C) {


if (n != 0) {
hanoi(n - 1, A, C, B);
printf("Move disk %d from %c to %c\n", n, A, C);
hanoi(n - 1, B, A, C);
}
}
Experiment No. ………………… Date……………….

OUTPUT: -
Experiment No. ………………… Date……………….

PROGRAM 7: -
WAP in C to implement QUEUE using array.
#include<stdio.h>
#include<conio.h>
#define MAX 100

int q[MAX + 1], front = 0, rear = 0;

void main() {
void create();
void traverse();
void insert();
void delet();

printf("PROGRAMMED BY KUMKUM KUMARI\n");


create();
traverse();
insert();
printf("\nAfter inserting an element");
traverse();
delet();
printf("\nAfter deletion");
traverse();
getch();
}

void create() {
char ch;
front = 1;
do {
rear++;
printf("\nInput element in queue:\n");
scanf("%d", &q[rear]);
printf("Press <Y/N> for more elements");
ch = getch();
} while (ch == 'Y');
}

void traverse() {
int i;
printf("\nElements in the queue are:\n");
for (i = front; i <= rear; ++i)
printf("%d\n", q[i]);
}
Experiment No. ………………… Date……………….

void insert() {
int m;
if (rear == MAX) {
printf("Queue is overflow\n");
return;
}
printf("\nInput new element to insert\n");
scanf("%d", &m);
rear++;
q[rear] = m;
}

void delet() {
if (front == 0) {
printf("Queue is underflow\n");
return;
}
if (front == rear) {
q[front] = '\0';
front = rear = 0;
} else {
q[front] = '\0';
front++;
}
}

OUTPUT: -

You might also like