Data Structure Lab Manual
Data Structure Lab Manual
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:
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:
#include <iostream>
using namespace std;
// Node structure for the linked list
struct Node {
int data;
Node* next;
};
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;
}
int main() {
ListADT list;
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;
// 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);
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:
#include <iostream>
#include <queue>
using namespace std;
int main() {
int n;
// Printing array
cout << "Array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << ' ';
}
cout << endl;