Stack Sort

This is a discussion on Stack Sort within the C++ Programming forums, part of the General Programming Boards category; Does anyone know how to sort a stack in alphabetical order using only stack operations(push(),pop(), etc)? Here is the code ...

  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 jtchamit@coe.neu.edu.

    #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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    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.
    MSVC++ 6.0

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-12-2006, 12:41 AM
  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, 06:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21