0% found this document useful (0 votes)
26 views

Data Structure Lab Manual

Uploaded by

shiva974053
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Data Structure Lab Manual

Uploaded by

shiva974053
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Dr.

A P J ABDUL KALAM SCHOOL OF ENGINEERING


16th, KM, Old Madras Road, Bangalore – 560049.

10ABTEC22312-DATA STRUCTURES WITH C++


LAB MANUAL

1. Write a C++ programs to implement Binary search.


#include<iostream>
using namespace std;
int rbinary_search(int list[],int key,int low,int high);
int main(){
int n,i,key,list[25],pos;
cout<<"Enter number of elements:\n";
cin>>n;
cout<<"enter"<<n<<"elements in ascending order";
for(i=0;i<n;i++)
cin>>list[i];
cout<<"Enter key to search: ";
cin>>key;
pos=rbinary_search(list,key,0,n-1);
if(pos==-1)
cout<<"element not found";
else
cout<<"Element found at index "<<pos;
}
int rbinary_search(int list[],int key,int low,int high){
int mid,pos=-1;
if(low<=high){
mid=(low+high)/2;if(key==list[mid]){
pos=mid;
return pos;
}
else if(key<list[mid])
return rbinary_search(list,key,low,mid-1);
else{
return rbinary_search(list,key,mid+1,high); }
}
return pos;
}
OUTPUT:

2. Write C++ programs to implement Merge sort using an array.


#include<iostream>
using namespace std;
void swapping(int &a, int &b) { //swap the content of
a and b
int temp;
temp = a;
a = b;
b = temp;
}
void display(int *array, int size) {
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
void merge(int *array, int l, int m, int r) {
int i, j, k, nl, nr; //size of left and right sub-arrays
nl = m-l+1; nr = r-m;
int larr[nl], rarr[nr]; //fill left and right sub-arrays
for(i = 0; i<nl; i++)
larr[i] = array[l+i];
for(j = 0; j<nr; j++)
rarr[j] = array[m+1+j];
i = 0; j = 0; k = l; //marge temp arrays to real array
while(i < nl && j<nr) {
if(larr[i] <= rarr[j]) {
array[k] = larr[i];
i++;
}else{
array[k] = rarr[j];
j++;
}
k++;
}
while(i<nl) { //extra element in left array
array[k] = larr[i];
i++; k++;
}
while(j<nr) { //extra element in right array
array[k] = rarr[j];
j++; k++;
}
}
void mergeSort(int *array, int l, int r) {
int m;
if(l < r) {
int m = l+(r-l)/2; // Sort first and second arrays
mergeSort(array, l, m);
mergeSort(array, m+1, r);
merge(array, l, m, r);
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n]; //create an array with given number of
elements
cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++) {
cin >> arr[i];
}
cout << "Array before Sorting: ";
display(arr, n);
mergeSort(arr, 0, n-1); //(n-1) for last index
cout << "Array after Sorting: ";
display(arr, n);
}
OUTPUT:

3. Write C++ programs to implement Heap sort using an array.


#include<iostream>
using namespace std;
void heapify(int arr[], int n, int i) {
int temp;
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i) {
temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}

void heapSort(int arr[], int n) {


int temp;
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i >= 0; i--) {
temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}

int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << "Given array is: " << endl;
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
heapSort(arr, n);
cout << "\nSorted array is: " << endl;
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
OUTPUT:

4. Write a C++ programs to implement the following using an array.

a) Stack ADT

