0

I'm trying to sort a deck with merge sort.I have all my code working except merge sort function .
Any help would be really helpful ;)

int temp;
int br = 1;
void push_l(int n);
void push_r(int n);
int pop_l(int &n);
int pop_r(int &n);

struct elem             
{
    int key;             
    elem *next;          

}*_left = NULL, *_right = NULL; 

void push_l(int n)      
{
    elem *p;        
    p = _left;          
    _left = new elem;   
    _left->key = n; 
    _left->next = p;        

    if (right == NULL)           

    {
        _right = _left;
    }
}



void push_r(int n)      

{
    elem *p;        
p = _right;     
    _right = new elem;  
    _right->key = n;        
    _right->next = NULL;    
    if (_left == NULL)       
    {
        _left = _right;     
    }
    else                    
        p->next = _right;   
}



int pop_l(int &n)           
{
    elem *p;            
    if (_left)              
    {
        n = _left->key;             
        p = _left;          
        _left = _left->next;    
        if (left == NULL)       
            _right = NULL;      
        delete p;       
        return 1;
    }
    else
        return 0;
}



int pop_r(int &n)           
{
    elem *p;            
    if (right)          
    {
        n = _right->key;         
        if (_left == _right)        
        {
            delete _right;      
            _left = _right = NULL;  
        }
        else
        {
            p = _left;          
            while (p->next != _right)    

                p = p->next;            
            n = _right->key;            
            p->next = NULL;     
            _right = p;         
        }
        return 1;
    }
    else
        return 0;
}


void output(ofstream& ostr, int n)      
{
    ostr << n << endl;
}



void get(int n)         
{
    int i, t;
    for (i = 1; i<br; i++)   
    {
        pop_l(t);   
        if (i == n)     
        {
            temp = t;   
        }
        push_r(t);  
    }

}

void set(int n, int d)      
{
    int t;
    for (int i = 1; i<br; i++)   
    {
        pop_l(t);   
        if (i == n)     
        {
            t = d;  
        }
        push_r(t);  
    }
}



void list()         
{
    cout << "\n Dek:";
    for (int i = 1; i<br; i++)       
    {
        get(i);             
        cout << "\t" << temp;       

    }
}


int mergesort(int a[], int n)
    {
       if(n<2) return;   
    int nleft=n/2, nright=n-nleft;
      mergesort(a, nleft);   
  mergesort(a+nleft, nright);   
   int *p=new int [n];   
 merge (a, nleft, a+nleft, nright, p);   
 for (int i=0; i<n; i++)    
   a[i]=p[i];  
  delete []p;

    }

    void search()       
{           
    mergesort();        
    int t=0;
    for (int i = 1; i<br; i++)   
    {
        get(i);         
        if (temp % 5 == 0)  
            t = temp;       
    }
    cout << "\n Max element \7: " << t << endl;
    cout << "\n Elements multiples of 5 and less than the maximum multiple of 7: " << endl;
    for (int i = 1; i<br; i++)       
    {
        get(i);         
        if (temp % 7 == 0 && temp<t) 
        {               
            cout << "\t" << temp;   
        }
    }
}



int main()
{
    int ch, m;
    do
    {
        cout << "\n\n\t\t MENU" << endl;
        cout << "\n\t1-Data from fail" << endl;
        cout << "\t2-Merge sort" << endl;
        cout << "\t3-Elements multiples of 5 and less than the maximum multiple of 7" << endl;
        cout << "\t4-save" << endl;
        cout << "\t5-Exit" << endl;
        cout << "\n\tVie izbrahte:";
        cin >> m;
        cout << endl;


        switch (m)
        {
        case 1:
        {
            ifstream ff;
            ff.open("deck.txt");
            while (ff)
            {
                ff >> ch;
                if (ch == 0) break;
                else
                {
                    push_r(ch);
                    br++;
                }
            }
            list();
            break;
        }

        case 2:
        {
            mergesort();
            list();
            break;
        }
        case 3:
        {
            search();
            break;
        }
        case 4:
        {
            ofstream ff("sorted deck.txt");
            while (pop_l(ch))
            {
                output(ff, ch);
            }
            cout << "\n\t *** File saved! ***" << endl;
        }
        }
    } while (m != 5);
}


