Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Data Structures

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

CSD 2106

Data Structures Laboratory


BONAFIDE CERTIFICATE

This is to Certified that the bonafide record of the work done by

RRN of III-semester

B. Tech in the course CSD 2106 Data Structures

Laboratory during the year 2023-2024.

Faculty in-charge Head of the Department

Submitted for the practical examination held on

Date: Internal Examiner


INDEX

Aim Output
S. Date Name Of The & Program & Viva Total Faculty
No Eperiment Algorithm Result Marks Sign

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.
Expt No: Date:

BASIC DATA STRUCTURE CONCEPT

AIM:

To write a program using a basic concept of data structure in C++

ALGORITHM:

Step1: Start the program


Step2: Get the size and elements of the array as an input from the user
Step3: Initialize one variable min Element with first element of the array
Step4: Using for loop, compare the elements of the array with the min element
Step5: The element is said to be smallest, if the array element is less than the min element
Step6: The output is displayed
Step7: End the program

PROGRAM:

#include <iostream>
using namespace std;
int main()
{
int arr[100], count, i, min;

cout << "\n Enter Number of Elements in Array:";


cin >> count;

cout << "\n Enter " << count << " numbers:";

for(i = 0; i < count; i++){


cin >> arr[i];
}

min = arr[0];
for(i = 0; i < count; i++){
if(arr[i] < min){
min = arr[i];
}
}

cout << "\n Minimum Element:" << min;


return 0;
}

RESULT:

The above program has been executed successfully and the desired output is obtained.
Expt No: Date:

LINKED LIST

(A) Singly Linked List

AIM:

To implement singly linked list in C++

ALGORITHM:

Step1: Start the program


Step2: Define a Node structure to hold a value and a pointer to the next node
Step3: To add a node create a new node and assign its data field
Step4: Define the last node in the list has a null value in the link field, indicating the end of
. the list
Step5: Create int main() and declare the values of the nodes
Step6: The singly linked list is displayed as output
Step7: End the program

PROGRAM:

#include <iostream>
using namespace std;
class node
{
public:
int data;

node* next;
node(int d)
{
data = d;
next = NULL;
}
};
void insertAthead(node*& head, int data)
{
node* n = new node(data);
n->next = head;
head = n;
}
void print(node* head)
{
while (head != NULL)
{
cout << head->data << "->";
head = head->next;
}
}
int main()
{

node* head = NULL;


insertAthead(head, 5);
insertAthead(head, 2);
insertAthead(head, 8);
insertAthead(head, 3);
print(head);
return 0;
}

RESULT:

The above program has been executed successfully and the desired output is obtained.

(B) Doubly Linked List

AIM:
To search an element represented by doubly linked list in C++.

ALGORITHM:

Step1: Start the program


Step2: Initialize a variable, say pos, to store the position of the node containing data value X in
. the doubly linked list
Step3: Initialize a pointer, say temp, to store the head node of the doubly linked list
Step4: Iterate over the linked list and for every node, check if data value of that node is equal to
. X or not.
Step5: If found to be true, then print pos. Otherwise, print -1
Step6: End the program

PROGRAM:

#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
};
void push(Node** head_ref, int new_data)
{
Node* new_node
= (Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL) {
(*head_ref)->prev = new_node;
}
(*head_ref) = new_node;
}
int search(Node** head_ref, int x)
{
Node* temp = *head_ref;
int pos = 0;
while (temp->data != x
&& temp->next != NULL) {
pos++;
temp = temp->next;
}
if (temp->data != x)
return -1;
return (pos + 1);
}
int main()
{
Node* head = NULL;
int X = 9;
push(&head, 14);
push(&head, 9);
push(&head, 8);
push(&head, 15);
push(&head, 18);

cout << search(&head, X);


return 0;
}

RESULT:

The above program has been executed successfully and the desired output is obtained.
Expt No: Date:

STACK ADT

(A)Adding element

AIM:

To add element into the stack in C++

ALGORITHM:

Step1: Start the program


Step2: Create a stack of strings called colors
Step3: Use the push() method to add elements to the stack
Step4: We use while loop and various stack methods to print the content of the stack
Step5: The output is displayed
Step6: Since the stack is a LIFO data structure, the element inserted last is retrieved first
Step7: End the program

PROGRAM:

#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<string> colors;
colors.push("Red");
colors.push("Orange");

cout << "Stack: ";


while(!colors.empty())
{
cout << colors.top() << ", ";
colors.pop();
}

return 0;
}

RESULT:

