Sort the data stored in 1) stack 2) queue and 3) linked list entered in part 1
#include <iostream>
#include <stack>
#include <queue>
#include <list>
#include <random>
#include <algorithm>
#include <ctime>
#include <vector>
using namespace std;
void SortSt(stack<int>& st);
void SortQu(queue<int>& qu);
void SortLi(list<int>& li);
int main()
{
stack<int> st;
queue<int> qu;
list<int> li;
srand(time(0));
cout << "inserted value: ";
for (int i = 0; i < 10; i++)
{
int temp = rand() % 50;
st.push(temp);
qu.push(temp);
li.push_back(temp);
cout << temp << " ";
}
cout << endl;
SortLi(li);
SortQu(qu);
SortSt(st);
cout << "Sorted list: ";
for (int i = 0; i < 10; i++)
{
cout << li.front() << " ";
li.pop_front();
}
cout << endl;
cout << "Sorted stack: ";
for (int i = 0; i < 10; i++)
{
cout << st.top() << " ";
st.pop();
}
cout << endl;
cout << "Sorted queue: ";
for (int i = 0; i < 10; i++)
{
cout << qu.front() << " ";
qu.pop();
}
cout << endl;
system("pause");
return 0;
}
void SortSt(stack<int>& st)
{
vector<int> temp;
while (!st.empty())
{
temp.push_back(st.top());
st.pop();
}
sort(temp.begin(), temp.end());
for (int i = temp.size() - 1; i >= 0; i--)
{
st.push(temp[i]);
}
}
void SortQu(queue<int>& qu)
{
vector<int> temp;
while (!qu.empty())
{
temp.push_back(qu.front());
qu.pop();
}
sort(temp.begin(), temp.end());
for (int i = 0; i < temp.size(); i++)
{
qu.push(temp[i]);
}
}
void SortLi(list<int>& li)
{
vector<int> temp;
while (!li.empty())
{
temp.push_back(li.front());
li.pop_front();
}
sort(temp.begin(), temp.end());
for (int i = 0; i < temp.size(); i++)
{
li.push_back(temp[i]);
}
}
Comments
Leave a comment