0% found this document useful (0 votes)
18 views22 pages

Mayank 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)
18 views22 pages

Mayank 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/ 22

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

PROGRAM 1
WAP in C to create 1D Array and also transverse it.
#include <stdio.h>
#include <conio.h>

void main () {
int n, i, a[20];
clrscr();
printf("Programmed by Mayank Kumar");
printf("Enter the length of array: ");
scanf("%d", &n);
printf("Enter the elements\n");
for (i = 0; i <= n - 1; i++) {
scanf("%d", &a[i]);
}
printf("Traversing of the array\n");
for (i = 0; i <= n - 1; i++) {
printf("\n%d", a[i]);
}
getch();
}

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

PROGRAM 2
WAP in C to INSERT an element in 1D array
1. AT THE BEGINNING
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define max 20

void main() {
int a[max], i, j;
clrscr();
printf("Programmed by Mayank Kumar");
printf("\nEnter limit:");
scanf("%d", &j);
if (j >= max) {
printf("\nArray size doesn’t fulfill demand");
exit(0);
}
printf("\nEnter elements:");
for (i = 0; i < j; i++)
scanf("%d", &a[i]);
for (i = j; i > 0; i--)
a[i] = a[i - 1];
printf("\nEnter new element:");
scanf("%d", &a[0]);
printf("\nAfter insert:");
j++;
for (i = 0; i < j; i++)
printf("\n%d", a[i]);
getch();
}
OUTPUT: -
Experiment No. ………………… Date……………….

2. AT THE END
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define max 20

void main() {
int a[max], i, j, n;
clrscr();
printf("Programmed by Mayank Kumar");
printf("\nEnter limit:");
scanf("%d", &j);
if (j >= max) {
printf("\nSize of limit doesn't match");
exit(0);
}
printf("\nEnter element:");
for (i = 0; i < j; i++) {
scanf("%d", &a[i]);
}
printf("\nEnter new element:");
scanf("%d", &n);
a[j] = n;
printf("\nElements are:");
for (i = 0; i <= j; i++)
printf("\n%d", a[i]);
getch();
}

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

3. AT THE GIVEN POSITION


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define max 20

void main() {
int a[max], i, j, k, n, pos;
float item;

printf("Programmed by Mayank Kumar");


printf("\nEnter limit:");
scanf("%d", &n);
if (n >= max) {
printf("\nArray size doesn't exist");
exit(0);
}

printf("\nInput elements of array:\n");


for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}

printf("\nEnter element to be inserted:\n");


scanf("%f", &item);
printf("\nPosition of insertion:\n");
scanf("%d", &pos);

n++;
for (k = n; k >= pos; k--) {
a[k] = a[k - 1];
Experiment No. ………………… Date……………….

}
a[--pos] = item;

printf("\nElements of array after insertion:\n");


for (i = 0; i < n; i++) {
printf("\n%d", a[i]);
}

getch();
}

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

PROGRAM 3
WAP in C to DELETE an element in 1D array
1. AT THE BEGINNING
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 20

void main() {
int a[max], i, j;

clrscr();
printf("Programmed by Mayank Kumar");
printf("\nEnter limit:");
scanf("%d", &j);
if (j >= max) {
printf("\nArray size doesn't exist");
exit(0);
}

printf("\nInput elements of array:");


for (i = 0; i < j; i++) {
scanf("%d", &a[i]);
}

printf("\nElements of array after deletion at beginning:\n");


for (i = 0; i <= j - 2; i++) {
a[i] = a[i + 1];
printf("\n%d", a[i]);
}
Experiment No. ………………… Date……………….

j--;
getch();
}

OUTPUT: -

2. AT THE END
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 20