The above program has been executed successfully and the desired output is obtained.
(B)Removing element

AIM:

To remove element from the stack in C++

ALGORITHM:

Step1: Start the program


Step2: Create a stack of strings called colors
Step3: Use the pop() method to remove an element from the stack
Step4: The output is displayed
Step5: The element at the top of the stack i.e. the element inserted last is removed
Step6: End the program

PROGRAM:

#include <iostream>
#include <stack>
using namespace std;
void display_stack(stack<string> st);
int main()
{
stack<string> colors;
colors.push("Red");
colors.push("Orange");
colors.push("Blue");
cout << "Initial Stack: ";
display_stack(colors);
colors.pop();
cout << "Final Stack: ";
display_stack(colors);
return 0;
}
void display_stack(stack<string> st)
{
while(!st.empty()) {
cout << st.top() << ", ";
st.pop();
}
cout << endl;
}

RESULT:

The above program has been executed successfully and the desired output is obtained.
Expt No: Date:

QUEUE ADT

(A)Getting the size

AIM:

To determine the size of the queue in C++

ALGORITHM:

Step1: Start the program


Step2: Created a queue of strings called languages
Step3: Declare the elements of the queue
Step4: Use the size() method to find the number of elements in the queue
Step5: The output is displayed
Step6: End the program

PROGRAM:

#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue<string> languages;
languages.push("Python");
languages.push("C++");
languages.push("Java");
int size = languages.size();
cout << "Size of the queue: " << size;
return 0;
}

RESULT:

The above program has been executed successfully and the desired output is obtained.
(B)Check if the Queue is empty

AIM:

To check whether the queue is empty or not in C++

ALGORITHM:

Step1: Start the program


Step2: Created a queue of strings called languages
Step3: Use the empty()method to determine if the queue is empty
Step4: Initially, the queue has no elements in it. So language.empty() returns true
Step5: Then the elements of the queue is initialized
Step6: Again use language.empty() to determine if the queue is empty and it returns false
Step7: End the program

PROGRAM:

#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue<string> languages;
cout << "Is the queue empty? ";
if (languages.empty()) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
cout << "Pushing elements..." << endl;
languages.push("Python");
languages.push("C++");
cout << "Is the queue empty? ";
if (languages.empty()) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}

RESULT:

The above program has been executed successfully and the desired output is obtained.
Expt No: Date:

PRIORITY QUEUE

AIM:

To declare a min-heap for priority queue in C++

ALGORITHM:

Step1: Start the program


Step2: A priority queue pq is created and pushes four elements
Step3: The methods like empty(),top(),size() and pop() are used
Step4: With the given conditions, the smallest number in the queue is displayed as top element
Step5: After using the pop() method, the top element is removed
Step6: Then the remaining elements are displayed
Step7: End the program

PROGRAM:

#include <iostream>
#include <queue>
using namespace std;
void showpq(priority_queue<int, vector<int>, greater<int> > gq)
{
priority_queue<int, vector<int>, greater<int> > p = gq;
while (!p.empty())
{
cout << '\t' << p.top();
p.pop();
}
cout << '\n';
}
int main()
{
priority_queue<int, vector<int>, greater<int> > pq;
pq.push(3);
pq.push(1);
pq.push(2);
pq.push(4);

cout << "The priority queue pq is : ";


showpq(pq);
cout<<endl;

cout<< "pq.empty() : "<<pq.empty()<<endl;

cout << "pq.size() : " << pq.size()<<endl;

cout << "pq.top() : " << pq.top()<<endl;


cout << "After using pq.pop(), our priority queue is : ";
pq.pop();
showpq(pq);
return 0;
}

RESULT:

The above program has been executed successfully and the desired output is obtained.
Expt No: Date:

SORTING AND SEARCHING

(A)Sorting an array

AIM:

To sort an array in ascending order in C++

ALGORITHM:

Step1: Start the program


Step2: Get the size of the array from the user as an input and declare an array of the given size
Step3: Take the input of all elements of the array
Step4: Use ‘for’ loop to give the condition for sorting the array
Step5: For every element check it from all the next elements to it
Step6: If the element is greater then swap that number
Step7: In the output, the array is sorted in ascending order
Step8: End the program

PROGRAM:

#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter the size of array: "; cin>>n;
int a[n];
cout<<"\nEnter the elements: ";
for(int i=0; i<n; i++) cin>>a[i];
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++) { if(a[i]>a[j])
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
cout<<"\nArray after swapping: ";
for(int i=0; i<n; i++)
cout<<a[i]<<" ";
return 0;
}