int main()
{
    int ch, m;
    do
    {
        cout << "\n\n\t\t MENU" << endl;
        cout << "\n\t1-Data from fail" << endl;
        cout << "\t2-Merge sort" << endl;
        cout << "\t3-Elements multiples of 5 and less than the maximum multiple of 7" << endl;
        cout << "\t4-save" << endl;
        cout << "\t5-Exit" << endl;
        cout << "\n\tVie izbrahte:";
        cin >> m;
        cout << endl;


        switch (m)
        {
        case 1:
        {
            ifstream ff;
            ff.open("deck.txt");
            while (ff)
            {
                ff >> ch;
                if (ch == 0) break;
                else
                {
                    push_r(ch);
                    br++;
                }
            }
            list();
            break;
        }

        case 2:
        {
            mergesort();
            list();
            break;
        }
        case 3:
        {
            search();
            break;
        }
        case 4:
        {
            ofstream ff("sorted deck.txt");
            while (pop_l(ch))
            {
                output(ff, ch);
            }
            cout << "\n\t *** File saved! ***" << endl;
        }
        }
    } while (m != 5);
}



    int main()
{
    int ch, m;
    do
    {
        cout << "\n\n\t\t MENU" << endl;
        cout << "\n\t1-Data from fail" << endl;
        cout << "\t2-Merge sort" << endl;
        cout << "\t3-Elements multiples of 5 and less than the maximum multiple of 7" << endl;
        cout << "\t4-save" << endl;
        cout << "\t5-Exit" << endl;
        cout << "\n\tVie izbrahte:";
        cin >> m;
        cout << endl;


        switch (m)
        {
        case 1:
        {
            ifstream ff;
            ff.open("deck.txt");
            while (ff)
            {
                ff >> ch;
                if (ch == 0) break;
                else
                {
                    push_r(ch);
                    br++;
                }
            }
            list();
            break;
        }

        case 2:
        {
            mergesort();
            list();
            break;
        }
        case 3:
        {
            search();
            break;
        }
        case 4:
        {
            ofstream ff("sorted deck.txt");
            while (pop_l(ch))
            {
                output(ff, ch);
            }
            cout << "\n\t *** File saved! ***" << endl;
        }
        }
    } while (m != 5);
2
Contributors
2
Replies
29
Views
2 Years
Discussion Span
Last Post by raliot
0

Why do you have 3 main() functions here? Are you trying to show various approaches you have attempted? If so, then break them into separate code blocks, or postings to this thread. Thanks.

0

sorry my mistake ,i have only one main() function

int temp;
int br = 1;
void push_l(int n);
void push_r(int n);
int pop_l(int &n);
int pop_r(int &n);
struct elem             
{
    int key;             
    elem *next;          
}*_left = NULL, *_right = NULL; 
void push_l(int n)      
{
    elem *p;        
    p = _left;          
    _left = new elem;   
    _left->key = n; 
    _left->next = p;        
    if (right == NULL)           
    {
        _right = _left;
    }
}
void push_r(int n)      
{
    elem *p;        
p = _right;     
    _right = new elem;  
    _right->key = n;        
    _right->next = NULL;    
    if (_left == NULL)       
    {
        _left = _right;     
    }
    else                    
        p->next = _right;   
}
int pop_l(int &n)           
{
    elem *p;            
    if (_left)              
    {
        n = _left->key;             
        p = _left;          
        _left = _left->next;    
        if (left == NULL)       
            _right = NULL;      
        delete p;       
        return 1;
    }
    else
        return 0;
}
int pop_r(int &n)           
{
    elem *p;            
    if (right)          
    {
        n = _right->key;         
        if (_left == _right)        
        {
            delete _right;      
            _left = _right = NULL;  
        }
        else
        {
            p = _left;          
            while (p->next != _right)    
                p = p->next;            
            n = _right->key;            
            p->next = NULL;     
            _right = p;         
        }
        return 1;
    }
    else
        return 0;
}
void output(ofstream& ostr, int n)      
{
    ostr << n << endl;
}
void get(int n)         
{
    int i, t;
    for (i = 1; i<br; i++)   
    {
        pop_l(t);   
        if (i == n)     
        {
            temp = t;   
        }
        push_r(t);  
    }
}
void set(int n, int d)      
{
    int t;
    for (int i = 1; i<br; i++)   
    {
        pop_l(t);   
        if (i == n)     
        {
            t = d;  
        }
        push_r(t);  
    }
}
void list()         
{
    cout << "\n Dek:";
    for (int i = 1; i<br; i++)       
    {
        get(i);             
        cout << "\t" << temp;       
    }
}
int mergesort(int a[], int n)
    {
       if(n<2) return;   
    int nleft=n/2, nright=n-nleft;
      mergesort(a, nleft);   
  mergesort(a+nleft, nright);   
   int *p=new int [n];   
 merge (a, nleft, a+nleft, nright, p);   
 for (int i=0; i<n; i++)    
   a[i]=p[i];  
  delete []p;
    }
    void search()       
{           
    mergesort();        
    int t=0;
    for (int i = 1; i<br; i++)   
    {
        get(i);         
        if (temp % 5 == 0)  
            t = temp;       
    }
    cout << "\n Max element \7: " << t << endl;
    cout << "\n Elements multiples of 5 and less than the maximum multiple of 7: " << endl;
    for (int i = 1; i<br; i++)       
    {
        get(i);         
        if (temp % 7 == 0 && temp<t) 
        {               
            cout << "\t" << temp;   
        }
    }
}
int main()
{
    int ch, m;
    do
    {
        cout << "\n\n\t\t MENU" << endl;
        cout << "\n\t1-Data from fail" << endl;
        cout << "\t2-Merge sort" << endl;
        cout << "\t3-Elements multiples of 5 and less than the maximum multiple of 7" << endl;
        cout << "\t4-save" << endl;
        cout << "\t5-Exit" << endl;
        cout << "\n\tVie izbrahte:";
        cin >> m;
        cout << endl;
        switch (m)
        {
        case 1:
        {
            ifstream ff;
            ff.open("deck.txt");
            while (ff)
            {
                ff >> ch;
                if (ch == 0) break;
                else
                {
                    push_r(ch);
                    br++;
                }
            }
            list();
            break;
        }
        case 2:
        {
            mergesort();
            list();
            break;
        }
        case 3:
        {
            search();
            break;
        }
        case 4:
        {
            ofstream ff("sorted deck.txt");
            while (pop_l(ch))
            {
                output(ff, ch);
            }
            cout << "\n\t *** File saved! ***" << endl;
        }
        }
    } while (m != 5);
}

Edited by raliot: code

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.