help

This is a discussion on help within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> using namespace std; struct node { int data; struct node* next; }; struct node* BuildOneTwoThree(){ struct node* head=NULL; ...

  1. #1
    uod
    uod is offline
    Registered User
    Join Date
    Jul 2006
    Posts
    2

    help

    Code:
    #include<iostream>
    using namespace std;
    struct node {
    	int data;
    	struct node* next;
    };
    struct node* BuildOneTwoThree(){
    	struct node* head=NULL;
    	struct node* second=NULL;
    	struct node* third=NULL;
    	head= new struct node,second= new struct node,third= new struct node;
    	head->next=second,second->next=third,third->next=NULL;
    	head->data=1,head->next->data=2,head->next->next->data=3;
    	return head;
    };
    int counter=0;
    int length(struct node* list) {
    	if(list!=NULL) {
    		counter+=1;
    		length(list->next);
    	}
    	return counter;
    }
    
    int main(){
    	struct node* list=BuildOneTwoThree();
    	cout<<"Length of List: "<<length(list)<<endl;
    	system("pause");
    	return 0;
    }
    is there any way not to use a global variable with this recursive function
    Code:
    int counter=0;
    int length(struct node* list) {
    	if(list!=NULL) {
    		counter++;
    		length(list->next);
    	}
    	return counter;
    }

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    move it inside the function and declare it static.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    uod
    uod is offline
    Registered User
    Join Date
    Jul 2006
    Posts
    2
    wow it worked.. thanx.. ^_^

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,403
    Actually it doesn't

    Don't return counter. Return a copy of it and set counter back to 0 before you return from the function. Otherwise any call to that function past the first one will not work as expected.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Static variables are always there, even when they are not needed. Another approach is to add a second parameter with a default value of zero, then use it for counting.

    Code:
    int length(struct node* list, int counter = 0)
    {
    	if(list != NULL)
    	{
    		return length(list -> next, counter + 1);
    	}
    	else
    	{
    		return 0;
    	}
    }
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

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