preslav_milev 0 Newbie Poster

Hello !

How can I change my sorting function called bubblesort, so that it uses a dynamic stack. Now I've done and funcion can work properly, but is using list, because I intercede for the next item and in stack i have access only to the top. Data for stacks are taken from external text files. Briefly, sorting function works, but must be converted to work with dynamic stack.

    #include <iostream>
    #include <stdlib.h>
    #include <fstream>
    #include <time.h>
    #include <algorithm>
    #include <stdio.h>
    #include <vector>
    #include <deque>
    #include <string>

    using namespace std; 

    int temp;
    int br = 0;

    struct stack 
    { 
        int key; 
        stack *next; 
    } *start, *p, *s1 = NULL, *s2 = NULL, *help = NULL; 

    void push(stack *&st, int n)
    {

        stack *p = st; 
        st = new stack; 
        st -> key = n; 
        st -> next = p;

    }

    int pop(stack *&start, int &n) 
    {
        if(start) 
        {
            stack *p = start; 
            n = start -> key; 
            start = start -> next;
            delete p;
            return 1;
        }
        else
            return 0;
    }

    void print_stack(int z)
    {

        if (z == 1)
        {
                if(s1)
                {
                    int a;

                    while(pop(s1, a))
                    {
                        cout << a << " ";
                        push(help, a);
                    }
                     while(pop(help, a))
                        push(s1, a);
                }
                else
                    cout << "Stack 1 is empty !";
        }

        if(z == 2)
        {
                if(s2)
                {
                    int b;

                     while( pop(s2, b) )
                     {
                          cout << b << " ";
                          push(help, b);
                     }

                     while( pop(help, b) )
                        push(s2, b);
                }
                else
                    cout << "Stack 2 is empty!";

        }

        cout << endl;

    }

    int bubblesort(stack *&S)
    {

        stack *T;  
        int n, flag;
        int current = pop(S, n);
        if (S == NULL)
            return 0;
        do
        {
            flag = 0;
            T = S;
            while (T->next)
            {
                if (T->next->key < T->key) 
                {
                    n = T->key;
                    T->key = T->next->key;
                    T->next->key = n;
                    flag = 1;
                }
                else
                    T = T->next;
            }
        } while (flag);

        return 0;

    }

    void input_first(stack *st)
    {

        int a;
        cout << "Stack1\n";

        ifstream i_file;
        i_file.open("stack1.txt", ios::in);
        if(i_file)
        {

            while (!i_file.eof())
            {

                i_file >> a;
                push(s1, a);

            }

            i_file.close();


        }
        else
            cout << "Error while opening input file!" << endl;

    }

    void input_second(stack *st)
    {

        int a;

        cout << "Stack2\n";

        ifstream i_file;
        i_file.open("stack2.txt", ios::in);
        if (i_file)
        {
            while (!i_file.eof())
            {

                i_file >> a;
                push(s2, a);

            }
            i_file.close();

        }
        else
            cout << "Error while opening input file!" << endl;

    }

    int main()
    {

        stack *test1 = NULL;
        stack *test2 = NULL;

        input_first(test1);
        print_stack(1);
        cout << endl;

        input_second(test2);
        print_stack(2);
        cout << endl;

        cout<<"Sorted first stack: ";
        bubblesort(s1);
        print_stack(1);
        cout << endl;

        cout<<"Sorted second stack: ";
        bubblesort(s2);
        print_stack(2);
        cout << endl;

        system("pause");
        return 0;

    }