#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val) {
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top]=val;
cout << "Element " << val << " pushed into stack\n"
<< endl;
}
}
void pop() {
if(top<=-1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
void display() {
if(top>=0) {
cout<<"Stack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<" ";
cout<<endl;
} else
cout<<"Stack is empty";
}
int main() {
int ch, val;
cout<<"1) Push in stack"<<endl;
cout<<"2) Pop from stack"<<endl;
cout<<"3) Display stack"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter choice: ";
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter value to be pushed:";
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
OUTPUT:
Push operation:
Displaying the stack elements:

Pop operation:

b) Queue ADT
#include <iostream>
using namespace std;
int queue[100], n = 100, front = - 1, rear = - 1;
void Insert() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow "<<endl;
else {
if (front == - 1)
front = 0;
cout<<"Insert the element in queue : ";
cin>>val;
rear++;
queue[rear] = val;
cout << "Element " << val << " inserted into queue \n" <<
endl;
}
}
void Delete() {
if (front == - 1 || front > rear) {
cout<<"Queue Underflow "<<endl;
return;
} else {
cout<<"Element deleted from queue is : "<< queue[front]
<<endl;
front++;;
}
}
void Display() {
if (front == - 1)
cout<<"Queue is empty"<<endl;
else {
cout<<"Queue elements are : ";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<" ";
cout<<endl;
}
}
int main() {
int ch;
cout<<"1) Insert element to queue"<<endl;
cout<<"2) Delete element from queue"<<endl;
cout<<"3) Display all the elements of queue"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice : ";
cin>>ch;
switch (ch) {
case 1: Insert();
break;
case 2: Delete();
break;
case 3: Display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}
OUTPUT:

Inserting elements into queue:

Displaying elements of the queue:


Popping elements from the queue:

5. Write a C++ programs to implement list ADT to perform following operations


a) Insert an element into a list.
b) Delete an element from list
c) Search for a key element in list
d) Count number of nodes in list

#include <iostream>
using namespace std;
// Node structure for the linked list
struct Node {
int data;
Node* next;
};

// Class representing the List ADT


class ListADT {
private:
Node* head;

public:
// Constructor to initialize the list
ListADT() {
head = nullptr;
}
// Function to insert an element into the list
void insert(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = head;
head = newNode;
cout << "Inserted: " << value << endl;
}

// Function to delete an element from the list


void deleteElement(int value) {
if (head == nullptr) {
cout << "List is empty. Nothing to delete." <<
endl;
return;
}

Node* temp = head;


Node* prev = nullptr;

// If the head contains the value


if (head->data == value) {
head = head->next;
delete temp;
cout << "Deleted: " << value << endl;
return;
}

// Search for the node to delete


while (temp != nullptr && temp->data != value) {
prev = temp;
temp = temp->next;
}
// If value is not found
if (temp == nullptr) {
cout << "Element not found in the list." << endl;
return;
}

// Unlink the node and delete it


prev->next = temp->next;
delete temp;
cout << "Deleted: " << value << endl;
}

// Function to search for an element in the list


bool search(int key) {
Node* temp = head;
while (temp != nullptr) {
if (temp->data == key) {
cout << "Element " << key << " found in the
list." << endl;
return true;
}
temp = temp->next;
}
cout << "Element " << key << " not found in the list."
<< endl;
return false;
}

// Function to count the number of nodes in the list


int countNodes() {
int count = 0;
Node* temp = head;
while (temp != nullptr) {
count++;
temp = temp->next;
}
return count;
}

// Function to display the list elements


void display() {
Node* temp = head;
if (temp == nullptr) {
cout << "List is empty." << endl;
return;
}
cout << "List: ";
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
};

int main() {
ListADT list;

// Insert elements into the list


list.insert(10);
list.insert(20);
list.insert(30);
list.insert(40);

// Display the list


list.display();

// Search for a key element in the list


list.search(20);

// Delete an element from the list


list.deleteElement(30);

// Display the list after deletion


list.display();

// Count the number of nodes in the list


cout << "Number of nodes in the list: " <<
list.countNodes() << endl;

return 0;
}

OUTPUT:

6. Write C++ programs that use recursive functions to traverse the given binary
tree in a) Preorder b) Inorder and c) Postorder.
#include <bits/stdc++.h>
using namespace std;

// Structure of a tree node


struct Node {
int data;
struct Node* left;
struct Node* right;
Node(int val)
{
data = val;
left = NULL;
right = NULL;
}
};

// Function for traversing tree using


// preorder postorder and inorder method
void PostPreInOrderInOneFlowRecursive(Node* root,
vector<int>& pre,
vector<int>& post,
vector<int>& in)
{

// Return if root is NULL


if (root == NULL)
return;

// Pushes the root data into the pre


// order vector
pre.push_back(root->data);

// Recursively calls for the left node


PostPreInOrderInOneFlowRecursive(
root->left, pre, post, in);

// Pushes node data into the inorder vector


in.push_back(root->data);
// Recursively calls for the right node
PostPreInOrderInOneFlowRecursive(
root->right, pre, post, in);

// Pushes the node data into the Post Order


// Vector
post.push_back(root->data);
}

