JDSSS
JDSSS
LAB FILE
Data Structures (BCS-201)
B.TECH ECE-AI (2022-2026)
Submitted To: Submitted By:
Ms. Rajni Sharma KALPANA
15801182022
ECE-AI 2
3 rd Semester 2nd year
INDEX
1. a. Traversal
b. Insertion
c. Deletion
d. Sorting
e. Searching (linear Search)
Write a c program to perform following operation on matrices
2. a. Addition
b. Subtraction
c. Multiplication
d. Transpose
#include <stdio.h>
void printArray(int* arr, int n)
{
int i;
printf("Array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[] = { 2, -1, 5, 6, 0, -3 };
int n = sizeof(arr) / sizeof(arr[0]);
printArray(arr, n);
return 0;
}
output
b.Inseration
#include <stdio.h>
int main()
{
int arr[100] = { 0 };
int i, x, pos, n = 10
for (i = 0; i < 10; i++)
arr[i] = i + 1;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
x = 50;
pos = 5;
n++;
for (i = n - 1; i >= pos; i--)
arr[i] = arr[i - 1];
arr[pos - 1] = x;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
output
c.Deletion
#include <stdio.h>
int deleteElement(int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++) {
if (arr[i] == x) {
break;
}
}
if (i < n) {
n = n - 1;
for (int j = i; j < n; j++) {
arr[j] = arr[j + 1];
}
}
return n;
}
{
int arr[] = { 11, 15, 6, 8, 9, 10 };
int n = sizeof(arr) / sizeof(arr[0]);
int x = 6;
n = deleteElement(arr, n, x);
return 0;
}
output
D. Sorting
#include <stdio.h>
int main()
{
printf("\n");
OUTPUT
return 0;
}
OUTPUT
EXPERIMENT 2
AIM : Write a c program to perform following operation on matrices
a.Addition
b. Subtraction
c. Multiplication
d. Transpose
A ADDITION
#include <stdio.h>
#define N 4
void add(int A[][N], int B[][N], int C[][N])
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
C[i][j] = A[i][j] + B[i][j];
}
int main()
{
int A[N][N] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
int B[N][N] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };
int C[N][N];
int i, j;
add(A, B, C);
return 0;
}
OUTPUT
B SUBTRACTION
#include <stdio.h>
#define N 4
void subtract(int A[][N], int B[][N], int C[][N])
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
C[i][j] = A[i][j] - B[i][j];
}
int main()
{
int A[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
subtract(A, B, C);
C MUTIPLICATION
#include <stdio.h>
#include <stdlib.h>
#define R1 2
#define C1 2
#define R2 2
#define C2 2
}
str3[j] = '\0';
// Print the concatenated string
printf("\nConcatenated string: %s", str3);
return 0;
}
OUTPUT
B Reverse a string
#include <stdio.h>
#include <string.h>
int main()
{
char str[40];
printf (" \n Enter a string to be reversed: ");
scanf ("%s", str);
#include <stdio.h>
#define OUT 0
#define IN 1
unsigned countWords(char *str)
{
int state = OUT;
unsigned wc = 0;
while (*str)
{
if (*str == ' ' || *str == '\n' || *str == '\t')
state = OUT;
else if (state == OUT)
{
state = IN;
++wc;
}
++str;
}
return wc;
}
int main(void)
{
char str[] = "One two three\n four\tfive ";
printf("No of words : %u", countWords(str));
return 0;
}
OUTPUT
EXPERIMENT 4
AIM: Write a c program to perform following operation on a single linked list data
structure
e. Traversal
f. Insertion
iii) Insertion after a particular node
iv) Insertion before a particular node
g. Deletion
h. Reversal of a linked list by revering the links
A. Traversal
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void traverseLinkedList(struct Node* head) {
struct Node* current = head;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
struct Node* head = first;
printf("Linked List: ");
traverseLinkedList(head);
free(first);
free(second);
free(third);
return 0;
}
OUTPUT
B. INSERTION
Insertion after a particular node
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertAfterNode(struct Node* prevNode, int newData) {
if (prevNode == NULL) {
printf("Previous node cannot be NULL.\n");
return;
}
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = newData;
newNode->next = prevNode->next;
prevNode->next = newNode;
}
void traverseLinkedList(struct Node* head) {
int main() {
struct Node* first = (struct Node*)malloc(sizeof(struct Node));
struct Node* second = (struct Node*)malloc(sizeof(struct Node));
struct Node* third = (struct Node*)malloc(sizeof(struct Node));
first->data = 1;
first->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
struct Node* head = first;
printf("Original Linked List: ");
traverseLinkedList(head);
insertAfterNode(second, 4);
printf("Modified Linked List: ");
traverseLinkedList(head);
free(first);
free(second);
free(third);
return 0;
}
OUTPUT
if (*headRef == NULL) {
printf("Linked list is empty.\n");
return;
}
free(second);
free(third);
return 0;
}
OUTPUT
C DELETION
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void deleteNode(struct Node** head, int key) {
struct Node* current = *head;
struct Node* prev = NULL;
if (current != NULL && current->data == key) {
*head = current->next;
free(current);
return;
}
while (current != NULL && current->data != key) {
prev = current;
current = current->next;
}
if (current == NULL) {
printf("Key not found in the list\n");
return;
}
prev->next = current->next;
free(current);
}
void displayList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
printf("Original Linked List: ");
displayList(head);
int key;
printf("Enter the value to delete: ");
scanf("%d", &key);
deleteNode(&head, key);
printf("Linked List after deletion: ");
displayList(head);
while (head != NULL) {
struct Node* temp = head;
head = head->next;
free(temp);
}
return 0;
}
OUTPUT
return 0;
}
OUTPUT
EXPERIMENT -5
AIM: Write a program to add two polynomial equations using linked list.
#include <stdio.h>
#include <stdlib.h>
struct Term {
int coefficient;
int exponent;
struct Term* next;
};
struct Term* createTerm(int coefficient, int exponent) {
struct Term* newTerm = (struct Term*)malloc(sizeof(struct Term));
if (newTerm == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newTerm->coefficient = coefficient;
newTerm->exponent = exponent;
newTerm->next = NULL;
return newTerm;
}
void insertTerm(struct Term** head, int coefficient, int exponent) {
struct Term* newTerm = createTerm(coefficient, exponent);
if (*head == NULL) {
*head = newTerm;
} else {
struct Term* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newTerm;
}
}
struct Term* addPolynomials(struct Term* poly1, struct Term* poly2) {
struct Term* result = NULL;
sum = sum->next;
free(temp);
}
return 0;
}
OUTPUT
EXPERIMENT 6
AIM: Write a program to perform following operation on a doubly linked list.
d. Traversal
e. Insertion
f. Deletion
A Traversal
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
void insertEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}
void traverseForward(struct Node* head) {
struct Node* current = head;
printf("Doubly Linked List (Forward): ");
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
void traverseBackward(struct Node* tail) {
struct Node* current = tail;
return 0;
}
OUTPUT
B INSERTION
#include <stdio.h>
#include <stdlib.h>
int main() {
struct Node* head = NULL;
return 0;
}
OUTPUT
C DELETION
#include <stdio.h>
#include <stdlib.h>
free(current);
printf("Node with value %d deleted.\n", key);
}
int main() {
struct Node* head = NULL;
displayList(head);
displayList(head);
return 0;
}
OUTPUT
EXPERIMENT - 7
AIM: Write c program to perform operations on a circular linked list.
d. Traversal
e. Insertion
f. Deletion
A Traversal
#include <stdio.h>
#include <stdlib.h>
// Function to insert a new node at the end of the circular linked list
void insertEnd(struct Node** head, int data) {
// Create a new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
// If the list is empty, make the new node the only node
if (*head == NULL) {
newNode->next = newNode;
*head = newNode;
} else {
// Find the last node in the list
struct Node* last = *head;
while (last->next != *head) {
last = last->next;
}
// Function to traverse and print the elements of the circular linked list
void traverse(struct Node* head) {
struct Node* current = head;
printf("\n");
}
int main() {
struct Node* head = NULL;
// Insert elements into the circular linked list
insertEnd(&head, 1);
insertEnd(&head, 2);
insertEnd(&head, 3);
traverse(head);
return 0;}
OUTPUT
B INSERTION
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the circular linked list
struct Node {
int data;
struct Node* next;
};
// Function to insert a new node at the beginning of the circular linked list
void insertBegin(struct Node** head, int data) {
// Create a new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
// If the list is empty, make the new node the only node
if (*head == NULL) {
newNode->next = newNode;
*head = newNode;
} else {
// Find the last node in the list
struct Node* last = *head;
while (last->next != *head) {
last = last->next;
}
// Insert the new node at the beginning
newNode->next = *head;
last->next = newNode;
*head = newNode;
}
}
// Function to insert a new node at the end of the circular linked list
void insertEnd(struct Node** head, int data) {
// Create a new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
// If the list is empty, make the new node the only node
if (*head == NULL) {
newNode->next = newNode;
*head = newNode;
} else {
// Find the last node in the list
struct Node* last = *head;
while (last->next != *head) {
last = last->next;
}
// Function to insert a new node after a specific node with a given data
void insertAfter(struct Node* prevNode, int data) {
if (prevNode == NULL) {
printf("The given previous node cannot be NULL.\n");
return;
}
// Function to traverse and print the elements of the circular linked list
void traverse(struct Node* head) {
struct Node* current = head;
do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
printf("\n");
}
int main() {
struct Node* head = NULL;
C DELETION
#include <stdio.h>
#include <stdlib.h>
// Function to insert a new node at the end of the circular linked list
void insertEnd(struct Node** head, int data) {
// Create a new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
// If the list is empty, make the new node the only node
if (*head == NULL) {
newNode->next = newNode;
*head = newNode;
} else {
// Find the last node in the list
struct Node* last = *head;
while (last->next != *head) {
last = last->next;
}
// Function to traverse and print the elements of the circular linked list
void traverse(struct Node* head) {
struct Node* current = head;
do {
printf("%d ", current->data);
current = current->next;
} while (current != head);
printf("\n");
}
int main() {
struct Node* head = NULL;
EXPERIMENT 8
AIM : Write c program to implement stack using array
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
stack->data[++stack->top] = element;
printf("Pushed %d onto the stack.\n", element);
}
return stack->data[stack->top];
}
int main() {
struct Stack stack;
initialize(&stack);
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
printf("Top element of the stack: %d\n", peek(&stack));
pop(&stack);
pop(&stack);
printf("Top element of the stack: %d\n", peek(&stack));
pop(&stack);
pop(&stack);
return 0;
}
OUTPUT
EXPERIMENT-9
AIM: Write a c program to implement queue using array
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
struct Node *head = NULL;
void push(int val)
{
struct Node *newNode = malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = head;
head = newNode;
}
void pop()
{
struct Node *temp;
if(head == NULL)
printf("Stack is Empty\n");
else
{
printf("Poped element = %d\n", head->data);
temp = head;
head = head->next;
free(temp);
}
}
void display()
{
struct Node *temp = head;
while(temp != NULL)
{
printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main()
{
push(10);
push(20);
push(30);
printf("Linked List\n");
display();
pop();
printf("After the pop, the new linked list\n");
display();
pop();
printf("After the pop, the new linked list\n");
display();
return 0;
}
OUTPUT
EXPERIMENT 10
AIM: write c program to implementation queue using array.
#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;
}
OUTPUT