RESULT:

The above program has been executed successfully and the desired output is obtained.
(B)Searching in an array

AIM:

To search an element of an array in C++

ALGORITHM:

Step1: Start the program


Step2: Get the size of the array from the user as an input
Step3: Using ‘for’ loop get the elements for the entered size and store it in the array
Step4: Get the element to be searched from the array
Step5:Using ‘for’ loop , traverse the array and compare the element to be searched with every
. element of the array
Step6: If that element is equal to any array element then print ‘Element found at the index i’’ or
. print Element not found
Step7: End the program

PROGRAM:

#include <iostream>
using namespace std;
int main()
{
int arr[100], count, i, num;
cout << "Enter Number of Elements in Array\n";
cin >> count;
cout << "Enter " << count << " numbers \n";
for(i = 0; i < count; i++){
cin >> arr[i];
}
cout << "Enter a number to serach in Array\n";
cin >> num;
for(i = 0; i < count; i++){
if(arr[i] == num){
cout << "Element found at index " << i;
break;
}
}
if(i == count){
cout << "Element Not Present in Input Array\n";
}
return 0;
}

RESULT:

The above program has been executed successfully and the desired output is obtained.
Expt No: Date:

TREE TRAVERSAL

AIM:

To perform the different types of traversal in C++

ALGORITHM:

Step1: Start the program


Step2: The struct node is pointed to by left and right sub tree
Step3: For inorder traversal , first visit all the nodes in left subtree, then visit the root node and
. then vist all the nodes in right subtree
Step4: For preorder traversal , first visit the root node, then visit all the nodes in left subtree and
. then vist all the nodes in right subtree
Step5: For postorder traversal , first visit all the nodes in left subtree ,then vist all the nodes in
. right subtree and then visit the root node
Step6: We don't have to create the stack ourselves because recursion maintains the correct order
. for us
Step7: The output is displayed
Step8: End the program

PROGRAM:

#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
Node(int data) {
this->data = data;
left = right = NULL;
}
};
void preorderTraversal(struct Node* node) {
if (node == NULL)
return;

cout << node->data << "->";


preorderTraversal(node->left);
preorderTraversal(node->right);
}
void postorderTraversal(struct Node* node) {
if (node == NULL)
return;

postorderTraversal(node->left);
postorderTraversal(node->right);
cout << node->data << "->";
}
void inorderTraversal(struct Node* node) {
if (node == NULL)
return;

inorderTraversal(node->left);
cout << node->data << "->";
inorderTraversal(node->right);
}
int main()
{
struct Node* root = new Node(1);
root->left = new Node(12);
root->right = new Node(9);
root->left->left = new Node(5);
root->left->right = new Node(6);

cout << "Inorder traversal ";


inorderTraversal(root);

cout << "\nPreorder traversal ";


preorderTraversal(root);

cout << "\nPostorder traversal ";


postorderTraversal(root);

return 0;
}

RESULT:

The above program has been executed successfully and the desired output is obtained.
Expt No: Date:

TREE STRUCTURE – BINARY TREE

AIM:

To implement binary tree in C++

ALGORITHM:

Step1: Start the program


Step2: Create a node which is pointed to by right and left sub tree
Step3: Create a constructor to the struct node that inserts value in the data variable
Step4: Left and right child is initialized to NULL and check if the tree is empty
Step5: Use inorder traversal to print the tree
Step6: Create a root and initialize the values to the left and right of the root
Step7: The output is displayed
Step8: End the program

PROGRAM:

#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* left;
struct Node* right;
Node(int value)
{data = value;
left = NULL;
right = NULL;}
};
void Printtree(struct Node *root)
{if(root == NULL)
return;
Printtree(root -> left);
cout << root -> data << " ";
Printtree(root -> right);}
int main()
{struct Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
Printtree(root);
return 0;
}

RESULT:

The above program has been executed successfully and the desired output is obtained.
Expt No: Date:

AVL TREE

AIM:

To implement AVL tree in C++

ALGORITHM:

Step1: Start the program


Step2: Create constructor to make our AVL tree with four member variables which are key to
. store the incoming value
Step3: *leftRotate(AVLtree*t1)-This function performs the left rotation operation by taking an
. argument and returns the tree after performing the left rotation
Step4: *rightRotate(AVLtree*t1)-This function performs the right rotation operation by taking
. an argument and returns the tree after performing the right rotation
Step5: *insertNode(AVLtree*tree,int key) – This function performs the insertion in AVL trees
. by taking two arguments *tree and key
Step6: Function is called to print the tree
Step7: The output is displayed
Step8: End the program

