Ds Lab Manual Rsce
Ds Lab Manual Rsce
MASTER PRACTICALLIST
Sr.no Title
1 Write a program to find out largest number from the array and also find it’s location
Write a program to traverse an array and find the sum and average
2 of data elements from an array.
6 To study and execute the Pattern matching Algorithms( Slow and Fast)
1
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
12 Understand the Queue structure and execute the insertion, deletion operation on it.
Practical No. 1
Aim: Write a program to find out largest number from the array and also find it’s location.
Software Requirement:
Turbo C7
Theory:Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
To create an array, define the data type (like int) and specify the name of the array
followed by square brackets [].
Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
This statement accesses the value of the first element [0] in myNumbers:
Example
int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);
To find out largest element from array.Following sample codes are useful.
#include <stdio.h>
int main() {
int n;
double arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
return 0;
}
Output
3
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Result: In this way, we have executed program to find largest element in an array.
Practical No. 2
Aim:
Write a program to traverse an array and find the sum and average of data elements from an array.
Software Requirement:
Turbo C7
Theory:
\* C Program to to Calculate Sum & Average of an Array *\
4
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Result: In this way, Students have executed the program to calculate sum & average of array elements.
5
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Practical No. 3
Aim: Write a Program to a) insert an element in an array b)delete an element
from an array.
Software Requirement:
Turbo C7
Theory:
Inserting or deleting an element at the of an array can be easily done. If we need to insert or remove
an element in the middle of an array, half of the items must be shifted to accommodate the new
element while maintaining the order of the other elements.
A new element can be inserted at any position of an array if there is enough memory space allocated
to accommodate the new element. Some of the elements should be shifted forward to keep the order
of the elements.
Suppose we want to add an element 93 at the 3rd position in the following array.
Initial array
After inserting all elements comes after 32 must be moved to the next location to accommodate the new
element and to keep the order of the elements as follows.
The following program demonstrates how to insert a new element at the specified position in an array.
#include <stdio.h>
6
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
int main()
{
int arr[100];
int i, item, pos, size=7;
printf("Enter 7 elements: ");
// reading array
for (i = 0; i < size; i++)
scanf("%d",&arr[i]);
// print the original array
printf("Array before insertion: ");
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
// read element to be inserted
printf("Enter the element to be inserted: ");
scanf("%d",&item);
// read position at which element is to be inserted
printf("Enter the position at which the element is to
be inserted: ");
scanf("%d",&pos);
// increase the size
size++;
// shift elements forward
for (i = size-1; i >= pos; i--)
arr[i] = arr[i - 1];
// insert item at position
arr[pos - 1] = item;
// print the updated array
printf("Array after insertion: ");
for (i = 0; i < size; i++)
7
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Enter 7 elements: 22 32 44 51 65 71 80
Array before insertion: 22 32 44 51 65 71 80
Enter the element to be inserted: 93
Enter the position at which the element is to be inserted: 3
Array after insertion: 22 32 93 44 51 65 71 80
Deleting an element from an array
Suppose we want to delete element 44 at the 3rd position in the following array.
Initial array
After deletion, all elements coming after 44 must be moved to the previous location to fill the free space and to
keep the order of the elements as follows.
The following program demonstrates how to delete an element at the specified position in an array.
#include<stdio.h>
int main()
8
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
{
int key, i, pos = -1, size=5;
int arr[5] = {1, 20, 5, 78, 30};
printf("Array before deletion: ");
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
printf("Enter element to delete: ");
scanf("%d",&key);
// traverse the array
// if any element matches the key, store its position
for(i = 0; i < size; i++)
{
if(arr[i] == key)
{
pos = i;
break;
}
}
if(pos != -1)
{
//shift elements backwards by one position
for(i = pos; i < size - 1; i++)
arr[i] = arr[i+1];
printf("Array after deletion: ");
for(i = 0; i < size - 1; i++)
printf("%d ",arr[i]);
}
else
printf("Element Not Found\n");
return 0;
9
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
}
Output
Result: In this way, Students have executed program to insert & delete element in to stack at the specified
position into array.
10
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Practical No. 4
Aim: To study & execute linear search method.
Software Requirement:
TC7 Software
Theory:
#include <stdio.h>
// starting traversal
// checking condition
if (arr[i] == key) {
return i;
11
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
return -1;
// Driver code
int main()
int key = 4;
// calling linearSearch
// linearSearch()
if (index == -1) {
12
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
else {
return 0;
Output
Result: In this way, Student had studied the program of Linear Search method.
Practical No. 5
Aim: To study & execute binary search method.
Software Requirement:
TC7
Theory:
#include <stdio.h>
int main()
{
13
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
14
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Practical No. 6
Aim: To Study Pattern Matching algorithm in C.
Software Requirement:
Turbo C7 Software
Theory:
Pattern Matching is widely used in computer science and many other fields. Pattern
Matching algorithms are used to search for patterns within a larger text or data set.
One of the most popular algorithms for pattern matching is the Boyer-
Moore algorithm, which was first published in 1977. In this article, we will discuss
Pattern Matching algorithms in C and how they work.
15
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
The Naive Pattern Matching algorithm is an improvement over the Brute Force
algorithm. It avoids unnecessary comparisons by skipping some positions in the text.
The algorithm starts comparing the pattern with the text at the first position. If the
characters match, it moves to the next position and repeats the comparison. If the
characters do not match, the algorithm moves to the next position in the text and
compares the pattern with the text again. The time complexity of the Naive
algorithm is also O(MXN), but it is faster than the Brute Force algorithm in most
cases.
Knuth-Morris-Pratt Algorithm:
The Knuth-Morris-Pratt (KMP) algorithm is a more advanced Pattern Matching
algorithm. It is based on the observation that when a mismatch occurs, some
information about the text and the pattern can be used to avoid unnecessary
comparisons. The algorithm precomputes a table that contains information about the
pattern. The table determines how many characters of the pattern can be skipped
when a mismatch occurs. The Time Complexity of the KMP algorithm is O(M+N).
The Boyer-Moore algorithm has two main components: the bad character rule and
the good suffix rule. The bad character rule works by comparing the character in the
pattern with the corresponding character in the data or text. If the characters do not
match, the algorithm moves the pattern to the right until it finds a character that
matches. The good suffix rule compares the suffix of the pattern with the
corresponding suffix of the data or text. If the suffixes do not match, the algorithm
moves the pattern to the right until it finds a matching suffix.
The Boyer-Moore algorithm is known for its efficiency and is widely used in many
applications. It is considered one of the fastest pattern matching algorithms
available.
In this example, we first initialize the array to -1 for all characters. We then iterate
through the pattern and update the array with the last occurrence of each character
in the pattern.
Next, we can implement the good suffix rule. We can use an array to store the length
of the longest suffix of the pattern that matches a suffix of the data or text. This
array can be used to determine how far we need to move the pattern to the right.
Result:
In this way, students have studied pattern matching algorithm in C.
17
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Practical No.7
Aim: To Study & Execute bubble sort program in C.
Software Requirement:
TC7 software
Theory:
Bubble sort is a simple and intuitive sorting algorithm. It repeatedly swaps adjacent
elements if they are in the wrong order until the array is sorted. In this algorithm, the
largest element "bubbles up" to the end of the array in each iteration. Bubble sort is
inefficient for large data sets, but it is useful for educational purposes and small data
sets. In this article, we will implement the bubble sort algorithm in C programming
language.
The first step is to define the bubble sort function. This function takes an integer
array and the size of the array as its parameters. The function returns nothing as it
modifies the original array.
1. #include <stdio.h>
2.
3. void bubble_sort(int arr[], int n) {
4. int i, j;
5. for (i = 0; i < n - 1; i++) {
6. for (j = 0; j < n - i - 1; j++) {
7. if (arr[j] > arr[j + 1]) {
8. int temp = arr[j];
9. arr[j] = arr[j + 1];
10. arr[j + 1] = temp;
11. }
12. }
18
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
13. }
14. }
15. int main() {
16. int arr[] = {64, 34, 25, 12, 22, 11, 90};
17. int n = sizeof(arr) / sizeof(arr[0]);
18. bubble_sort(arr, n);
19. printf("Sorted array: ");
20. for (int i = 0; i < n; i++) {
21. printf("%d ", arr[i]);
22. }
23. return 0;
24. }
Result:
In this we have studied Bubble Sort algorithm & executed Bubble Sort Program in C.
19
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Practical No.8
Aim: study and implement various operations on singly linked list
(a) Traversing the linked list.
(b) Insert a node at the front of the linked list.
(c) Delete a last node of the linked list.
(d) Searching a Linked list.
Theory:
A Linked List is a linear data structure that consists of two parts: one is the data part
and the other is the address part. In this article, all the common operations of a singly
linked list are discussed in one menu-driven program.
Operations to be performed :
createList(): To create the list with the ‘n’ number of nodes initially as defined by the
user.
traverse(): To see the contents of the linked list, it is necessary to traverse the given
linked list. The given traverse() function traverses and prints the content of the linked
list.
insertAtFront(): This function simply inserts an element at the front/beginning of the
linked list.
insertAtEnd(): This function inserts an element at the end of the linked list.
insertAtPosition(): This function inserts an element at a specified position in the
linked list.
deleteFirst(): This function simply deletes an element from the front/beginning of the
linked list.
deleteEnd(): This function simply deletes an element from the end of the linked list.
deletePosition(): This function deletes an element from a specified position in the
linked list.
maximum(): This function finds the maximum element in a linked list.
mean(): This function finds the mean of the elements in a linked list.
20
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
sort(): This function sorts the given linked list in ascending order.
reverseLL(): This function reverses the given linked list.
display(): This function displays the linked list.
search(): This function searches for an element user wants to search.
Below is the implementation of the above operations:
C
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
};
21
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
void createList()
if (start == NULL) {
int n;
scanf("%d", &n);
if (n != 0) {
int data;
start = newnode;
temp = start;
scanf("%d", &data);
start->info = data;
22
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
temp->link = newnode;
scanf("%d", &data);
newnode->info = data;
temp = temp->link;
else
23
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
void traverse()
// List is empty
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
temp = temp->link;
24
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
void insertAtFront()
int data;
scanf("%d", &data);
temp->info = data;
// assigned to start
temp->link = start;
25
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
start = temp;
void insertAtEnd()
int data;
scanf("%d", &data);
// Changes links
26
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
temp->link = 0;
temp->info = data;
head = start;
head = head->link;
head->link = temp;
void insertAtPosition()
27
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
// Change Links
temp = start;
newnode->info = data;
newnode->link = 0;
temp = temp->link;
i++;
newnode->link = temp->link;
temp->link = newnode;
28
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
void deleteFirst()
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
start = start->link;
free(temp);
void deleteEnd()
29
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
if (start == NULL)
printf("\nList is Empty\n");
else {
temp = start;
while (temp->link != 0) {
prevnode = temp;
temp = temp->link;
free(temp);
prevnode->link = 0;
void deletePosition()
30
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
int i = 1, pos;
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
// Position to be deleted
scanf("%d", &pos);
temp = start;
31
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
temp = temp->link;
i++;
// Change Links
position = temp->link;
temp->link = position->link;
// Free memory
free(position);
32
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
void maximum()
int a[10];
int i;
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
temp = start;
// maximum element
33
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
// element
max = temp->info;
temp = temp->link;
"is : %d ",
max);
void mean()
34
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
int a[10];
int i;
// If LL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
temp = start;
// element in the LL
float m;
35
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
// Traverse the LL
temp = temp->link;
count++;
m = sum / count;
36
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
// in ascending order
void sort()
int temp;
// If LL is empty
if (start == NULL) {
return;
// Else
else {
37
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
// Traverse the LL
index = current->link;
// element
// at current
temp = current->info;
current->info = index->info;
index->info = temp;
index = index->link;
38
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
current = current->link;
void reverseLL()
t1 = t2 = NULL;
// If LL is empty
if (start == NULL)
printf("List is empty\n");
39
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
// Else
else {
// Traverse the LL
// reversing of points
t2 = start->link;
start->link = t1;
t1 = start;
start = t2;
start = t1;
temp = start;
40
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
"list is : ");
// Print the LL
temp = temp->link;
void search()
41
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
if (start == NULL) {
else {
int key;
scanf("%d", &key);
// checking by traversing
if (tr->info == key) {
found = 1;
break;
42
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
else {
tr = tr->link;
if (found == 1) {
printf(
key);
else {
"list.\n",
key);
43
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
// Driver Code
int main()
createList();
int choice;
while (1) {
" starting\n");
" end\n");
44
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
"any position\n");
"first element\n");
"last element\n");
"the elements\n");
"linked list\n");
printf("\t13 To exit\n");
45
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
scanf("%d", &choice);
switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
46
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
maximum();
break;
case 9:
mean();
break;
case 10:
sort();
break;
case 11:
47
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
reverseLL();
break;
case 12:
search();
break;
case 13:
exit(1);
break;
default:
printf("Incorrect Choice\n");
return 0;
Output:
Menu:
48
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
49
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
51
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Result :
52
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Practical No.9
Aim: To study optimization techniques using OR-tools.
Theory:
Inserting a new node in a doubly linked list is very similar to inserting new
node in linked list. There is a little extra work required to maintain the link of the
previous node. A node can be inserted in a Doubly Linked List in four ways:
At the front of the DLL.
In between two nodes
After a given node.
Before a given node.
At the end of the DLL.
Add a node at the front in a Doubly Linked List:
The new node is always added before the head of the given Linked List. The task can
be performed by using the following 5 steps:
1. Firstly, allocate a new node (say new_node).
2. Now put the required data in the new node.
3. Make the next of new_node point to the current head of the doubly linked list.
53
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Below is the implementation of the 5 steps to insert a node at the front of the linked list:
C
C++
Java
Python3
C#
Javascript
// 1. allocate node
54
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
new_node->data = new_data;
new_node->next = (*head_ref);
new_node->prev = NULL;
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
Below is the implementation of the 7 steps to insert a node after a given node in the
linked list
56
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
if (prev_node == NULL) {
return;
new_node->data = new_data;
new_node->next = prev_node->next;
57
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
prev_node->next = new_node;
new_node->prev = prev_node;
if (new_node->next != NULL)
new_node->next->prev = new_node;
If the previous node of the new_node is not NULL, then set the next pointer of this
previous node as new_node.
Else, if the prev of new_node is NULL, it will be the new head node.
Illustration:
See the below illustration where ‘B‘ is being inserted before ‘C‘.
if (next_node == NULL) {
return;
59
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
new_node->data = new_data;
new_node->prev = next_node->prev;
next_node->prev = new_node;
new_node->next = next_node;
60
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
if (new_node->prev != NULL)
new_node->prev->next = new_node;
else
head = new_node;
Result:
In this way we have studied various operations on doubly link list.
61
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Practical No .10
Aim: To study and implement following operations on the circular linked list.
(a) Insert a node at the end of the linked list.
(b) Insert a node before specified position.
(c) Delete a first node of the linked list.
(d) Delete a node after specified position.
Theory:
In a circular Singly linked list, the last node of the list contains a pointer to the first
node of the list. We can have circular singly linked list as well as circular doubly
linked list.
We traverse a circular singly linked list until we reach the same node where we
started. The circular singly liked list has no beginning and no ending. There is no null
value present in the next part of any of the nodes.
Circular linked list are mostly used in task maintenance in operating systems. There
are many examples where circular linked list are being used in computer science
including browser surfing where a record of pages visited in the past by the user, is
maintained in the form of circular linked lists and can be accessed again on clicking
the previous button.
62
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
However, due to the fact that we are considering circular linked list in the memory
therefore the last node of the list contains the address of the first node of the list.
We can also have more than one number of linked list in the memory with the
different start pointers pointing to the different start nodes in the list. The last node
is identified by its next part which contains the address of the start node of the list.
We must be able to identify the last node of any linked list so that we can find out
the number of iterations which need to be performed while traversing the list.
63
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
SN Operation Description
1 Insertion at beginning Adding a node into circular singly linked list at the beginning.
2 Insertion at the end Adding a node into circular singly linked list at the end.
S Operation Description
N
1 Deletion at Removing the node from circular singly linked list at the beginning.
beginning
2 Deletion at the Removing the node from circular singly linked list at the end.
end
3 Searching Compare each element of the node with the given item and return the
location at which the item is present in the list otherwise return null.
4 Traversing Visiting each element of the list at least once in order to perform some
specific operation.
1. #include<stdio.h>
2. #include<stdlib.h>
3. struct node
4. {
64
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
5. int data;
6. struct node *next;
7. };
8. struct node *head;
9.
10.void beginsert ();
11.void lastinsert ();
12.void randominsert();
13.void begin_delete();
14.void last_delete();
15.void random_delete();
16.void display();
17.void search();
18.void main ()
19.{
20. int choice =0;
21. while(choice != 7)
22. {
23. printf("\n*********Main Menu*********\n");
24. printf("\nChoose one option from the following list ...\n");
25. printf("\n===============================================\
n");
26. printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete from l
ast\n5.Search for an element\n6.Show\n7.Exit\n");
27. printf("\nEnter your choice?\n");
28. scanf("\n%d",&choice);
29. switch(choice)
30. {
31. case 1:
32. beginsert();
65
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
33. break;
34. case 2:
35. lastinsert();
36. break;
37. case 3:
38. begin_delete();
39. break;
40. case 4:
41. last_delete();
42. break;
43. case 5:
44. search();
45. break;
46. case 6:
47. display();
48. break;
49. case 7:
50. exit(0);
51. break;
52. default:
53. printf("Please enter valid choice..");
54. }
55. }
56.}
57.void beginsert()
58.{
59. struct node *ptr,*temp;
60. int item;
61. ptr = (struct node *)malloc(sizeof(struct node));
62. if(ptr == NULL)
66
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
63. {
64. printf("\nOVERFLOW");
65. }
66. else
67. {
68. printf("\nEnter the node data?");
69. scanf("%d",&item);
70. ptr -> data = item;
71. if(head == NULL)
72. {
73. head = ptr;
74. ptr -> next = head;
75. }
76. else
77. {
78. temp = head;
79. while(temp->next != head)
80. temp = temp->next;
81. ptr->next = head;
82. temp -> next = ptr;
83. head = ptr;
84. }
85. printf("\nnode inserted\n");
86. }
87.
88.}
89.void lastinsert()
90.{
91. struct node *ptr,*temp;
92. int item;
67
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
123.
124. void begin_delete()
125. {
126. struct node *ptr;
127. if(head == NULL)
128. {
129. printf("\nUNDERFLOW");
130. }
131. else if(head->next == head)
132. {
133. head = NULL;
134. free(head);
135. printf("\nnode deleted\n");
136. }
137.
138. else
139. { ptr = head;
140. while(ptr -> next != head)
141. ptr = ptr -> next;
142. ptr->next = head->next;
143. free(head);
144. head = ptr->next;
145. printf("\nnode deleted\n");
146.
147. }
148. }
149. void last_delete()
150. {
151. struct node *ptr, *preptr;
152. if(head==NULL)
69
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
153. {
154. printf("\nUNDERFLOW");
155. }
156. else if (head ->next == head)
157. {
158. head = NULL;
159. free(head);
160. printf("\nnode deleted\n");
161.
162. }
163. else
164. {
165. ptr = head;
166. while(ptr ->next != head)
167. {
168. preptr=ptr;
169. ptr = ptr->next;
170. }
171. preptr->next = ptr -> next;
172. free(ptr);
173. printf("\nnode deleted\n");
174.
175. }
176. }
177.
178. void search()
179. {
180. struct node *ptr;
181. int item,i=0,flag=1;
182. ptr = head;
70
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
213. }
214. if(flag != 0)
215. {
216. printf("Item not found\n");
217. }
218. }
219.
220. }
221.
222. void display()
223. {
224. struct node *ptr;
225. ptr=head;
226. if(head == NULL)
227. {
228. printf("\nnothing to print");
229. }
230. else
231. {
232. printf("\n printing values ... \n");
233.
234. while(ptr -> next != head)
235. {
236.
237. printf("%d\n", ptr -> data);
238. ptr = ptr -> next;
239. }
240. printf("%d\n", ptr -> data);
241. }
242.
72
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
243. }
Output:
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Enter Data?20
node inserted
*********Main Menu*********
73
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
Enter Data?30
node inserted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
node deleted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
74
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
6.Show
7.Exit
node deleted
*********Main Menu*********
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
*********Main Menu*********
75
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
===============================================
1.Insert in begining
2.Insert at last
3.Delete from Beginning
4.Delete from last
5.Search for an element
6.Show
7.Exit
76
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Practical No 11
Aim: Understand the stack structure and execute the push, pop operation on it.
Theory:
The Last-In-First-Out (LIFO) concept is used by Stacks, a type of linear data structure.
The Queue has two endpoints, but the Stack only has one (front and rear). It only has
one pointer, top pointer, which points to the stack's topmost member. When an
element is added to the stack, it is always added to the top, and it can only be
removed from the stack. To put it another way, a stack is a container that allows
insertion and deletion from the end known as the stack's top.
Due to the possibility of understating inventory value, LIFO is not a reliable indicator
of ending inventory value. Due to increasing COGS, LIFO leads to reduced net
income (and taxes). However, under LIFO during inflation, there are fewer inventory
write-downs. Results from average cost are in the middle of FIFO and LIFO.
77
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Although the income from each widget is the same because of its fixed sales price,
the cost of each widget is determined by the inventory method used. The last item
received is the first item sold according to the LIFO mechanism. Accordingly, the
$200 widgets were the first to sell. Following that, the business sold two more of the
$100 widgets. Five widgets are priced at $200 each, and two are priced at $100, for
a total cost of $1,200 for the widgets using the LIFO approach. As opposed to FIFO,
which sells the $200 widgets last, the $100 widgets are sold first. As a result, the
price of the sold widgets, which breaks down to five at $100 and two at $200, will be
reported as $900. That's why LIFO generates bigger profits during price-increasing
periods.
Because of this, LIFO increases expenses during times of price growth and decreases
net revenue, which also lowers taxable income. Similar to this, LIFO reduces
expenses and raises net income during periods of declining prices, which also raises
taxable revenue.
Stack Vs Array
Stack: A stack is a type of linear data structure whose components may only be
added to or removed from the top side of the list. The Last in First out (LIFO)
principle governs stacks, meaning that the element put last is the first element to be
removed. Push operations and pop operations are the terms used to describe the
addition and removal of elements from stacks, respectively. A pointer named top is
used in stack to maintain track of the last piece that is currently present in the list.
78
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
push()
It stacks up a new item. A stack overflow circumstance is when the stack is
completely full.
push()
It stacks up a new item. A stack overflow circumstance is when the stack is
completely full.
1. begin
2. if stack is full
3. return
4. endif
79
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
5. else
6. increment top
7. stack[top] assign value
8. end else
9. end procedure
pop()
It takes something out of the stack. In the opposite sequence from which they were
pushed, the things are popped. The condition is referred to as an underflow if the
stack is empty.
1. begin
2. if stack is empty
3. return
4. endif
5. else
6. store value of stack[top]
7. decrement top
8. return value
9. end else
10. end procedure
1. #include <stdio.h>
2. #include <stdlib.h>
3. #define SIZE 4
4. int top = -1, inp_array[SIZE];
80
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
5. void push();
6. void pop();
7. void show();
8. int main()
9. {
10. int choice;
11. while (1)
12. {
13. printf("\nPerform operations on the stack:");
14. printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.End");
15. printf("\n\nEnter the choice: ");
16. scanf("%d", &choice);
17. switch (choice)
18. {
19. case 1:
20. push();
21. break;
22. case 2:
23. pop();
24. break;
25. case 3:
26. show();
27. break;
28. case 4:
29. exit(0);
30. default:
31. printf("\nInvalid choice!!");
32. }
33. }
34. }
81
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
65. {
66. printf("\nUnderflow!!");
67. }
68. else
69. {
70. printf("\nElements present in the stack: \n");
71. for (int i = top; i >= 0; --i)
72. printf("%d\n", inp_array[i]);
73. }
74. }
Output
Explanation
As we can see in the above example of push and pop operation in c, we pushed 10,
20 and 30. After successfully pushing elements into the stack we pop out 30 from
the stack and to check if the element is pop out or not, we checked it by performing
top operation on the stack, which found out to be 20.
Conclusion:
In this way we have studied Push & Pop operation on Stack
83
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Practical No 12
Aim: nderstand the Queue structure and execute the insertion, deletion operation on it.
Software:TC7 software
Theory: Queue, like Stack, is also an abstract data structure. The thing that
makes queue different from stack is that a queue is open at both its ends.
Hence, it follows FIFO (First-In-First-Out) structure, i.e. the data item inserted
first will also be accessed first. The data is inserted into the queue through
one end and deleted from it using the other end.
Representation of Queues
Similar to the stack ADT, a queue ADT can also be implemented using
arrays, linked lists, or pointers. As a small example in this tutorial, we
implement queues using a one-dimensional array.
Basic Operations
84
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
Queue uses two pointers − front and rear. The front pointer accesses the
data from the front end (helping in enqueueing) while the rear pointer
accesses data from the rear end (helping in dequeuing).
Insertion operation: enqueue()
The enqueue() is a data manipulation operation that is used to insert
elements into the stack. The following algorithm describes the enqueue()
operation in a simpler way.
Algorithm
1. START
2. Check if the queue is full.
3. If the queue is full, produce overflow error and exit.
4. If the queue is not full, increment rear pointer to point
the next empty space.
5. Add data element to the queue location, where the rear
is pointing.
6. return success.
7. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
85
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
int itemCount = 0;
bool isFull(){
return itemCount == MAX;
}
bool isEmpty(){
return itemCount == 0;
}
int removeData(){
int data = intArray[front++];
if(front == MAX) {
front = 0;
}
itemCount--;
return data;
}
void insert(int data){
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
}
intArray[++rear] = data;
itemCount++;
}
}
int main(){
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
86
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
printf("Queue: ");
while(!isEmpty()) {
int n = removeData();
printf("%d ",n);
}
}
Output
Queue: 3 5 9 1 12 15
Algorithm
1. START
2. Check if the queue is empty.
3. If the queue is empty, produce underflow error and exit.
4. If the queue is not empty, access the data where front
is pointing.
5. Increment front pointer to point to the next available
data element.
6. Return success.
7. END
Example
#include <stdio.h>
#include <string.h>
87
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
bool isFull(){
return itemCount == MAX;
}
bool isEmpty(){
return itemCount == 0;
}
void insert(int data){
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
}
intArray[++rear] = data;
itemCount++;
}
}
int removeData(){
int data = intArray[front++];
if(front == MAX) {
front = 0;
}
itemCount--;
return data;
}
int main(){
88
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
int i;
/* insert 5 items */
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
printf("Queue: ");
for(i = 0; i < MAX; i++)
printf("%d ", intArray[i]);
Algorithm
1. START
2. Return the element at the front of the queue
3. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
int peek(){
return intArray[front];
}
bool isFull(){
return itemCount == MAX;
}
void insert(int data){
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
}
intArray[++rear] = data;
90
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
itemCount++;
}
}
int main(){
int i;
/* insert 5 items */
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
printf("Queue: ");
for(i = 0; i < MAX; i++)
printf("%d ", intArray[i]);
printf("\nElement at front: %d\n",peek());
}
Output
Queue: 3 5 9 1 12 15
Element at front: 3
Algorithm
1. START
2. If the count of queue elements equals the queue size,
return true
3. Otherwise, return false
91
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
4. END
Example
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
bool isFull(){
return itemCount == MAX;
}
void insert(int data){
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
}
intArray[++rear] = data;
itemCount++;
}
}
int main(){
int i;
/* insert 5 items */
insert(3);
92
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
printf("Queue: ");
for(i = 0; i < MAX; i++)
printf("%d ", intArray[i]);
printf("\n");
if(isFull()) {
printf("Queue is full!\n");
}
}
Output
Queue: 3 5 9 1 12 15
Queue is full!
The isEmpty() operation verifies whether the stack is empty. This operation
is used to check the status of the stack with the help of top pointer.
Algorithm
1. START
2. If the count of queue elements equals zero, return true
3. Otherwise, return false
4. END
Example
C C++JavaPython
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
bool isEmpty(){
return itemCount == 0;
}
int main(){
int i;
printf("Queue: ");
for(i = 0; i < MAX; i++)
printf("%d ", intArray[i]);
printf("\n");
if(isEmpty()) {
printf("Queue is Empty!\n");
}
}
Output
Queue: 0 0 0 0 0 0
Queue is Empty!
Complete Implementation
94
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
C C++JavaPython
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 6
int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;
int peek(){
return intArray[front];
}
bool isEmpty(){
return itemCount == 0;
}
bool isFull(){
return itemCount == MAX;
}
int size(){
return itemCount;
}
void insert(int data){
if(!isFull()) {
if(rear == MAX-1) {
rear = -1;
}
intArray[++rear] = data;
itemCount++;
}
}
95
Rajarshi Shahu College Of Engineering,Buldana
Department of Computer Science and Engineering
Session 2023-24
Subject: Data Structure Lab Semester: 3rd
int removeData(){
int data = intArray[front++];
if(front == MAX) {
front = 0;
}
itemCount--;
return data;
}
int main(){
/* insert 5 items */
insert(3);
insert(5);
insert(9);
insert(1);
insert(12);
insert(15);
printf("Queue size: %d", size());
printf("\nQueue: ");
for(int i = 0; i < MAX; i++){
printf("%d ", intArray[i]);
}
if(isFull()) {
printf("\nQueue is full!");
}
}
Output
Queue size: 6
Queue: 3 5 9 1 12 15
Queue is full!
Element removed: 3
Size of Queue after deletion: 5
Element at front: 5
Conclusion:
In this way we have studied various operations on Queue data structure.
97