Does anyone know how to sort a stack in alphabetical order using only stack operations(push(),pop(), etc)? Here is the code so far. Any ideas? Please email me at [email protected].
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main()
{
stack <string> initial;
stack <string> smallest;
string name;
string comp1;
string comp2;
int count=0,
y=0;
int smallcount=0;
cerr << "Enter name\n";
cerr << "Enter -1 to quit: ";
cin >> name;
while(name != "-1")
{
initial.push(name);
count++;
cerr << "Enter name\n";
cerr << "Enter -1 to quit: ";
cin >> name;
}
/* initial sort */
for (y=1 ; y <= count-1 ; y++)
{
comp1=initial.top();
initial.pop();
comp2=initial.top();
initial.pop();
cout<<"comp1: "<<comp1<<endl;
cout<<"comp2: "<<comp2<<endl;
if(comp1 < comp2)
{
smallest.push(comp1);
smallcount++; initial.push(comp2);
}
if(comp1 > comp2)
{
smallest.push(comp2);
smallcount++;
initial.push(comp1);
}
}
cout<<"\nsmallest values: "<<smallcount<<endl;
while(!smallest.empty())
{
comp1=smallest.top();
smallest.pop();
cout<<comp1<<" ";
}
cout<<"\nlargest value: \n";
while(!initial.empty())
{
comp1=initial.top();
initial.pop();
cout<<comp1<<" ";
}
cout<<endl;
return 0;
}