0% found this document useful (0 votes)
9 views69 pages

JDSSS

This document is a lab file for Data Structures course at Indira Gandhi Delhi Technical University for Women, detailing various programming experiments in C. It includes tasks such as implementing array operations, matrix operations, string manipulations, and linked list operations, along with example code for each task. The document serves as a practical guide for students to learn and apply data structure concepts through coding.

Uploaded by

kkalpanaa02
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)
9 views69 pages

JDSSS

This document is a lab file for Data Structures course at Indira Gandhi Delhi Technical University for Women, detailing various programming experiments in C. It includes tasks such as implementing array operations, matrix operations, string manipulations, and linked list operations, along with example code for each task. The document serves as a practical guide for students to learn and apply data structure concepts through coding.

Uploaded by

kkalpanaa02
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/ 69

INDIRA GANDHI DELHI TECHNICAL

UNIVERSITY FOR WOMEN

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

S.N Experiment Date Remarks


O
Write a C program to implement Array data structure with
the following operations

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

3. Write c program to perform following string operation


a. Concatenate two strings
b. Reverse a string
c. Find the no. of occurrences of a word in a string

4. Write a c program to perform following operation on a single


linked list data structure
a. Traversal
b. Insertion
i) Insertion after a particular node
ii) Insertion before a particular node
c. Deletion
d. Reversal of a linked list by revering the links

5. Write a program to add two polynomial equations using linked list.

6. Write a program to perform following operation on a doubly linked


list.
a. Traversal
b. Insertion
c. Deletion

7. Write c program to perform operations on a circular linked list.


a. Traversal
b. Insertion
c. Deletion

Write c program to implement stack using array.


8.

9. Write c program to implement stack using linked list

10. Write c program to implement queue using array


EXPERIMENT 1
AIM : write a c program to implement array data structure with following operations.
a. Traversal
b. Insertion
c. Deletion
d. Sorting
e. Searching (linear search)
a. Traversal
C program to traverse the array

#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);

printf("Modified array is \n");


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

return 0;
}
output

D. Sorting

#include <stdio.h>

int main()
{

int arr[] = {5, 2, 8, 7, 1};


int temp = 0;

int length = sizeof(arr)/sizeof(arr[0]);

printf("Elements of original array: \n");


for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
for (int i = 0; i < length; i++) {
for (int j = i+1; j < length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

printf("\n");

printf("Elements of array sorted in ascending order: \n");


for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
return 0;
}

OUTPUT

E. Searching (linear search)


#include <stdio.h>

int linearSearch(int* arr, int size, int key)


{
for (int i = 0; i < size; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
int main()
{
int arr[10] = { 3, 4, 1, 7, 5, 8, 11, 42, 3, 13 };
int size = sizeof(arr) / sizeof(arr[0]);
int key = 4;
int index = linearSearch(arr, size, key);
if (index == -1) {
printf("The element is not present in the arr.");
}
else {
printf("The element is present at arr[%d].", index);
}

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);

printf("Result matrix is \n");


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

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}};

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;

subtract(A, B, C);

printf("Result matrix is \n");


for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
printf("%d ", C[i][j]);
printf("\n");
}
return 0;
}
OUTPUT

C MUTIPLICATION
#include <stdio.h>
#include <stdlib.h>
#define R1 2
#define C1 2
#define R2 2
#define C2 2

void mulMat(int mat1[][C1], int mat2[][C2])


{
int rslt[R1][C2];
printf("Multiplication of given two matrices is:\n");
for (int i = 0; i < R1; i++) {
for (int j = 0; j < C2; j++) {
rslt[i][j] = 0;

for (int k = 0; k < R2; k++) {


rslt[i][j] += mat1[i][k] * mat2[k][j];
}
printf("%d\t", rslt[i][j]);
}
printf("\n");
}
}
int main()
{
int mat1[R1][C1] = { { 1, 1 },
{ 2, 2 } };
int mat2[R2][C2] = { { 1, 1 },
{ 2, 2 } };
if (C1 != R2) {
printf("The number of columns in Matrix-1 must be "
"equal to the number of rows in "
"Matrix-2\n");
printf("Please update MACROs value according to "
"your array dimension in "
"#define section\n");
exit(EXIT_FAILURE);
}
mulMat(mat1, mat2);
return 0;
}
OUTPUT
D TRANSPOSE
#include <stdio.h>
#define N 4
void transpose(int A[][N], int B[][N])
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
B[i][j] = A[j][i];
}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], i, j;
transpose(A, B);
printf("Result matrix is \n");
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++)
printf("%d ", B[i][j]);
printf("\n");
}
return 0;
}
OUTPUT
EXPERIMENT 3
AIM: Write c program to perform following string operation
A. Concatenate two strings
B. Reverse a string
C. Find the no. of occurrences of a word in a string

