Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Recursive function not working

$
0
0
The function named sub1_skip is supposed to subtract 1 from the stuff field every other node in the list. It's not working and I don't quite understand why. Any help would be greatly appreciated.

#include <cstdlib>
#include <iostream>

using namespace std;

struct list_node
{
       char stuff;
       list_node* next;
};

typedef list_node* list_ptr;

void build (list_ptr& front, int nodes, char ch);
void print (list_ptr front);
int sub1_skip (list_ptr& front);

int main(int argc, char *argv[])
{
    list_ptr front;
    
    build (front, 10, 'u');
    sub1_skip (front);
    cout << "\nThe list is:\n";
    print (front);
    
    cout << "\n\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}

int sub1_skip (list_ptr& front){
     
     if (front -> stuff == 0 || front -> stuff % 2 == 0){
        front -> stuff = front -> stuff - 1;
        return front -> stuff;}
     else
         return front -> stuff;
     
     }

void build (list_ptr& front, int nodes, char ch)
{
     if (nodes == 0)
        front = NULL;
     else
     {
         front = new list_node;
         front -> stuff = ch;
         build (front -> next, nodes - 1, ch + 1);
     }
}

void print (list_ptr front)
{
     if (front != NULL)
     {
        cout << front -> stuff << " ";
        print (front -> next);
     }
}


Viewing all articles
Browse latest Browse all 51036

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>