Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Queue's

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am new at this game and was requesting some help from a pro. I cant figure out these errors while debugging. I fixed most of them but there are a few left. Thanks for the help.
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"


struct queueNode
{ /*self-referential structure*/
char data;
struct queueNode *nextPtr;
};

typedef struct queueNode QueueNode;
typedef QueueNode *QueueNodePtr ;

/* function prototype*/

void printQueue ( QueueNodePtr );
int isEmpty( QueueNodePtr);
char dequeue( QueueNodePtr *, QueueNodePtr * );
void enqueue( QueueNodePtr *, QueueNodePtr *, char );
void instructions (void);

int main()
{

QueueNodePtr headPtr = NULL;
QueueNodePtr tailPtr = NULL;
int choice;
char item;

instructions ();
printf("?");
scanf("%d", &choice );

while ( choice !=3 )
{

switch( choice )
{

case 1:
printf( "Enter a character: ");
scanf("\n%c", &item );
enqueue( &headPtr, &tailPtr, item );
printQueue ( headPtr );
break;
case 2:
if ( !isEmpty( headPtr ) )
{
item = dequeue( &headPtr, &tailPtr );
printf("%c has been dequeued.\n", item );
}

printQueue ( headPtr );
break;
default:
printf( "Bad Choice. \n\n" );
instructions();
break;
}

printf("?");
scanf("%d", &choice );

}

printf(" End of run.\n")
return 0;
}

void instructions (void)
{
printf("Enter your choice:\n"
" 1 to add an item to the queue\n"
" 2 to remove an item from the queue\n"
" 3 to end\" );
}

void enqueue ( QueueNodePtr *headPtr, QueueNodePtr *tailPtr,char value)
{

QueueNodePtr newPtr;

newPtr = malloc ( sizeof( QueueNode) );

if ( newPtr != NULL)
{
newPtr->data = value;
newPtr->nextPtr = NULL;

if ( isEmpty( *headPtr ) )
*headPtr = newPtr;
else
( *tailPtr )->nextPtr = newPtr;

*tailPtr = newPtr;
}
else
printf(" %c not inserted. No memory available.\n",value);
}


char dequeue ( QueueNodePtr *headerPtr, QueueNodePtr *tailPtr )
{
char value;
QueueNodePtr tempPtr;

value = (*headPtr )->data;
tempPtr= *headPtr;

*headPtr = ( headPtr )->nextPtr;

if ( *headPtr == NULL)
*tailPtr = NULL;

free ( tempPtr );
return value;

}


int isEmpty ( QueueNodePtr headPtr)

{

return headPtr == NULL;

}


void printQueue ( QueueNodePtr currentPtr )
{
if ( currentPtr == NULL )
printf("Queue is empty.\n\n");


while ( currentPtr != NULL )
{
printf( "%c --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;

}

printf("Null\n\n" );

}
 
here are my errors. I figured you would run it through a compiler, but then again, I am new at this stuff. Thanks a bunch for the help.
RR

\My Documents\OOP\project1\p1.cpp(84) : error C2440: '=' : cannot convert from 'void *' to 'struct queueNode *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
C:\My Documents\OOP\project1\p1.cpp(108) : error C2065: 'headPtr' : undeclared identifier
C:\My Documents\OOP\project1\p1.cpp(108) : error C2100: illegal indirection
C:\My Documents\OOP\project1\p1.cpp(108) : error C2227: left of '->data' must point to class/struct/union
C:\My Documents\OOP\project1\p1.cpp(109) : error C2100: illegal indirection
C:\My Documents\OOP\project1\p1.cpp(109) : error C2440: '=' : cannot convert from 'int' to 'struct queueNode *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\My Documents\OOP\project1\p1.cpp(111) : error C2100: illegal indirection
C:\My Documents\OOP\project1\p1.cpp(111) : error C2227: left of '->nextPtr' must point to class/struct/union
C:\My Documents\OOP\project1\p1.cpp(113) : error C2100: illegal indirection
Error executing cl.exe.

project1.exe - 9 error(s), 0 warning(s)
 
"\My Documents\OOP\project1\p1.cpp(84) : error C2440: '=' : cannot convert from 'void *' to 'struct queueNode *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast"

--> when you call malloc you are not casting to the pointer type you need. All calls to malloc or calloc return a void pointer.
Example: int intPtr = (int *)malloc(5 * sizeof(int));

--> All your other errors seem to be from a typo in your dequeue function. Your prototype uses headerPtr while you use headPtr.

Regards,
Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top