PROGRAM:

#include<iostream>
using namespace std;
class AVLtree
{
public:
int key;
AVLtree *left;
AVLtree *right;
int height;
};
int max(int a, int b)
{
return (a > b)? a : b;
}

int getHeight(AVLtree *tree)


{
if (tree == NULL)
return 0;
return tree->height;
}
AVLtree* newNode(int key)
{
AVLtree* tree = new AVLtree();
tree->key = key;
tree->left = NULL;
tree->right = NULL;
tree->height = 1;
return(tree);
}
AVLtree *leftRotate(AVLtree *t1)
{
AVLtree *curr = t1->right;
AVLtree *t2 = curr->left;

curr->left = t1;
t1->right = t2;

t1->height = max(getHeight(t1->left),
getHeight(t1->right)) + 1;
curr->height = max(getHeight(curr->left),
getHeight(curr->right)) + 1;
return curr;
}
AVLtree *rightRotate(AVLtree *t1)
{
AVLtree *curr = t1->left;
AVLtree *t2 = curr->right;

curr->right = t1;
t1->left = t2;

t1->height = max(getHeight(t1->left),
getHeight(t1->right)) + 1;
curr->height = max(getHeight(curr->left),
getHeight(curr->right)) + 1;

return curr;
}
int getBalance(AVLtree *tree)
{
if (tree == NULL)
return 0;
return getHeight(tree->left) - getHeight(tree->right);
}
AVLtree* insertNode(AVLtree* tree, int key)
{
if (tree == NULL)
return(newNode(key));

if (key < tree->key)


tree->left = insertNode(tree->left, key);
else if (key > tree->key)
tree->right = insertNode(tree->right, key);
else
return tree;

tree->height = 1 + max(getHeight(tree->left),
getHeight(tree->right));

int balance = getBalance(tree);


if (balance > 1 && key < tree->left->key)
return rightRotate(tree);

if (balance < -1 && key > tree->right->key)


return leftRotate(tree);

if (balance > 1 && key > tree->left->key)


{
tree->left = leftRotate(tree->left);
return rightRotate(tree);
}

if (balance < -1 && key < tree->right->key)


{
tree->right = rightRotate(tree->right);
return leftRotate(tree);
}
return tree;
}
void preOrder(AVLtree *root)
{
if(root != NULL)
{
cout << root->key << " ";
preOrder(root->left);
preOrder(root->right);
}
}
int main()
{
AVLtree *tree = NULL;
tree = insertNode(tree, 10);
tree = insertNode(tree, 20);
tree = insertNode(tree, 30);
tree = insertNode(tree, 40);
tree = insertNode(tree, 50);
tree = insertNode(tree, 25);
cout << "Preorder traversal of the AVL tree is: \t";
preOrder(tree);
return 0;
}

RESULT:

The above program has been executed successfully and the desired output is obtained.
Expt No: Date:

SHORTEST PATH ALGORITHM

AIM:

To implement shortest path algorithm (Dijkstra's algorithm ) in C++

ALGORITHM:

Step1: Start the program


Step2: Create a set sptSet that keeps track of vertices included in the shortest path tree
Step3: Assign a distance value to all vertices in the input graph
Step4: Initialize all distance values as INFINITE . Assign the distance value as 0 for the source
. vertex so that it is picked first
Step5: Pick a vertex u that is not there in sptSet and has a minimum distance value. Include u to
. sptSet.
Step6: To update the distance values, iterate through all adjacent vertices
Step7: For every adjacent vertex, if the sum of the distance value of u (from source) and weight
. of edge u-v, is less than the distance value of v, then update the distance value of v.
Step8: The output is displayed
Step8: End the program

PROGRAM:

#include <iostream>
using namespace std;
#include <limits.h>
#define V 9
int minDistance(int dist[], bool sptSet[])
{
int min = INT_MAX, min_index;

for (int v = 0; v < V; v++)


if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;

return min_index;
}
void printSolution(int dist[])
{
cout << "Vertex \t Distance from Source" << endl;
for (int i = 0; i < V; i++)
cout << i << " \t\t\t\t" << dist[i] << endl;
}
void dijkstra(int graph[V][V], int src)
{
int dist[V];
bool sptSet[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v]
&& dist[u] != INT_MAX
&& dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
printSolution(dist);
}
int main()
{
int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
dijkstra(graph, 0);
return 0;
}

RESULT:

The above program has been executed successfully and the desired output is obtained.

You might also like