Dsa Program 1-5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

PROGRAM-1

AIM: Implement sparse matrix using array. Description of program:


a. Read a 2D array from the user.
b. Store it in the sparse matrix form, use array of structures.
c. Print the final array.

THEORY:
CODE
#include <stdio.h>
struct Element {
int row;
int col;
Int value;
};
int main() {
printf(“Ansh Balgotra \n”);
int rows, cols;
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);

int matrix[rows][cols];

for (int i = 0; i < rows; i++) {


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

struct Element sparse[rows * cols];


int sparseSize = 0;

for (int i = 0; i < rows; i++) {


for (int j = 0; j < cols; j++) {
if (matrix[i][j] != 0) {
sparse[sparseSize].row = i;
sparse[sparseSize].col = j;
sparse[sparseSize].value = matrix[i][j];
sparseSize++;
}
}
}

printf("\nSparse Matrix:\n");
printf("Row\tColumn\tValue\n");
for (int i = 0; i < sparseSize; i++)
{
printf("%d\t%d\t%d\n", sparse[i].row, sparse[i].col, sparse[i].value);
}
return 0;
}
OUTPUT
PROGRAM-2
AIM: Create a linked list with nodes having information about a student and perform
a. Insert a new node at specified position.
b. Delete a node with the roll number of student specified.
c. Reversal of that linked list.

THEORY:
CODE
#include <stdio.h>
#include
<string.h>

struct Student {
int roll;
char name[50];
struct Student*
next;
};

struct Student students[100];

int studentCount = 0;

void insertNode(int position, int roll, const char* name) {


if (position < 1 || position > studentCount + 1) {
printf("Invalid position for insertion.\n");
return;
}

if (studentCount >= 100) {


printf("Maximum number of students reached.\n");
return;
}

for (int i = studentCount; i >= position; i--) {


students[i] = students[i - 1];
}

students[position - 1].roll = roll;


strcpy(students[position - 1].name, name);
studentCount++;
}

void deleteNode(int rollToDelete)


{ int found = 0;
for (int i = 0; i < studentCount; i++) {
if (students[i].roll == rollToDelete)
{
for (int j = i; j < studentCount - 1; j++) {
students[j] = students[j + 1];
}
studentCount--;
found = 1
break;
}
}
if (!found) {
printf("Student with roll number %d not found.\n", rollToDelete);
}
}

void reverseList() {
struct Student
temp;
for (int i = 0; i < studentCount / 2; i++) {
temp = students[i];
students[i] = students[studentCount - i - 1];
students[studentCount - i - 1] = temp;
}
}

void printList() {
for (int i = 0; i < studentCount; i++) {
printf("Roll: %d, Name: %s\n", students[i].roll, students[i].name);
}
}
int main() {
printf(“Ansh Balgotra \n”);
insertNode(1, 101, "Alice");
insertNode(2, 102, "Bob");
insertNode(3, 103, "Charlie");

insertNode(2, 104, "David"); deleteNode(102);

reverseList();

printList();
return 0;
}
OUTPUT
PROGRAM-3
AIM:Create doubly linked list with nodes having information about an employee and
perform Insertion at front of doubly linked list and perform deletion at end of that doubly
linked list.

THEORY:
CODE
#include <stdio.h>
#include<stdlib.h>

struct Employee {
int empID;
char name[50];
};

struct Node {
struct Employee data;
struct Node* next;
struct Node* prev;
};

struct Node* head = NULL;


struct Node* tail = NULL;

void insertAtFront(int empID, const char* name) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data.empID = empID;
strcpy(newNode->data.name, name);

newNode->next = head;
newNode->prev =
NULL;

if (head != NULL) {
head->prev = newNode;
}

head = newNode;

if (tail == NULL)

{
tail = newNode;
}
}

void deleteAtEnd() {
if (tail != NULL) {
struct Node* temp = tail;
tail = tail->prev;

if (tail != NULL) {
tail->next =
NULL;
} else {
head = NULL;
}

free(temp);
}
}

void displayList() {
struct Node* current = head;
while (current != NULL) {
printf("Employee ID: %d, Name: %s\n", current->data.empID, current->data.name);
current = current->next;
}
}