A concatenate two srings


#include <stdio.h>
int main()
{
char str1[100] = "hello", str2[100] = "World";
char str3[100];
int i = 0, j = 0;
printf("\nFirst string: %s", str1);
printf("\nSecond string: %s", str2);
while (str1[i] != '\0') {
str3[j] = str1[i];
i++;
j++;
}
i = 0;
while (str2[i] != '\0') {
str3[j] = str2[i];
i++;
j++;

}
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);

printf (" \n After the reverse of a string: %s ", strrev(str));


return 0;
}
OUTPUT
C Find the no. of occurrence of a word in a string

#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;

while (current != NULL) {


printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
// Create nodes for the linked list
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("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) {

struct Node* current = head;

while (current != NULL) {


printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}

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

Insertion before a particular node


#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void insertBeforeNode(struct Node** headRef, struct Node* targetNode, int newData)
{

if (*headRef == NULL) {
printf("Linked list is empty.\n");
return;
}

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));


newNode->data = newData;
newNode->next = NULL;
if (*headRef == targetNode) {
newNode->next = *headRef;
*headRef = newNode;
return;
}
struct Node* current = *headRef;
while (current != NULL && current->next != targetNode) {
current = current->next;
}
if (current == NULL) {
printf("Target node not found in the linked list.\n");
free(newNode);
return;
}
newNode->next = current->next;
current->next = newNode;
}
void traverseLinkedList(struct Node* head) {
struct Node* current = head;

while (current != NULL) {


printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
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);
insertBeforeNode(&head, second, 4);
printf("Modified Linked List: ");
traverseLinkedList(head);
free(first);

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

D REVERSAL OF A LINKED LIST BY REVERSING THE LINKS


#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;
}
struct Node* reverseList(struct Node* head) {
struct Node* prev = NULL;
struct Node* current = head;
struct Node* next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
return head;
}
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);
head = reverseList(head);
printf("Reversed Linked List: ");
displayList(head);
while (head != NULL) {
struct Node* temp = head;
head = head->next;
free(temp);
}

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;

while (poly1 != NULL && poly2 != NULL) {


if (poly1->exponent == poly2->exponent) {
int sum_coeff = poly1->coefficient + poly2->coefficient;
insertTerm(&result, sum_coeff, poly1->exponent);
poly1 = poly1->next;
poly2 = poly2->next;
} else if (poly1->exponent > poly2->exponent) {
insertTerm(&result, poly1->coefficient, poly1->exponent);
poly1 = poly1->next;
} else {
insertTerm(&result, poly2->coefficient, poly2->exponent);
poly2 = poly2->next;
}
}
while (poly1 != NULL) {
insertTerm(&result, poly1->coefficient, poly1->exponent);
poly1 = poly1->next;
}
while (poly2 != NULL) {
insertTerm(&result, poly2->coefficient, poly2->exponent);
poly2 = poly2->next;
}
return result;
}
void displayPolynomial(struct Term* poly) {
struct Term* current = poly;
while (current != NULL) {
printf("%dx^%d", current->coefficient, current->exponent);
current = current->next;
if (current != NULL) {
printf(" + ");
}
}
printf("\n");
}
int main() {
struct Term* poly1 = NULL;
struct Term* poly2 = NULL;
insertTerm(&poly1, 3, 2);
insertTerm(&poly1, 2, 1);
insertTerm(&poly1, 5, 0);
insertTerm(&poly2, 1, 2);
insertTerm(&poly2, 3, 1);
insertTerm(&poly2, 4, 0);
printf("First Polynomial: ");
displayPolynomial(poly1);
printf("Second Polynomial: ");
displayPolynomial(poly2);
struct Term* sum = addPolynomials(poly1, poly2);
printf("Sum of Polynomials: ");
displayPolynomial(sum);
while (poly1 != NULL) {
struct Term* temp = poly1;
poly1 = poly1->next;
free(temp);
}
while (poly2 != NULL) {
struct Term* temp = poly2;
poly2 = poly2->next;
free(temp);
}
while (sum != NULL) {
struct Term* temp = sum;

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;

printf("Doubly Linked List (Backward): ");


while (current != NULL) {
printf("%d -> ", current->data);
current = current->prev;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
insertEnd(&head, 1);
insertEnd(&head, 2);
insertEnd(&head, 3);
insertEnd(&head, 4);
traverseForward(head);
traverseBackward(head);
while (head != NULL) {
struct Node* temp = head;
head = head->next;
free(temp);
}

return 0;

}
OUTPUT
B INSERTION
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a doubly linked list node


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

// Function to create a new node


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;
}

// Function to insert a node at the beginning of the doubly linked list


void insertBegin(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
}

// Function to insert a node at the end of the doubly linked list


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;
}
}

