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

Advanced Data Structures Lab Updated

Ads lab syllabus

Uploaded by

ashayamal2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Advanced Data Structures Lab Updated

Ads lab syllabus

Uploaded by

ashayamal2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Advanced Data Structures

1. Develop a C++ program to implement stack of strings using vectors in STL.


#include <iostream>
#include <string>
#include <vector>

using namespace std;

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;
}

2. Develop a C++ program to implement ordinary queue using lists in STL.


#include <iostream>
#include <string>
#include <list>

using namespace std;

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>

using namespace std;

struct Term {
int coeff;
int exp;
};

int evalPoly(list<Term> poly, int x) {


int sum = 0;
for (auto it : poly) {
sum += it.coeff * pow(x, it.exp);
}
return sum;
}

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;
}

void displayPoly(list<Term> poly) {


for (auto it = poly.begin(); it != poly.end(); it++) {
cout<<it->coeff<<"x^"<<it->exp;
if (next(it) != poly.end()) {
cout<<" + ";
}
}
}

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>

using namespace std;

struct Term {
int coeff;
int powx;
int powy;
};

int evalPoly(list<Term> poly, int x, int y) {


int sum = 0;
for (auto it : poly) {
sum += it.coeff * pow(x, it.powx) * pow(y, it.powy);
}
return sum;
}
void displayPoly(list<Term> poly) {
for (auto it = poly.begin(); it != poly.end(); it++) {
cout<<it->coeff<<"x^"<<it->powx<<"y^"<<it->powy;
if (next(it) != poly.end()) {
cout<<" + ";
}
}
}

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>

using namespace std;

vector<int> readVector(int n) {
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin>>v[i];
}
return v;
}

void displayVector(vector<int> &v) {


for (int i : v) {
cout<<i<<" ";
}
cout<<endl;
}

void merge(vector<int> &v, int l, int m, int r) {


int n1 = m - l + 1;
int n2 = r - m;
vector<int> L(n1), R(n2);
for (int i = 0; i < n1; i++) {
L[i] = v[l + i];
}
for (int i = 0; i < n2; i++) {
R[i] = v[m + 1 + i];
}
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
v[k++] = L[i++];
}
else {
v[k++] = R[j++];
}
}
while (i < n1) {
v[k++] = L[i++];
}
while (j < n2) {
v[k++] = R[j++];
}
}

void mergeSort(vector<int> &v, int l, int r) {


if (l < r) {
int m = (l + r) / 2;
mergeSort(v, l, m);
mergeSort(v, m + 1, r);
merge(v, l, m, r);
}
}

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>

using namespace std;

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;
}

void merge(list<int> &lst, int l, int m, int r) {


int n1 = m - l + 1;
int n2 = r - m;
list<int> L, R;
auto it = lst.begin();
for (int i = 0; i < l; i++) {
it++;
}
for (int i = 0; i < n1; i++) {
L.push_back(*it);
it++;
}
for (int i = 0; i < n2; i++) {
R.push_back(*it);
it++;
}
auto i = L.begin(), j = R.begin(), k = lst.begin();
for (int i = 0; i < l; i++) {
k++;
}
while (i != L.end() && j != R.end()) {
if (*i <= *j) {
*k = *i;
i++;
} else {
*k = *j;
j++;
}
k++;
}
while (i != L.end()) {
*k = *i;
i++;
k++;
}
while (j != R.end()) {
*k = *j;
j++;
k++;
}
}

void mergeSort(list<int> &lst, int l, int r) {


if (l < r) {
int m = (r + l) / 2;
mergeSort(lst, l, m);
mergeSort(lst, m + 1, r);
merge(lst, l, m, r);
}
}

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>

using namespace std;

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>

using namespace std;

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 linear_probing(int val, int m) {


if (load_factor(m) > 0.75) {
rehash();
}
int i = 0;
int index = val % table_size;
while (table[index] != -1) {
index = (index + 1) % table_size;
i++;
if (i == table_size) {
cout << "Table is full" << endl;
return;
}
}
table[index] = val;
}
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;
linear_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;
}
Quadra c Probing:
#include <iostream>
#include <bits/stdc++.h>

using namespace std;

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 quadratic_probing(int val, int m) {


if (load_factor(m) > 0.75) {
rehash();
}
int i = 0;
int index = val % table_size;
while (table[index] != -1) {
index = (index + i * i) % table_size;
i++;
if (i == table_size) {
cout << "Table is full" << endl;
return;
}
}
table[index] = val;
}

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;
}

You might also like