Data Structures
Data Structures
Data Structures
RRN of III-semester
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:
AIM:
ALGORITHM:
PROGRAM:
#include <iostream>
using namespace std;
int main()
{
int arr[100], count, i, min;
cout << "\n Enter " << count << " numbers:";
min = arr[0];
for(i = 0; i < count; i++){
if(arr[i] < min){
min = arr[i];
}
}
RESULT:
The above program has been executed successfully and the desired output is obtained.
Expt No: Date:
LINKED LIST
AIM:
ALGORITHM:
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()
{
RESULT:
The above program has been executed successfully and the desired output is obtained.
AIM:
To search an element represented by doubly linked list in C++.
ALGORITHM:
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);
RESULT:
The above program has been executed successfully and the desired output is obtained.
Expt No: Date:
STACK ADT
(A)Adding element
AIM:
ALGORITHM:
PROGRAM:
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<string> colors;
colors.push("Red");
colors.push("Orange");
return 0;
}
RESULT:
The above program has been executed successfully and the desired output is obtained.
(B)Removing element
AIM:
ALGORITHM:
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
AIM:
ALGORITHM:
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:
ALGORITHM:
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:
ALGORITHM:
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);
RESULT:
The above program has been executed successfully and the desired output is obtained.
Expt No: Date:
(A)Sorting an array
AIM:
ALGORITHM:
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:
ALGORITHM:
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:
ALGORITHM:
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;
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);
return 0;
}
RESULT:
The above program has been executed successfully and the desired output is obtained.
Expt No: Date:
AIM:
ALGORITHM:
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:
ALGORITHM:
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;
}
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));
tree->height = 1 + max(getHeight(tree->left),
getHeight(tree->right));
RESULT:
The above program has been executed successfully and the desired output is obtained.
Expt No: Date:
AIM:
ALGORITHM:
PROGRAM:
#include <iostream>
using namespace std;
#include <limits.h>
#define V 9
int minDistance(int dist[], bool sptSet[])
{
int min = INT_MAX, min_index;
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.