void main() {
int a[max], i, j;
clrscr();
printf("Programmed by Mayank Kumar");
printf("\nEnter limit:");
scanf("%d", &j);

if (j >= max) {
printf("\nArray size doesn't exist");
exit(0);
}
Experiment No. ………………… Date……………….

printf("\nInput elements of array:");


for (i = 0; i < j; i++) {
scanf("%d", &a[i]);
}

printf("\nElements of array after deletion at end:\n");


for (i = 0; i < j - 1; i++) {
printf("%d ", a[i]);
}

getch();
}

OUTPUT: -

3. DELETION OF THE SPECIFIC ELEMENT


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 20

void main() {
int a[max], i, j, k, n;
Experiment No. ………………… Date……………….

printf("Programmed by Mayank Kumar");


printf("\nEnter limit:");
scanf("%d", &j);

if (j >= max) {
printf("\nArray size doesn't exist");
exit(0);
}

printf("\nInput elements of array:\n");


for (i = 0; i < j; i++) {
scanf("%d", &a[i]);
}

printf("\nEnter element which you want to delete:\n");


scanf("%d", &n);

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


if (n == a[i]) {
for (k = i; k <= j - 2; k++) {
a[k] = a[k + 1];
}
j--;
break;
}
}

printf("\nElements of array after deletion:\n");


for (i = 0; i < j; i++) {
Experiment No. ………………… Date……………….

printf("\n%d", a[i]);
}

getch();
}

OUTPUT: -
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 MAYANK KUMAR\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 MAYANK KUMAR\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');
}

void traverse () {
Experiment No. ………………… Date……………….

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 MAYANK KUMAR\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 MAYANK KUMAR\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: -
Experiment No. ………………… Date……………….

PROGRAM – 11: -
Program to find no. of nodes and leaves in the BST
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
long value;
struct node *left;
struct node *right;
};
struct node *tree=NULL;
struct node *insert(struct node *tree,long value);
void countnode(struct node *tree);
void countleave(struct node*tree);
select();
struct node *temp;
int node = 1, total = 0;
void main ()
{
struct node *tree=NULL;
int choice;
long digit;
clrscr();
printf("Programmed by MAYANK KUMAR\n");
do
{
choice=select();
switch(choice)
{
case 1:
puts("Enter integers: To quit enter 0");
scanf("%ld", &digit);
while(digit != 0)
{
tree = insert(tree,digit);
scanf("%ld", &digit);
}
continue;

case 2:
countnode(tree);
printf("Number of nodes=%d\n", node);
continue;

case 3:
Experiment No. ………………… Date……………….

countleave(tree);
printf("Number of leaves=%d\n", total);
continue;

case 4:
puts("END");
exit(0);
}
}
while(choice != 4);
}

int select()
{
int selection;
do
{
puts("Enter 1: Insert a node");
puts("Enter 2: Display Number of nodes");
puts("Enter 3: Display Number of leave");
puts("Enter 4: End");
puts("Enter your choice");
scanf("%d", &selection);
if((selection<1) || (selection>4))
{
puts("Wrong choice: Try Again");

}
}
while((selection<1) || (selection>4));
return selection;
}
struct node *insert(struct node *tree,long digit)
{
if(tree==NULL)
{
tree=(struct node *)malloc(sizeof(struct node));
tree->left=tree->right=NULL;
tree->value=digit;
}
else if(digit<tree->value)
tree->left=insert(tree->left,digit);
else if(digit>tree->value)
tree->right=insert(tree->right,digit);
else if(digit==tree->value)
{
Experiment No. ………………… Date……………….

puts("Duplicates Nodes: Program Exited");


getch();
exit(0);
}
return(tree);
}
void countnode(struct node *tree)
{
if(tree != NULL)
{
if(tree->left != NULL)
{
node++;
countnode(tree->left);
}
if(tree->right != NULL)
{
node++;
countnode(tree->right);
}
}
}
void countleave(struct node *tree)
{
if(tree != NULL)
{
if((tree->left==NULL) && (tree->right==NULL))
total++;
else
{
countleave(tree->left);
countleave(tree->right);
}
}
}
Experiment No. ………………… Date……………….

OUTPUT: -

You might also like