Thread: What's wrong with this?

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    28

    What's wrong with this?

    Code:
    while( stackTop( sHead ) != '(' )
       some code;
    and the definition of stackTop is:
    Code:
    char stackTop( Node *s ){
    	
    	if( !isEmpty( s ) )
    		return s->data;
    	
    	return '\0';
    	
    } // stackTop
    Nothing wrong with? BTW, stackTop returns the top node of a stack w/o deleting it.

    I get a suspicious pointer conversion warning.
    Dat
    http://hoteden.com <-- not an porn site damnit!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: What's wrong with this?

    [edit]
    Nevermind. (If you actually saw my reply.) Your description of the issue and the actual issue itself are different. You describe it returning a node, and yet it really returns a single character. Your terminology threw me off in my answer.
    [/edit]
    On a side note, you should be also checking for a NULL on that return. Otherwise, it's possible for an empty stack to let your loop continue indefinately.

    You'll need to provide more code for us to correctly provide a cause/resolution to your problem.

    Quzah.
    Last edited by quzah; 06-20-2003 at 02:44 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Continuing on the above two replies, the only time you won't have an infinite loop is if the '(' is the top of the stack, unless you pop elements in 'some-code', since you appear to be always checking the same element.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I think we may be talking about this. I haven't been much help over there.
    Code:
                while( stackTop( sHead ) != '(' ) 
    [Error 64] Type mismatch (arg. no. 1) (struct node * = struct node **)
    sHead is a pointer to pointer; the function stackTop wants a pointer.
    Last edited by Dave_Sinkula; 06-20-2003 at 04:24 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @Dat : Post your latest source and someone will help you. We'll try and stay on track this time
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    28
    Dave_Sinkula actually understood my question, but here's the full source:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #define LENGTH(x) ( sizeof((x)) / sizeof((x[0])) )
    
    typedef struct node{
    	char data;
    	struct node *next;
    } Node;
    
    void push( Node **sHead, char value );
    char pop( Node **sHead );
    int isEmpty( Node *head );
    void printS( Node *s );
    char stackTop( Node *s );
    void convertInToPost( Node **sHead, char *infix, char *postfix );
    void strchar( char *str, char c );
    int isOperator( char c );
    int precedence( char op1, char op2 );
    
    int main( void ){
    	Node *head = NULL;
    	char infix[20];
    	char postfix[10];
    	postfix[0] = '\0';
    	strcpy( infix, "1+2" );
    	
    	convertInToPost( &head, infix, postfix );
    	
    	printf( "%s ", postfix );
    	
    	
    
    	return 0;
    	
    } // main
    
    void convertInToPost( Node **sHead, char *infix, char *postfix ){
    	int i;
    	
    	push( sHead, '(' );
    	strncat( infix, ")", 1 );
    	
    	while( !isEmpty( *sHead ) )
    		for( i = 0 ; i < LENGTH( infix ) ; i++ )
    			if( isdigit(infix[i]) )
    				strchar( postfix, infix[i] );
    			else if( infix[i] == '(' )
    				push( sHead, infix[i] );
    			else if( isOperator( infix[i] ) ){
    				if( precedence( stackTop( *sHead ), infix[i] ) >= 0  )
    					strchar( postfix, pop( sHead ) );
    				push( sHead, infix[i] );
    			}else if( infix[i] == ')' ){
    				while( stackTop( sHead ) != '(' )
    					strchar( postfix, pop( sHead ) );
    				pop( sHead );
    			}
    	
    } // convertIntoPost
    
    void strchar( char *str, char c ){
    	
    	while( *str != '\0' )
    		str++;
    	*str = c;
    	*(str + 1 ) = '\0';
    	
    } // strchar
    
    int isOperator( char c ){
    	
    	if( c == '*' || c == '/' || c == '+' || c == '-' )
    		return 1;
    	
    	return 0;
    	
    } // isOperator
    
    int precedence( char op1, char op2 ){
    
    	
    	switch( op1 ){
    		case '*': case '/':
    			if( op2 == '*' || op2 == '/' )
    				return 0;
    			else
    				return 1;	
    		case '+': case '-':
    			if( op2 == '*' || op2 == '/' )
    				return -1;
    			else
    				return 0;
    	
    	}
    	
    	return -10;
    	
    } // precedence
    
    char stackTop( Node *s ){
    	
    	if( !isEmpty( s ) )
    		return s->data;
    	
    	return '\0';
    	
    } // stackTop
    
    void printS( Node *s ){
    	
    	while( !isEmpty( s ) ){
    		printf( "%3c --> ", s->data );
    		s = s->next;		
    	}
    	printf( "NULL\n\n" );
    	
    } // printS
    
    
    void push( Node **sHead, char value ){
    	Node *newNode;
    	
    	newNode = malloc( sizeof( Node ) );
    	
    	if( newNode != NULL ){
    		newNode->data = value;
    		newNode->next = *sHead;
    		*sHead = newNode;	
    	}else
    		printf( "***ERROR*** Could _NOT_ allocate memory for new object!\n" );
    	
    } // push
    
    char pop( Node **sHead ){
    	Node *temp;
    	char value;
    	
    	if( !isEmpty( *sHead ) ){
    		value = (*sHead)->data;
    		temp = *sHead;
    		*sHead = (*sHead)->next;
    		free( temp );
    		return value;		
    	}
    	
    	return '\0';
    	
    } // pop
    
    
    int isEmpty( Node *head ){
    	return head == NULL;
    } // isEmpty
    Dat
    http://hoteden.com <-- not an porn site damnit!

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    28
    Update:

    After passing the right argument to my stackTop() function, the program worked. And in case you were wondering, it converts infix notation such as 1 + 2 to postfix, such as 1 2 +
    Dat
    http://hoteden.com <-- not an porn site damnit!

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by dat
    Dave_Sinkula actually understood my question, but here's the full source:
    Since all you showed us was:

    stackTop( sHead )

    Without saying what 'sHead' was, the best we could do was guess. Next time, provide the correct information in the first place, and it'll save you a bunch of time.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM