Advanced Data Structures Lab Updated
Advanced Data Structures Lab Updated
class STACK {
private:
vector<string> stack;
public:
void push(string item) {
cout<<"Item "<<item<<" pushed onto the STACK";
stack.push_back(item);
}
void pop() {
if (stack.empty()) {
cout<<"STACK UNDERFLOW";
return;
}
cout<<"Item "<<stack.back()<<" popped from the STACK";
stack.pop_back();
}
void display() {
if (stack.empty()) {
cout<<"STACK EMPTY";
return;
}
cout<<"STACK CONTENTS: ";
for (auto i: stack) {
cout<<i<<" ";
}
}
};
int main() {
STACK s;
int choice;
string item;
while (1) {
printf("\n\n1. Push\n2. Pop\n3. Display\n4. Exit\nEnter your choice: ");
cin>>choice;
switch (choice) {
case 1:
cout<<"Enter the item to be pushed: ";
cin>>item;
s.push(item);
break;
case 2:
s.pop();
break;
case 3:
s.display();
break;
case 4:
exit(0);
default:
cout<<"Invalid choice";
}
}
return 0;
}
class QUEUE {
private:
list<string> queue;
public:
void insert(string item) {
cout<<"Item "<<item<<" inserted into the QUEUE";
queue.push_back(item);
}
void remove() {
if (queue.empty()) {
cout<<"QUEUE UNDERFLOW";
return;
}
cout<<"Item "<<queue.front()<<" removed from the QUEUE";
queue.pop_front();
}
void display() {
if (queue.empty()) {
cout<<"QUEUE EMPTY";
return;
}
cout<<"QUEUE CONTENTS: ";
for (auto i: queue) {
cout<<i<<" ";
}
}
};
int main() {
QUEUE q;
string item;
int choice;
while (1) {
cout<<"\n\n1. Insert\n2. Delete\n3. Display\n4. Exit\nEnter your choice: ";
cin>>choice;
switch (choice) {
case 1:
cout<<"Enter the item to be inserted: ";
cin>>item;
q.insert(item);
break;
case 2:
q.remove();
break;
case 3:
q.display();
break;
case 4:
exit(0);
default:
cout<<"Invalid choice";
}
}
}
3. Develop a C++ program to store and evaluate a polynomial using lists in STL.
One Variable
#include <iostream>
#include <list>
#include <cmath>
struct Term {
int coeff;
int exp;
};
list<Term> readPoly() {
list<Term> poly;
int n;
cout<<"Enter the number of terms: ";
cin>>n;
for (int i = 0; i < n; i++) {
Term term;
cout<<"Enter the coefficient and degree of term "<<i+1<<": ";
cin>>term.coeff>>term.exp;
poly.push_back(term);
}
return poly;
}
int main() {
int x;
cout<<"Enter the polynomial: \n";
list<Term> poly = readPoly();
cout<<"Enter the value of x: ";
cin>>x;
cout<<"The polynomial is: ";
displayPoly(poly);
cout<<"\nEvaluated value of polynomial at x = "<<x<<" is: "<<evalPoly(poly, x);
return 0;
}
Two Variable
#include <iostream>
#include <list>
#include <cmath>
struct Term {
int coeff;
int powx;
int powy;
};
list<Term> readPoly() {
list<Term> poly;
int n;
cout<<"Enter the number of terms: ";
cin>>n;
for(int i=0; i<n; i++) {
Term t;
cout<<"Enter the coefficient: ";
cin>>t.coeff;
cout<<"Enter the power of x: ";
cin>>t.powx;
cout<<"Enter the power of y: ";
cin>>t.powy;
poly.push_back(t);
}
return poly;
}
int main() {
int x, y;
cout<<"Enter the polynomial: \n";
list<Term> poly = readPoly();
cout<<"Enter the value of x: ";
cin>>x;
cout<<"Enter the value of y: ";
cin>>y;
cout<<"The polynomial is: ";
displayPoly(poly);
cout<<"\nEvaluated value of polynomial at x = "<<x<<" and y = "<<y<<" is:
"<<evalPoly(poly, x, y);
return 0;
}
4. Develop a C++ program to sort two unordered vectors/lists into ordered vectors/lists.
Vector:
#include <iostream>
#include <vector>
vector<int> readVector(int n) {
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin>>v[i];
}
return v;
}
int main() {
int n1, n2;
cout<<"Enter the size of the first vector: ";
cin>>n1;
cout<<"Enter the elements of the first vector: ";
vector<int> v1 = readVector(n1);
cout<<"Enter the size of the second vector: ";
cin>>n2;
cout<<"Enter the elements of the second vector: ";
vector<int> v2 = readVector(n2);
vector<int> v;
v.insert(v.end(), v1.begin(), v1.end());
v.insert(v.end(), v2.begin(), v2.end());
cout<<"Vector 1: ";
displayVector(v1);
cout<<"Vector 2: ";
displayVector(v2);
mergeSort(v, 0, v.size() - 1);
cout<<"Sorted merged vector: ";
displayVector(v);
return 0;
}
List:
#include <iostream>
#include <list>
list<int> readList(int n) {
list<int> l;
for (int i = 0; i < n; i++) {
int x;
cin>>x;
l.push_back(x);
}
return l;
}
void displayList(list<int> l) {
for (int x : l)
cout<<x<<" ";
cout<<endl;
}
int main() {
int n1, n2;
cout<<"Enter the size of first list: ";
cin>>n1;
cout<<"Enter the elements of first list: ";
list<int> l1 = readList(n1);
cout<<"Enter the size of second list: ";
cin>>n2;
cout<<"Enter the elements of second list: ";
list<int> l2 = readList(n2);
list<int> l;
cout<<"List 1: ";
displayList(l1);
cout<<"List 2: ";
displayList(l2);
l.merge(l1);
l.merge(l2);
cout<<"Sorted merged list: ";
mergeSort(l, 0, l.size() - 1);
displayList(l);
return 0;
}
5. Develop a C++ program to implement separate chaining hash table along with
rehashing to store a set of integers for a table of prime size.
#include <iostream>
#include <bits/stdc++.h>
class Hashtable
{
private:
vector<list<int>> table;
int table_size;
public:
Hashtable(int n) {
table_size = n;
table.resize(n);
}
bool isPrime(int x) {
if (x <= 1) return false;
for (int i = 2; i <= x / 2; i++) {
if (x % i == 0) return false;
}
return true;
}
double load_factor(int m) {
return (double)m / (double)table_size;
}
void rehash() {
int new_tablesize = 2 * table_size;
while (!isPrime(new_tablesize)) {
new_tablesize++;
}
vector<list<int>> new_table(new_tablesize);
for (auto it : table) {
for (auto key : it) {
int index = key % new_tablesize;
new_table[index].push_back(key);
}
}
table = move(new_table);
table_size = new_tablesize;
}
void insert(int val, int m) {
int index = val % table_size;
table[index].push_back(val);
if (load_factor(m) > 0.75) {
rehash();
}
}
void assign() {
int n;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Enter the "<<n<<" elements: ";
for (int i = 0; i < n; i++) {
int x;
cin>>x;
insert(x, i + 1);
}
}
void display() {
int i = 0;
for (auto it : table) {
cout<<"["<<i++<<"]";
for (auto val : it) {
cout<<"->"<<val;
}
cout<<endl;
}
}
};
int main() {
Hashtable h1(5);
h1.assign();
h1.display();
return 0;
}
6. Develop a C++ program to implement linear probing and quadra c probing along with
rehashing to store a set of integers for a table of prime size.
Linear Probing:
#include <iostream>
#include <bits/stdc++.h>
class Hashtable {
private:
vector<int> table;
int table_size;
public:
Hashtable(int n) {
table_size = n;
table.resize(n, -1);
}
bool isPrime(int x) {
if (x <= 1) return false;
for (int i = 2; i <= x / 2; i++) {
if (x % i == 0) return false;
}
return true;
}
double load_factor(int m) {
return (double)m / (double)table_size;
}
void rehash() {
int new_tablesize = 2 * table_size;
while (!isPrime(new_tablesize)) {
new_tablesize++;
}
vector<int> new_table(new_tablesize, -1);
for (auto val : table) {
if (val != -1) {
int index = val % new_tablesize;
while (new_table[index] != -1) {
index = (index + 1) % new_tablesize;
}
new_table[index] = val;
}
}
table = move(new_table);
table_size = new_tablesize;
}
void display() {
for (int i = 0; i < table_size; i++) {
cout<<"["<<i<<"] -> "<<table[i]<<endl;
}
cout << endl;
}
};
int main() {
Hashtable h1(5);
h1.assign();
h1.display();
return 0;
}
Quadra c Probing:
#include <iostream>
#include <bits/stdc++.h>
class Hashtable {
private:
vector<int> table;
int table_size;
public:
Hashtable(int n) {
table_size = n;
table.resize(n, -1);
}
bool isPrime(int x) {
if (x <= 1) return false;
for (int i = 2; i <= x / 2; i++) {
if (x % i == 0) return false;
}
return true;
}
double load_factor(int m) {
return (double)m / (double)table_size;
}
void rehash() {
int new_tablesize = 2 * table_size;
while (!isPrime(new_tablesize)) {
new_tablesize++;
}
vector<int> new_table(new_tablesize, -1);
for (auto val : table) {
if (val != -1) {
int index = val % new_tablesize;
while (new_table[index] != -1) {
index = (index + 1) % new_tablesize;
}
new_table[index] = val;
}
}
table = move(new_table);
table_size = new_tablesize;
}
void assign() {
int n;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Enter the "<<n<<" elements: ";
for (int i = 0; i < n; i++) {
int x;
cin>>x;
quadratic_probing(x, i + 1);
}
}
void display() {
for (int i = 0; i < table_size; i++) {
cout<<"["<<i<<"] -> "<<table[i]<<endl;
}
cout << endl;
}
};
int main() {
Hashtable h1(5);
h1.assign();
h1.display();
return 0;
}