C/Reverse satck .
Expert: Zlatko - 11/23/2009
QuestionDear mr ZlatKo
the previous answer help me a lot...
now i facing some problem...
the code i edited is as bellow,
can you check for me and tell me where is the mistake and how to solve it...
tahnk you so much.
the code :
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
/* self-referential structure */
struct stackNode {
char* data[15]; /* define data as an int */
struct stackNode *nextPtr; /* stackNode pointer */
}; /* end structure stackNode */
struct stackNode1 {
char* data[15]; /* define dat1a as an int */
struct stackNode1 *nextPtr; /* stackNode1 pointer */
}; /* end structure stackNode1 */
typedef struct stackNode StackNode; /* synonym for struct stackNode */
typedef StackNode *StackNodePtr; /* synonym for StackNode* */
typedef struct stackNode1 StackNode1; /* synonym for struct stackNode */
typedef StackNode1 *StackNode1Ptr; /* synonym for StackNode* */
/* prototypes */
void push( StackNodePtr *topPtr, char* info );
void push1( StackNode1Ptr *topPtr, char* info );
char* pop( StackNodePtr *topPtr );
int isEmpty( StackNodePtr topPtr );
void printStack( StackNodePtr currentPtr );
void printStack1( StackNode1Ptr current1Ptr );
int inputString(char* dest, int space);
int i,counter=0;
/* function main begins program execution */
int main( void )
{
StackNodePtr stackPtr = NULL; /* points to stack top */
StackNode1Ptr stack1Ptr = NULL; /* points to stack top */
char value[16];
clrscr(); /*Clear screen*/
for(i = 0; i < 10;)
{
printf( "Enter [%2d] string: " ,i);
gets(value);
if (inputString(value, sizeof(value)) >= sizeof(value))
{
printf("Thats too long, try again\n");
}
else
{
i++;
counter+=1;
push( &stackPtr, strdup(value) );
}
}
clrscr();
for (i=0;i<10;i++)
printf("%s \n",value[i]);
for(i=0;i<counter;i++)
{
push1( &stack1Ptr, pop( &stackPtr ) );
printStack1( stack1Ptr );
}
getch();
return 0; /* indicates successful termination */
} /* end main */
/* Insert a node at the stack top */
void push( StackNodePtr *topPtr, char* info )
{
StackNodePtr newPtr; /* pointer to new node */
newPtr = malloc( sizeof( StackNode ) );
/* insert the node at stack top */
if ( newPtr != NULL ) {
newPtr->data == *info;
newPtr->nextPtr = *topPtr;
*topPtr = newPtr;
} /* end if */
else { /* no space available */
printf( "%s not inserted. No memory available.\n", info );
} /* end else */
} /* end function push */
void push1( StackNode1Ptr *topPtr, char* info )
{
StackNode1Ptr newPtr; /* pointer to new node */
newPtr = malloc( sizeof( StackNode1 ) );
/* insert the node at stack top */
if ( newPtr != NULL ) {
newPtr->data = info;
newPtr->nextPtr = *topPtr;
*topPtr = newPtr;
} /* end if */
else { /* no space available */
printf( "%s not inserted. No memory available.\n", info );
} /* end else */
} /* end function push */
/* Remove a node from the stack top */
char *pop( StackNodePtr *topPtr )
{
StackNodePtr tempPtr; /* temporary node pointer */
char* popValue; /* node value */
tempPtr = *topPtr;
popValue = ( *topPtr )->data;
*topPtr = ( *topPtr )->nextPtr;
free( tempPtr );
return (popValue);
} /* end function pop */
/* Print the stack */
void printStack( StackNodePtr currentPtr )
{
/* if stack is empty */
if ( currentPtr == NULL ) {
printf( "The stack is empty.\n\n" );
} /* end if */
else {
printf( "The stack is:\n" );
/* while not the end of the stack */
while ( currentPtr != NULL ) {
printf( "%s --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
} /* end while */
printf( "NULL\n" );
} /* end else */
} /* end function printList */
void printStack1( StackNode1Ptr current1Ptr )
{
/* if stack is empty */
if ( current1Ptr == NULL ) {
printf( "The stack is empty.\n\n" );
} /* end if */
else {
printf( "The reversed stack is:\n" );
/* while not the end of the stack */
while ( current1Ptr != NULL ) {
printf( "%s --> ", current1Ptr->data );
current1Ptr = current1Ptr->nextPtr;
} /* end while */
printf( "NULL\n" );
} /* end else */
} /* end function printList */
/* Return 1 if the stack is empty, 0 otherwise */
int isEmpty( StackNodePtr topPtr )
{
return topPtr == NULL;
} /* end function isEmpty */
int inputString(char* dest, int space)
{
int count = 0;
if (dest == NULL || space == 0) return 0;
while(1)
{
char c = getc(stdin);
if (c == '\n' || c == EOF)
{
if (space) *dest = 0;
break;
}
++count;
if (space > 1)
{
*dest++ = c;
--space;
}
if (space == 1)
{
*dest = 0;
--space;
}
}
return count;
}
the error is mismatch parameter in push and Lvalue is require.
may i know what is lvalue and how to solve the code.
Thank.
AnswerCanno, I don't have much time right now to look at your problem. I will look at it later today. But I repeat that you should remove all redundant code referring to stack node 1
Remove
struct stackNode1 {
char* data[15]; /* define dat1a as an int */
struct stackNode1 *nextPtr; /* stackNode1 pointer */
}; /* end structure stackNode1 */
typedef struct stackNode1 StackNode1; /* synonym for struct stackNode */
typedef StackNode1 *StackNode1Ptr; /* synonym for StackNode* */
and all functions working with stackNode1
They are redundant. They are not necessary. You use struct stackNode, StackNode, and StackNodePtr data types for BOTH stacks.
The char* data[15] should be just char* data;. The memory storing the string data will be allocated by strdup. Look closely for the strdup in the input loop I gave you in my last answer.
I will look more at your problem in about 10 hours from now.
Best regards
Zlatko
EDIT:
Canno, I've looked at your code, and mostly it just needed the stackNode1 stuff to be deleted. It had a few small logic errors. I fixed them for you but you really need to learn how to read compiler errors and warnings. You should also learn how to trace through your code with a debugger. Once again, my changes are marked with /*XXX
I gave the program a quick test. It is up to you to test further.
Bye.
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
#include <string.h> /*XXX added for strdup */
/* self-referential structure */
struct stackNode {
char* data; /*XXX changed */ /* define data as an int */
struct stackNode *nextPtr; /* stackNode pointer */
}; /* end structure stackNode */
typedef struct stackNode StackNode; /* synonym for struct stackNode */
typedef StackNode *StackNodePtr; /* synonym for StackNode* */
/* prototypes */
void push( StackNodePtr *topPtr, char* info );
char* pop( StackNodePtr *topPtr );
int isEmpty( StackNodePtr topPtr );
void printStack( StackNodePtr currentPtr );
int inputString(char* dest, int space);
int i,counter=0;
/* function main begins program execution */
int main( void )
{
StackNodePtr stackPtr = NULL; /* points to stack top */
StackNodePtr stack1Ptr = NULL; /* points to stack top */
char value[16];
clrscr(); /*Clear screen*/
for(i = 0; i < 10;)
{
printf( "Enter [%2d] string: " ,i);
/*XXX gets(value); you don't need gets(value), the inputString routine handles input */
if (inputString(value, sizeof(value)) >= sizeof(value))
{
printf("Thats too long, try again\n");
}
else
{
i++;
counter+=1;
push( &stackPtr, strdup(value) );
}
}
clrscr();
/*XXX you don't need this, value is a single string, not an array of strings
for (i=0;i<10;i++)
printf("%s \n",value[i]);
*/
printStack( stackPtr ); /*XXX instead, I think you want this */
for(i=0;i<counter;i++)
{
push( &stack1Ptr, pop( &stackPtr ) );
printStack( stack1Ptr );
}
getch();
return 0; /* indicates successful termination */
} /* end main */
/* Insert a node at the stack top */
void push( StackNodePtr *topPtr, char* info )
{
StackNodePtr newPtr; /* pointer to new node */
newPtr = malloc( sizeof( StackNode ) );
/* insert the node at stack top */
if ( newPtr != NULL ) {
newPtr->data = info; /*XXX storing info, not *info, we are storing the pointer to the string, also changed == to = */
newPtr->nextPtr = *topPtr;
*topPtr = newPtr;
} /* end if */
else { /* no space available */
printf( "%s not inserted. No memory available.\n", info );
} /* end else */
} /* end function push */
/* Remove a node from the stack top */
char *pop( StackNodePtr *topPtr )
{
StackNodePtr tempPtr; /* temporary node pointer */
char* popValue; /* node value */
tempPtr = *topPtr;
popValue = ( *topPtr )->data;
*topPtr = ( *topPtr )->nextPtr;
free( tempPtr );
return (popValue);
} /* end function pop */
/* Print the stack */
void printStack( StackNodePtr currentPtr )
{
/* if stack is empty */
if ( currentPtr == NULL ) {
printf( "The stack is empty.\n\n" );
} /* end if */
else {
printf( "The stack is:\n" );
/* while not the end of the stack */
while ( currentPtr != NULL ) {
printf( "%s --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
} /* end while */
printf( "NULL\n" );
} /* end else */
} /* end function printList */
/* Return 1 if the stack is empty, 0 otherwise */
int isEmpty( StackNodePtr topPtr )
{
return topPtr == NULL;
} /* end function isEmpty */
int inputString(char* dest, int space)
{
int count = 0;
if (dest == NULL || space == 0) return 0;
while(1)
{
char c = getc(stdin);
if (c == '\n' || c == EOF)
{
if (space) *dest = 0;
break;
}
++count;
if (space > 1)
{
*dest++ = c;
--space;
}
if (space == 1)
{
*dest = 0;
--space;
}
}
return count;
}