// Driver Code
int main()
{
struct Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
root->right->left = new Node(6);
root->right->right = new Node(7);
root->left->left->left = new Node(8);
root->left->left->left->right
= new Node(12);
root->left->right->left = new Node(9);
root->right->right->left = new Node(10);
root->right->right->right = new Node(11);

// Declaring the vector function to store


// in, post, pre order values
vector<int> pre, post, in;

// Calling the function;


PostPreInOrderInOneFlowRecursive(
root, pre, post, in);
// Print the values of Pre order, Post order
// and In order
cout << "Pre Order : ";
for (auto i : pre) {
cout << i << " ";
}

cout << endl;


cout << "Post Order : ";
for (auto i : post) {
cout << i << " ";
}
cout << endl;
cout << "In Order : ";
for (auto i : in) {
cout << i << " ";
}
return 0;
}
OUTPUT:

7. Write C++ programs to implement the deque (double ended queue) ADT using a
doubly linked list and an array.
#include<iostream>
using namespace std;
#define SIZE 10
class dequeue {
int a[20],f,r;
public:
dequeue();
void insert_at_beg(int);
void insert_at_end(int);
void delete_fr_front();
void delete_fr_rear();
void show();
};
dequeue::dequeue() {
f=-1;
r=-1;
}
void dequeue::insert_at_end(int i) {
if(r>=SIZE-1) {
cout<<"\n Insertion is not possible, overflow!!!!";
} else {
if(f==-1) {
f++;
r++;
} else {
r=r+1;
}
a[r]=i;
cout<<"\nInserted item is"<<" "<<a[r];
}
}
void dequeue::insert_at_beg(int i) {
if(f==-1) {
f=0;
a[++r]=i;
cout<<"\n Inserted element is:"<<" "<<i;
} else if(f!=0) {
a[--f]=i;
cout<<"\n Inserted element is:"<<" "<<i;
} else {
cout<<"\n Insertion is not possible, overflow!!!";
}
}
void dequeue::delete_fr_front() {
if(f==-1) {
cout<<"Deletion is not possible::dequeue is empty";
return;
}
else {
cout<<"The deleted element is:"<<a[f];
if(f==r) {
f=r=-1;
return;
} else
f=f+1;
}
}
void dequeue::delete_fr_rear() {
if(f==-1) {
cout<<"Deletion is not possible::dequeue is empty";
return;
}
else {
cout<<"The deleted element is:"<<a[r];
if(f==r) {
f=r=-1;
} else
r=r-1;
}
}
void dequeue::show() {
if(f==-1) {
cout<<"Dequeue is empty";
} else {
for(int i=f;i<=r;i++) {
cout<<a[i]<<" ";
}
}
}
int main() {
int c,i;
dequeue d;
do { //perform switch opeartion
cout<<"\n 1.Insert at beginning";
cout<<"\n 2.Insert at end";
cout<<"\n 3.Show";
cout<<"\n 4.Deletion from front";
cout<<"\n 5.Deletion from rear";
cout<<"\n 6.Exit";
cout<<"\nEnter your choice: ";
cin>>c;
switch(c) {
case 1:
cout<<"Enter the element to be inserted: ";
cin>>i;
d.insert_at_beg(i);
break;
case 2:
cout<<"Enter the element to be inserted: ";
cin>>i;
d.insert_at_end(i);
break;
case 3:
d.show();
break;
case 4:
d.delete_fr_front();
break;
case 5:
d.delete_fr_rear();
break;
case 6:
exit(1);
break;
default:
cout<<"Invalid choice";
break;
}
} while(c!=7);
}
OUTPUT:
Insert at beginning:

Insert at the end:


Displaying the elements:

Deletion from front:

Deletion from rear:

8. Write a C++ program to implement priority queue.

#include <iostream>
#include <queue>
using namespace std;

int main() {
int n;

cout << "Enter the number of elements: ";


cin >> n;

int* arr = new int[n]; // Dynamic array allocation

// Taking input from the user


cout << "Enter " << n << " integers: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}

// Defining priority queue


priority_queue<int> pq;

// Printing array
cout << "Array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << ' ';
}
cout << endl;

// Pushing array elements into the priority queue


for (int i = 0; i < n; i++) {
pq.push(arr[i]);
}

// Printing priority queue


cout << "Priority Queue: ";
while (!pq.empty()) {
cout << pq.top() << ' ';
pq.pop();
}
cout << endl;

delete[] arr; // Freeing the dynamically allocated memory


return 0;
}
OUTPUT:

You might also like