// Function to insert a node at a specific position in the doubly linked list


void insertAtPosition(struct Node** head, int data, int position) {
if (position < 1) {
printf("Invalid position\n");
return;
}

struct Node* newNode = createNode(data);


if (*head == NULL) {
if (position == 1) {
*head = newNode;
} else {
printf("Invalid position\n");
}
} else {
if (position == 1) {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
} else {
struct Node* current = *head;
int currentPosition = 1;
while (currentPosition < position - 1 && current->next != NULL) {
current = current->next;
currentPosition++;
}
if (currentPosition == position - 1) {
newNode->next = current->next;
newNode->prev = current;
if (current->next != NULL) {
current->next->prev = newNode;
}
current->next = newNode;
} else {
printf("Invalid position\n");
}
}
}
}

// Function to display the doubly linked list


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;

// Insert elements at the beginning


insertBegin(&head, 1);
insertBegin(&head, 2);

// Insert elements at the end


insertEnd(&head, 3);
insertEnd(&head, 4);
// Insert elements at a specific position
insertAtPosition(&head, 5, 3);

printf("Doubly Linked List: ");


displayList(head);

// Free the memory used by the doubly linked list


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

return 0;
}
OUTPUT

C DELETION
#include <stdio.h>
#include <stdlib.h>

// Define the structure for a node in the doubly linked list


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

// Function to create a new node


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

// Function to delete a node by value


void deleteNode(struct Node** head, int key) {
if (*head == NULL) {
printf("The list is empty.\n");
return;
}

struct Node* current = *head;

// Check if the key is in the first node


if (current->data == key) {
*head = current->next;
if (*head != NULL)
(*head)->prev = NULL;
free(current);
printf("Node with value %d deleted.\n", key);
return;
}

// Traverse the list to find the node to delete


while (current != NULL && current->data != key) {
current = current->next;
}

// If the key is not found


if (current == NULL) {
printf("Node with value %d not found.\n", key);
return;
}
// Adjust the pointers to delete the node
current->prev->next = current->next;
if (current->next != NULL)
current->next->prev = current->prev;

free(current);
printf("Node with value %d deleted.\n", key);
}

// Function to display the doubly linked list


void displayList(struct Node* head) {
printf("Doubly Linked List: ");
while (head != NULL) {
printf("%d <-> ", head->data);
head = head->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = NULL;

// Create a doubly linked list


head = createNode(1);
head->next = createNode(2);
head->next->prev = head;
head->next->next = createNode(3);
head->next->next->prev = head->next;
head->next->next->next = createNode(4);
head->next->next->next->prev = head->next->next;

displayList(head);

// Delete a node by value


deleteNode(&head, 3);

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>

// 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 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;
}

// Insert the new node at the end


last->next = newNode;
newNode->next = *head;
}
}

