Thread: Stack Sort

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    Stack Sort

    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;

    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I've never done it before, but i would suggest just simply sorting them with an alphabetizing function, and then have a functin that pushes them on the stack. A general rule with functions - Do one thing, Do it well (An inside joke for DavidP - Do it, Do it right, Do it right now). Try breaking your code up into smaller pieces, with one job for each function/framework.

  3. #3
    GeekSeb
    Guest
    As Far As i Know, Stacks Arent Supposed to Be Sorted. But Anywho:

    To Alphabetize, When adding the extra node to the stack, traverse your stack with two pointers, one to the element for checking and one to the previous element. When you find a spot for the new one to be inserted, then you do it. Then, no extra function is needed. Just Incorporate Into the Push() Function.

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Surely sorting a stack defeats the object (the LIFO paradigm). Anyway, to do it you'll need two more stacks to hold out of order values while you sort. A bit like Towers of Hanoi.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM