Dsa Program 1-5
Dsa Program 1-5
Dsa Program 1-5
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];
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;
};
int studentCount = 0;
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");
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;
};
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;
};
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;
}
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;
}
int main() {
printf(“Ansh Balgotra\n”);
insertAtFront(1, "ABC College");
insertAtFront(2, "XYZ College");
insertAtFront(3, "DEF College");
deleteAtEnd();
return 0;
}
OUTPUT
PROGRAM-5
AIM:Implement two stacks using a single array.
THEORY:
CODE
#include <stdio.h>
#include <stdbool.h>
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);
}
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);