Jeeevith Final
Jeeevith Final
II SEMESTER
Computer Science & Engineering
Department
Laboratory Manual
Computational Thinking for Structured Design-2 303105151
FACULTYOF ENGINEERING & TECHNOLOGY
Computational Thinking for
Structured Design – 2
(303105151)
B.Tech - 1st Year 2nd Semester
CERTIFICATE
This is to certify that Mr. TUTA JEEVITH SWARUP has completed all the
practical of the subject on his/her own and is submitting the lab manual
with proper formatting as per the given instructions. His work is found
satisfactory and hence, he/she has successfully completed the term-work
submission of Computational Thinking for Structured Design-2
(303105151).
1423
7 8 10 9
6352
Output:
172
283
395
4 10 6
Explanation:
The sorted matrix according to the conditions is
1234
7 8 9 10
2356
after sorting matrix the elements as to be printed in
column major order
172
283
395
4 10 6
1. Write a c program to perform linear Search.
9 2. Write a c program to perform binary search.
Write a c program to Create a single Linked
10 list and perform Following Operations
A. Insertion At Beginning
B. Insertion At End
C. Insertion After a particular node
D. Insertion Before a particular node
E. Insertion at specific position
F. Search a particular node
G. Return a particular node
H. Deletion at the beginning
I. Deletion at the end
J. Deletion after a particular node
K. Deletion before a particular node
L. Delete a particular node
M. Deletion at a specific position
11
1. Write a program to Reverse a singly Linked list.
2. Write a c program to check whether the created
linked list is palindrome or not
Practical -1
Theory:- This program asks the user for the initial size of the array, then allows them to input elements.
After that, it asks for the new size of the array and resizes it accordingly using realloc(). Finally, it prints out the
elements of the resized array.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int *p,n,i,newlength;
printf("enter the size of array\n");
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
if(p==NULL)
{
printf("memory allocate fail");
exit(1);
}
printf("enter the values\n");
for(i=0;i<n;i++)
scanf("%d",&p[i]);
printf("values are: \n", p[i]);
for(i=0;i<n;i++){
printf("%d ",p[i]);
printf("\nenter new size\n");
scanf("%d",&newlength);
for(i=0;i<newlength;i++){
p=realloc(p,n*sizeof(int));
printf("enter values\n");
for(i=0;i<newlength;i++)
scanf("%d",&p[i]);
printf("values are: ", p[i]);
for(i=0;i<newlength;i++)
printf("%d ",p[i]);
free(p);
}
Output:-
Theory:- This program prompts the user for the initial number of subarrays and elements in each
subarray, input the elements, then asks for the new number of subarrays and elements in each subarray.It
reallocates memory for the 2D array accordingly and prints the updated array.
#include <stdlib.h>
#include <stdio.h>
int N,M;
int count = 0;
int **A;
}
}
return 0;
}
Output:
Practical:-2
Aim-2.1:- Write a to display present date and time using c language
Theory:- This program will display the current date and time in the format YYYY-MM-DD HH:MM:SS.
#include<stdio.h>
#include<time.h>
int main()
{
time_t tm;
time(&tm);
printf("Current Date/Time = %s", ctime(&tm));
return 0;
}
Output:-
#include<stdio.h>
#define PI 3.14159
int main(){
#ifdef PI
printf("PI is defined\n");
#elif defined(SQUARE)
printf("Square is defined\n");
#else
#error "Neither PI nor SQUARE is defined"
#endif
#ifndef SQUARE
printf("Square is not defined");
#else
printf( "Square is defined" );
#endif
return 0;
}
Output:-
Practical :-3
Aim:3.1:- Write a C program that uses functions to perform the following Operations.
#include <stdio.h>
#include <conio.h>
struct complex {
float real, imag;
}a, b, c;
struct complex read(void);
void write(struct complex);
struct complex add(struct complex, struct complex);
struct complex sub(struct complex, struct complex);
struct complex mul(struct complex, struct complex);
struct complex div(struct complex, struct complex);
void main () {
clrscr();
printf("Enter the 1st complex number\n ");
a = read();
write(a);
printf("Enter the 2nd complex number\n");
b = read();
write(b);
printf("Addition\n ");
c = add(a, b);
write(c);
printf("Substraction\n ");
c = sub(a, b);
write(c);
printf("Multiplication\n");
c = mul(a, b);
write(c);
printf("Division\n");
c = div(a, b);
write(c);
getch();
}
struct complex read(void) {
struct complex t;
printf("Enter the real part\n");
scanf("%f", &t.real);
printf("Enter the imaginary part\n");
scanf("%f", &t.imag);
return t;
}
void write(struct complex a)
{
printf("Complex number is\n");
printf(" %.1f + i %.1f", a.real, a.imag);
printf("\n");
}
Aim:3.2:- Write a c program to store records of n students based on roll_no, name, gender
and 5 subject marks i) Calculate the percentage of each student using 5 subjects. ii) Display the
student list according to their percentages.
Theory:-
• The program uses a structure Student to represent each student, including their roll number,
name, gender, marks in five subjects, and percentage.
• It calculates the percentage for each student based on their marks in five subjects.
• Then, it sorts the students based on their percentages using qsort.
• Finally, it displays the student list sorted by percentage.
#include<stdio.h>
int main(){
struct student {
char firstname[50];
int roll;
char gender[6];
float total,percentage;
}s[5];
int i;
float math;
float physics;
float chemistry;
float english;
float hindi;
float percentage,total;
s[i].roll=i+1;
printf("\ndisplaying
informations:\n\n");for(i=0;i<=1;i++){
printf("\n roll number is %d\n",i+1);
printf("firstname:");
puts(s[i].firstname);
printf("marks:%.1f",s[i].total);
printf("\npercentage is %.2f\n",s[i].percentage);
printf("\n");
}
return 0;
}
Output:-
Practical:-4
Theory:- This program uses an array of structures to store the employee records. It prompts the user to enter
the details for each employee, including EMP_ID, EMP_NAME, EMP_DEPTID, EMP_PHNO, and EMP_SALARY.
Then, it sorts the records based on EMP_NAME using the qsort function from the standard library, and finally,
it displays all the employee details in sorted order.
#include<stdio.h>
int main()
{
struct employee
{
int salary,id,p_no,dept_id;
char name[100];
}s[100],s1[100];
int n,i,j,index=0;
printf("Enter number total no. of Employee=\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Employee[%d]-id=\n",i);
scanf("%d",&s[i].id);
printf("Employee[%d]-name=\n",i);
scanf("%s",s[i].name);
printf("Employee[%d]-dept id=\n",i);
scanf("%d",&s[i].dept_id);
printf("Employee[%d]-salary=\n",i);
scanf("%d",&s[i].salary);
}
for(i=0;i<=n;i++)
{
s1[i]=s[i];
printf("\nID\tNAME\tSALARY\n");
for(i=0;i<n;i++)
printf("%d\t%s\t%d\n",s[i].id,s[i].name,s[i].salary);
}
for(i=0;i<n;i++) {
for(j=0;j<n-1-i;j++) {
if(s[j].salary<s[j+1].salary) {
s[j].salary=s[j].salary ^ s[j+1].salary;
s[j+1].salary=s[j].salary ^ s[j+1].salary;
s[j].salary=s[j].salary ^ s[j+1].salary;
}
}
}
printf("Sorted By Salary is=\n\n");
printf("\nID\tNAME\tSALARY\n");
for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
if(s[i].salary==s1[j].salary)
index=j;
}
printf("%d\t%s\t%d\n",s1[index].id,s1[index].name,s1[index].salary);
}
return 0;
}
Output:-
Practical:-5
Aim:5.1:- Write a c program to implement selection Sort & Bubble sort 2. Write a C program
to reverse the elements within a given range in a sorted list.
Example :
input : 10 9 1 2 4 3 4 6 7 8 10 3 8
output: 1 2 8 7 6 4 4 3 9 10
the sorted list of given array elements is 1 2 3 4 4 6 7 8 9 10 , after reversing the elements within the
range 3 and 8 is 1 2 8 7 6 4 4 3 9 10
Theory :- This program first sorts the array using Selection Sort, then reverses the elements within the
range [3, 8] using the reverseRange function, and finally prints the sorted and reversed array.
#include <stdio.h>
#include <stdbool.h>
void swap(int *xp, int *yp){
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(int arr[], int n){
int i, j;
bool swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
swapped = true;
}
}
if (swapped == false)
break;
}
}
void selectionSort(int arr[], int n){
int i, j, min_idx;
for (i = 0; i < n-1; i++){
min_idx = i;
for (j = i+1; j < n; j++) if
(arr[j] < arr[min_idx])
min_idx = j;
if(min_idx != i)
swap(&arr[min_idx], &arr[i]);
}
}
void printArray(int arr[], int size){
int i;
Practical:-6
Aim:6.1:- 1. Write a c program to implement Insertion sort & Quick sort
Theory:- Insertion Sort: Builds the sorted array gradually, inserting each element into its
correct position. Quick Sort: Efficiently partitions the array based on a chosen pivot element,
recursively sorting subarrays.
#include<stdio.h>
void swap(int* p1,int* p2){
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
void insertionShort(int arr[],int n){
int i, key,j;
for(i=1;i<n;i++){
key=arr[i];
j=i-1;
while (j>=0 && arr[j]>key){
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
}
}
int partition(int arr[] , int low,int high){
int pivot = arr[high];
int j,i =(low-1);
for( j=low;j<=high;j++){
if(arr[j]<pivot){
i++;
swap(&arr[i],&arr[j]);
}
}
swap(&arr[i+1], &arr[high]);
return (i+1);
}
void quickSort(int arr[],int low, int high){
if(low<high){
int pi= partition (arr,low,high);
quickSort(arr,low,pi-1);
quickSort(arr,pi+1,high);
}
}
void printArray(int arr[],int n){
int i;
for(i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
}
int main(){
int arr[7],arr1[7],M,N,i;
printf("Enter the size for quick sort:");
scanf("%d",&N);
for(i=0;i<N;i++){
scanf("%d",&arr[i]);
}
printf("%d\n",arr[i]);
Output:
Aim:6.2:- Write a c program to sort the given n integers and perform following operations i) Find the
products of every two odd position elements ii) Find the sum of every two even position elements
Explanation:
Input : 9 1 9 8 3 5 4 7 2 6
Output: 3 15 35 63 6 10 14
The sorted list of given input is 1 2 3 4 5 6 7 8 9, the product of alternative odd position elements is
1*3 = 3,3*5=15,5*7=35… and the sum of two even position elements 2+4 =6,4+6=10.
Theory:- This program first sorts the given integers using Quick Sort. Then, it calculates the products of
every two odd position elements and the sum of every two even position elements.
#include <stdio.h>
int main() {
int arr[] = {9, 1, 9, 8, 3, 5, 4, 7, 2, 6};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
calculateOperations(arr, n);
return 0;
}
if (i % 2 == 0) {
if (i + 1 < n) {
printf("%d ", arr[i] * arr[i + 1]);
}
} else {
if (i + 1 < n) {
printf("%d ", arr[i] + arr[i + 1]);
}
}
}
printf("\n");
}
Output:-
Practical:-7
Theory:- This program implements the Merge Sort algorithm in C. It sorts the given array of integers in
ascending order and prints the sorted array.
#include <stdio.h>
void merge(int arr[], int left, int mid, int right);
void mergeSort(int arr[], int left, int right);
int main() {
int i,arr[] = {9, 1, 9, 8, 3, 5, 4, 7, 2, 6};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
printf("Sorted array: \n");
for ( i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
arr[k] = L[i];
i++;
k++;
}
Output:-
Practical:-8
Aim:8.1:- 1. Write a c program to sort in ascending order and reverse the individual row
elements of an mxn matrix input : 3 4 1 4 2 3 7 8 10 9 6 3 5 2 output: 4 3 2 1 10 9 8 7 6 5 3 2
Theory:- This program takes an mxn matrix as input, sorts each row in ascending order, and thenreverses
each row individually.
#include <stdio.h>
int main() {
int m, n,i,j;
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &m, &n);
int matrix[m][n];
reverseRow(matrix[i], n);
}
printf("Modified matrix:\n");
for (i = 0; i < m; i++) {
for ( j = 0; j < n; j++) {
return 0;
}
} Output:-
Aim:8.2:- . Write a c program to sort elements in row wise and print the elements of matrix in Colum
major order Input: 3 4 1 4 2 3 7 8 10 9 6 3 5 2 Output: 1 7 2 2 8 3 3 9 5 4 10 6 Explanation: The sorted
matrix according to the conditions is 1 2 3 4 7 8 9 10 2 3 5 6 after sorting matrix the elements as
to be printed in column major order 1 7 2 2 8 3 3 9 5 4 10 6
Theory:- This program first sorts the elements in each row of the matrix in ascending order. Then,
it prints the elements of the matrix in column-major order after sorting.
#include <stdio.h>
int main() {
int m, n;
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d %d", &m, &n);
int matrix[m][n],i,j;
return 0;
}
Output:-
Practical:-9
Aim:9.1:- . Write a c program to perform linear Search.
Theory:- This program prompts the user to input the number of elements in the array, then reads the
elements of the array. It also takes an element from the user to search for in the array. It then performs a
linear search on the array and prints whether the element is found or not, along with its index if found.
#include <stdio.h>
int linearSearch(int arr[], int n, int key) {
int i;
for (i = 0; i < n; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
int main() {
int n, i,key;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers:\n", n);for
( i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &key);
int index = linearSearch(arr, n, key);
if (index != -1) {
printf("Element %d found at index %d.\n", key, index);
} else {
printf("Element %d not found in the array.\n", key);
}
return 0;
}
Output:-
Practical :-10
Aim:10.1:- Write a c program to Create a single Linked list and perform Following Operations
A. Insertion At Beginning
B. Insertion At End
C. Insertion After a particular node
D. Insertion Before a particular node
E. Insertion at specific position
F. Search a particular node
G. Return a particular node
H. Deletion at the beginning
I. Deletion at the end
J. Deletion after a particular node
K. Deletion before a particular node
L. Delete a particular node
M. Deletion at a specific position
Theory:- This code covers all the mentioned operations on a singly linked list: insertion at the beginning,
insertion at the end, insertion after a particular node, insertion before a particular node, search for a particular
node, deletion at the beginning, deletion at the end, deletion of a particular node, and display of the linked list.
#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 insertAtBeginning(struct Node** headRef, int data) {
struct Node* newNode = createNode(data);
newNode->next = *headRef;
*headRef = newNode;
}
void insertAtEnd(struct Node** headRef, int data) {
struct Node* newNode = createNode(data);
if (*headRef == NULL) {
*headRef = newNode;
return;
}
struct Node* current = *headRef;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
void insertAfterNode(struct Node* prevNode, int data) {if
(prevNode == NULL) {
printf("Previous node cannot be NULL.\n");
return;
}
struct Node* newNode = createNode(data);
newNode->next = prevNode->next; prevNode-
>next = newNode;
}
void insertBeforeNode(struct Node** headRef, struct Node* nextNode, int data) {if
(*headRef == NULL || nextNode == NULL) {
printf("Head node or next node cannot be NULL.\n");
return;
}
if (*headRef == nextNode) {
insertAtBeginning(headRef, data);
return;
}
struct Node* current = *headRef; while
(current->next != nextNode) {
current = current->next;
}
struct Node* newNode = createNode(data);
newNode->next = current->next;
current->next = newNode;
}
struct Node* searchNode(struct Node* head, int key) {
struct Node* current = head;
while (current != NULL && current->data != key) {
current = current->next;
}
return current;
}
void deleteAtBeginning(struct Node** headRef) {if
(*headRef == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = *headRef;
*headRef = (*headRef)->next;
free(temp);
}
void deleteAtEnd(struct Node** headRef) {if
(*headRef == NULL) {
printf("List is empty.\n");
return;
}
if ((*headRef)->next == NULL) {
free(*headRef);
*headRef = NULL;
return;
}
struct Node* current = *headRef;
struct Node* prev = NULL;
while (current->next != NULL) {
prev = current;
current = current->next;
}
free(current);
prev->next = NULL;
}
void deleteNode(struct Node** headRef, struct Node* delNode) {if
(*headRef == NULL || delNode == NULL) {
printf("Head node or delete node cannot be NULL.\n");
return;
}
if (*headRef == delNode) {
*headRef = delNode->next;
free(delNode);
return;
}
struct Node* current = *headRef;
struct Node* prev = NULL;
while (current != delNode && current != NULL) {prev
= current;
current = current->next;
}
if (current == NULL) {
printf("Node not found in the list.\n");
return;
}
prev->next = delNode->next;
free(delNode);
}
void display(struct Node* head) {if
(head == NULL) {
printf("Linked list is empty.\n");
return;
}
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 1);
insertAtEnd(&head, 3);
insertAfterNode(head, 1);
insertBeforeNode(&head, head->next, 2);
printf("Linked list after insertions: ");
display(head);
struct Node* searchedNode = searchNode(head, 2);if
(searchedNode != NULL) {
printf("Node with data %d found.\n", searchedNode->data);
}
deleteAtBeginning(&head);
deleteAtEnd(&head);
deleteNode(&head, head);
printf("Linked list after deletions: ");
display(head);
return 0;
}
Output:-
Practical:-11
Aim:11.1:- Write a program to Reverse a singly Linked list
Theory:- This program demonstrates the reversal of a singly linked list. Initially, it inserts three nodes at the
beginning of the list. Then, it displays the original list, reverses the list using the reverseList function, and
finally displays the reversed list.
#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 insertAtBeginning(struct Node** headRef, int data) {
struct Node* newNode = createNode(data);
newNode->next = *headRef;
*headRef = newNode;
}
void reverseList(struct Node** headRef) {
struct Node* prevNode = NULL;
struct Node* currentNode = *headRef;
struct Node* nextNode = NULL;
while (currentNode != NULL) {
nextNode = currentNode->next;
currentNode->next = prevNode;
prevNode = currentNode;
currentNode = nextNode;
}
*headRef = prevNode;
}
void display(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 1);
printf("Original list: ");
display(head);
reverseList(&head);
printf("Reversed list: ");
display(head);
return 0;
}
Output:-
Aim:11.2:- Write a c program to check whether the created linked list is palindrome or not
Theory:- This program checks whether the linked list is a palindrome or not. It first constructs a linked list
and then checks whether it is a palindrome using the isPalindrome function. The reverseList
function is used to reverse the second half of the linked list for comparison.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.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 insertAtBeginning(struct Node** headRef, int data) {
struct Node* newNode = createNode(data);
newNode->next = *headRef;
*headRef = newNode;
}
struct Node* reverseList(struct Node* head) {
struct Node *prev = NULL, *current = head, *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current; current
= next;
}
return prev;
}
bool isPalindrome(struct Node* head) {
if (head == NULL || head->next == NULL) {
return true;
}
struct Node *slow = head, *fast = head;
while (fast->next != NULL && fast->next->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
struct Node* secondHalf = reverseList(slow->next);
struct Node* firstHalf = head;
while (secondHalf != NULL) {
if (firstHalf->data != secondHalf->data) {
return false;
}
firstHalf = firstHalf->next;
secondHalf = secondHalf->next;
}
return true;
}
void display(struct Node* head) {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 1);
printf("Original list: ");
display(head);
if (isPalindrome(head)) {
printf("The linked list is a palindrome.\n");
} else {
printf("The linked list is not a palindrome.\n");
}
return 0;
}
Output:-
Practical:-12
Aim:12:- Write a c program to Create a Circular Linked list and perform Following Operations
A. Insertion at Beginning
B. Insertion At End
C. Insertion After a particular node
D. Insertion Before a particular node
E. Insertion at specific position
F. Search a particular node
G. Return a particular node
H. Deletion at the beginning
I. Deletion at the end
J. Deletion after a particular node
K. Deletion before a particular node
L. Delete a particular node
M. Deletion at a specific position
Theory:- A circular linked list is a linked list where the last node points back to the first node, forming a
circular structure. In contrast to a traditional linked list where the last node points to NULL, in a circular
linked list, the next pointer of the last node points back to the head of the list. This creates a loop-like
structure where traversal can start from any node and reach all other nodes by following the next pointers.
#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 insertAtBeginning(struct Node** headRef, int data) {
struct Node* newNode = createNode(data);
if (*headRef == NULL) {
newNode->next = newNode;
*headRef = newNode;
} else {
struct Node* last = *headRef; while
(last->next != *headRef) {
last = last->next;
}
newNode->next = *headRef;
last->next = newNode;
*headRef = newNode;
}
}
Practical:-13
Aim:13:- Write a c program to Create a Circular single Linked list and perform Following Operations
A. Insertion After a particular node
B. Insertion Before a particular node
C. Search a particular node
D. Return a particular node
E. Deletion before a particular node
F. Delete a particular node.
Theory:- This program demonstrates the operations of a circular linked list. It creates a circular linkedlist
and performs insertion after a particular node, insertion before a particular node, searching for a
particular node, returning a particular node, deletion before a particular node, and deletion of a particular
node. Finally, it displays the circular linked list after each operation.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = malloc(sizeof(struct Node));if
(newNode == NULL) {
printf("Memory allocation failed.\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertAfterNode(struct Node* prevNode, int data) {if
(prevNode == NULL) {
printf("Previous node cannot be NULL.\n");
return;
}
struct Node* newNode = createNode(data);
newNode->next = prevNode->next; prevNode-
>next = newNode;
}
void insertBeforeNode(struct Node* head, struct Node* nextNode, int data) {if
(head == NULL || nextNode == NULL) {
printf("Head node or next node cannot be NULL.\n");
return;
}
struct Node* current = head;
while (current->next != nextNode && current->next != head) {
current = current->next;
}
struct Node* newNode = createNode(data);
newNode->next = current->next;
current->next = newNode;
}
void deleteNode(struct Node** headRef, struct Node* delNode) {if
(*headRef == NULL || delNode == NULL) {
printf("Head node or delete node cannot be NULL.\n");
return;
}
struct Node* current = *headRef;
while (current->next != delNode && current->next != *headRef) {
current = current->next;
}
current->next = delNode->next;
free(delNode);
}
void display(struct Node* head) {if
(head == NULL) {
printf("Circular linked list is empty.\n");
return;
}
struct Node* current = head;do
{
printf("%d ", current->data);
current = current->next;
} while (current != head);
printf("\n");
}
int main() {
struct Node* head = createNode(1);
head->next = head;
insertAfterNode(head, 2);
insertAfterNode(head, 3);
printf("Circular linked list after insertion after a particular node: ");
display(head);
insertBeforeNode(head, head->next, 4);
insertBeforeNode(head, head->next->next, 5);
printf("Circular linked list after insertion before a particular node: ");
display(head);
deleteNode(&head, head->next);
printf("Circular linked list after deletion of a particular node: ");
display(head);
return 0;
}
Output:-
Practical:-14
Aim:14:- Write a c program to Create a Circular Doubly Linked list and perform Following
Operations
A. Insertion After a particular node
B. Insertion Before a particular node
C. Search a particular node
D. Return a particular node
E. Deletion before a particular node
F. Delete a particular node.
Theory:- This code provides the basic functionality for a circular doubly linked list: insertion after a
particular node, insertion before a particular node, searching for a node, deletion before a particular
node, and deletion of a particular node.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;struct
Node* prev;
};
int main() {
struct Node* head = createNode(1); head-
>next = head; // Making it circularhead-
>prev = head; // Making it circular
insertAfterNode(head, 2);
insertAfterNode(head, 3);
printf("After insertion after a particular node: ");
display(head);
insertBeforeNode(&head, head->next, 4);
insertBeforeNode(&head, head->next->next, 5);
printf("After insertion before a particular node: ");
display(head);
struct Node* searchedNode = searchNode(head, 3);if
(searchedNode != NULL) {
printf("Node with data %d found.\n", searchedNode->data);
}
deleteBeforeNode(&head, head->next->next);
printf("After deletion before a particular node: ");
display(head);
deleteNode(&head, head->next); printf("After
deletion of a particular node: ");display(head);
return 0;
}
Output:-