int main() {
printf(“Ansh Balgotra\n”);
insertAtFront(101, "Alice");
insertAtFront(102, "Bob");
insertAtFront(103, "Charlie");
printf("Doubly Linked List (After Insertion at Front):\n");
displayList();
deleteAtEnd();
printf("\nDoubly Linked List (After Deletion at End):\n");
displayList();
return 0;
}

OUTPUT
PROGRAM-4
AIM:Create circular linked list having information about a college and perform Insertion at
front and perform deletion at end.

THEORY:
CODE
#include <stdio.h>
#include <stdlib.h>
#include
<string.h>

struct College {
int collegeID;
char
name[50];
};

struct Node {
struct College data;
struct Node* next;
};

struct Node* head = NULL;

void insertAtFront(int collegeID, const char* name) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node)); newNode->data.collegeID = collegeID;
strcpy(newNode->data.name, name);

if (head == NULL) {
newNode->next = newNode;
head = newNode;
} else {
struct Node* tail = head;
while (tail->next != head)
{
tail = tail->next;
}
newNode->next =
head; tail->next =
newNode; head =
newNode;
}
}

void deleteAtEnd() {
if (head == NULL) {
printf("List is empty. Nothing to delete.\
n"); return;
}

struct Node* current = head;


struct Node* prev = NULL;
while (current->next != head) {
prev = current;
current = current->next;
}

if (prev == NULL)
{ free(current);
head = NULL;
} else {
prev->next =
head;
free(current);
}
}

void displayList() {
if (head == NULL) {
printf("The list is empty.\
n"); return;
}

struct Node* current = head;


do {
printf("College ID: %d, Name: %s\n", current->data.collegeID, current->data.name);
current = current->next;
} while (current != head);
}

int main() {
printf(“Ansh Balgotra\n”);
insertAtFront(1, "ABC College");
insertAtFront(2, "XYZ College");
insertAtFront(3, "DEF College");

printf("Circular Linked List (After Insertion at Front):\n");


displayList();

deleteAtEnd();

printf("\nCircular Linked List (After Deletion at End):\n");


displayList();

return 0;
}
OUTPUT
PROGRAM-5
AIM:Implement two stacks using a single array.

THEORY:
CODE
#include <stdio.h>
#include <stdbool.h>

#define MAX_SIZE 100

int arr[MAX_SIZE];
int top1 = -1;
int top2 = MAX_SIZE;

bool isFull() {
return (top1 == top2 - 1);
}

bool isEmpty1() {
return (top1 == -
1);
}

bool isEmpty2() {
return (top2 == MAX_SIZE);
}

void push1(int value) {


if (!isFull()) {
arr[++top1] = value;
} else {
printf("Stack 1 is full. Cannot push %d\n", value);
}
}

void push2(int value) {


if (!isFull()) {
arr[--top2] = value;
} else {
printf("Stack 2 is full. Cannot push %d\n", value);
}
}
int pop1() {
if (!isEmpty1()) {
return
arr[top1--];
} else {
printf("Stack 1 is empty. Cannot pop.\
n"); return -1; // Assuming -1 as an error
code
}
}

int pop2() {
if (!isEmpty2()) {
return arr[top2+
+];
} else {
printf("Stack 2 is empty. Cannot pop.\
n"); return -1; // Assuming -1 as an error
code
}
}
int peek1() {
if (!isEmpty1()) {
return
arr[top1];
} else {
printf("Stack 1 is empty. Cannot peek.\
n"); return -1; // Assuming -1 as an error
code
}
}
int peek2() {
if (!isEmpty2()) {
return
arr[top2];
} else {
printf("Stack 2 is empty. Cannot peek.\
n"); return -1; // Assuming -1 as an error
code
}
}
int main() {
printf(“Ansh Balgotra\n”);
push1(1);
push1(2);
push1(3);

push2(4);
push2(5);
push2(6);

printf("Stack 1 Pop: %d\n", pop1());


printf("Stack 2 Pop: %d\n", pop2());

printf("Stack 1 Peek: %d\n", peek1());


printf("Stack 2 Peek: %d\n", peek2());
return 0;
}
OUTPUT

You might also like