// Function to traverse and print the elements of the circular linked list
void traverse(struct Node* head) {
struct Node* current = head;

// If the list is empty, nothing to traverse


if (head == NULL) {
printf("Circular Linked List is empty.\n");
return;
}

printf("Circular Linked List elements: ");


do {
printf("%d ", current->data);
current = current->next;
} while (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;
}

// Insert the new node at the end


last->next = newNode;
newNode->next = *head;
}
}

// 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;
}

// Create a new node


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;

// Insert the new node after the specified node


newNode->next = prevNode->next;
prevNode->next = newNode;
}

// Function to traverse and print the elements of the circular linked list
void traverse(struct Node* head) {
struct Node* current = head;

// If the list is empty, nothing to traverse


if (head == NULL) {
printf("Circular Linked List is empty.\n");
return;
}

printf("Circular Linked List elements: ");

do {
printf("%d ", current->data);
current = current->next;
} while (current != head);

printf("\n");
}

int main() {
struct Node* head = NULL;

// Insert elements into the circular linked list


insertEnd(&head, 1);
insertEnd(&head, 2);
insertBegin(&head, 0);
insertAfter(head->next, 3);

// Traverse and print the circular linked list


traverse(head);
return 0;
}
OUTPUT

C DELETION
#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 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;
}

// Insert the new node at the end


last->next = newNode;
newNode->next = *head;
}
}

// Function to delete a node by value from the circular linked list


void deleteNode(struct Node** head, int key) {
if (*head == NULL) {
printf("Circular Linked List is empty. Cannot delete.\n");
return;
}

struct Node* current = *head;


struct Node* prev = NULL;
// Search for the node to be deleted
while (current->data != key) {
if (current->next == *head) {
printf("Node with value %d not found in the Circular Linked List.\n",
key);
return;
}
prev = current;
current = current->next;
}

// If the node to be deleted is the only node in the list


if (current == *head && current->next == *head) {
*head = NULL;
free(current);
return;
}

// If the node to be deleted is the head of the list


if (current == *head) {
prev = *head;
while (prev->next != *head) {
prev = prev->next;
}
*head = current->next;
prev->next = *head;
}

// If the node to be deleted is not the head of the list


prev->next = current->next;
free(current);
}

// Function to delete the entire circular linked list


void deleteList(struct Node** head) {
if (*head == NULL) {
printf("Circular Linked List is already empty.\n");
return;
}

struct Node* current = *head;


struct Node* temp = NULL;

while (current->next != *head) {


temp = current;
current = current->next;
free(temp);
}
*head = NULL;
free(current);
}

// Function to traverse and print the elements of the circular linked list
void traverse(struct Node* head) {
struct Node* current = head;

// If the list is empty, nothing to traverse


if (head == NULL) {
printf("Circular Linked List is empty.\n");
return;
}

printf("Circular Linked List elements: ");

do {
printf("%d ", current->data);
current = current->next;
} while (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 and print the circular linked list


printf("Original Circular Linked List:\n");
traverse(head);

// Delete a node by value


deleteNode(&head, 2);
printf("Circular Linked List after deleting node with value 2:\n");
traverse(head);
deleteList(&head);
printf("Circular Linked List after deleting the entire list:\n");
traverse(head);
return 0;
}
OUTPUT

EXPERIMENT 8
AIM : Write c program to implement stack using array
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100

// Define the stack structure


struct Stack {
int data[MAX_SIZE];
int top;
};

// Function to initialize an empty stack


void initialize(struct Stack* stack) {
stack->top = -1;
}

// Function to check if the stack is empty


int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
// Function to check if the stack is full
int isFull(struct Stack* stack) {
return stack->top == MAX_SIZE - 1;
}

// Function to push an element onto the stack


void push(struct Stack* stack, int element) {
if (isFull(stack)) {
printf("Stack overflow! Cannot push element %d.\n", element);
return;
}

stack->data[++stack->top] = element;
printf("Pushed %d onto the stack.\n", element);
}

// Function to pop an element from the stack


int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty! Cannot pop.\n");
return -1; // Return a sentinel value indicating an error
}

int element = stack->data[stack->top--];


printf("Popped %d from the stack.\n", element);
return element;
}
int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty! Cannot peek.\n");
return -1;
}

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

You might also like