Hello, I have been struggling with this project for several days. This is all about doubly Linked List and I have to add reverse section with given code in project. So far it was doing good. I learned a lot from this but the only thing I had issue is deleting reverse characters. I noticed if I have certain characters, it won't have issue to delete the characters but when I have certain characters, the reversed charcter displayed different character like if I want to delete n, this project will display E with dash mark above it. I don't know how to address this problem. Last thing, I'm trying to learn this in program c not c++.
Also, when it comes to deleting the last node, the program kept running infinite. I tried to add system pause or break but nothing works.
(Sorry, English isn't my first language. I hoped I clarified as I can! And thank you for taking time to help me to understand this :) )
Here's my code:
#include<stdio.h>
#include<stdlib.h>
// self-referential structure
struct queueNode {
char data; // define data as a char
struct queueNode *nextPtr; // queueNode pointer
struct queueNode *prevPtr;
}; // end structure queueNode
typedef struct queueNode QueueNode;
typedef QueueNode *QueueNodePtr;
// function prototypes
void printQueue( QueueNodePtr currentPtr );
int isEmpty( QueueNodePtr headPtr );
char dequeue( QueueNodePtr *headPtr, char value );
void enqueue( QueueNodePtr *headPtr, char value );
void instructions( void );
void reverse( QueueNodePtr currentPtr);
// function main begins program execution
int main( void )
{
QueueNodePtr headPtr = NULL; // initialize headPtr
QueueNodePtr tailPtr = NULL; // initialize tailPtr
unsigned int choice; // user's menu choice
char item; // char input by user
instructions(); // display the menu
printf( "%s", "? " );
scanf( "%u", &choice );
// while user does not enter 3
while ( choice != 3 ) {
switch( choice ) {
// enqueue value
case 1:
printf( "%s", "Enter a character: " );
scanf( "\n%c", &item );
enqueue( &headPtr, item );
printQueue( headPtr );
reverse( headPtr );
break;
// dequeue value
case 2:
// if queue is not empty
if ( !isEmpty( headPtr ) )
{
printf("Enter a character to be deleted:");
scanf("\n%c", &item);
if(dequeue(&headPtr, item))
{
printf("%c deleted.\n", item);
printQueue(headPtr);
reverse(headPtr);
}
else{
printf("%c not found.\n", item);
}
}
else
{
printf("List is empty.\n");
}
break;
default:
puts( "Invalid choice.\n" );
instructions();
break;
} // end switch
printf( "%s", "? " );
scanf( "%u", &choice );
} // end while
puts( "End of run." );
} // end main
// display program instructions to user
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\n" );
} // end function instructions
// insert a node in at queue tail
void enqueue( QueueNodePtr *headPtr, char value )
{
QueueNodePtr newPtr; // pointer to new node
QueueNodePtr currentPtr;
QueueNodePtr previousPtr;
newPtr = (QueueNodePtr)malloc(sizeof(QueueNode));
if ( newPtr != NULL ) { // is space available
newPtr->data = value;
newPtr->nextPtr = NULL;
newPtr->prevPtr = NULL;
previousPtr = NULL;
currentPtr = *headPtr;
while(currentPtr != NULL && value > currentPtr-> data)
{
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
if(previousPtr == NULL)
{
newPtr->nextPtr = *headPtr;
if(*headPtr != NULL)
(*headPtr)->prevPtr = newPtr;
*headPtr = newPtr;
}
else
{
newPtr->prevPtr = previousPtr;
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = currentPtr;
if(currentPtr != NULL)
currentPtr->prevPtr = newPtr;
}
} // end if
else {
printf( "%c not inserted. No memory available.\n", value );
} // end else
} // end function enqueue
// remove node from queue head
char dequeue( QueueNodePtr *headPtr, char value )
{
QueueNodePtr tempPtr; // temporary node pointer
QueueNodePtr currentPtr;
QueueNodePtr previousPtr;
if(value == ( *headPtr )->data)
{
tempPtr = *headPtr;
if((*headPtr)->nextPtr !=NULL)
{
*headPtr = ( *headPtr )->nextPtr;
(*headPtr)->prevPtr = NULL;
}
free(tempPtr);
return value;
}
else
{
previousPtr = *headPtr;
currentPtr = (*headPtr)->nextPtr;
while(currentPtr != NULL && currentPtr->data != value)
{
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
if(currentPtr !=NULL)
{
tempPtr = currentPtr;
previousPtr->nextPtr= currentPtr->nextPtr;
free(tempPtr);
return value;
}
}
return '\0';
} // end function dequeue
// return 1 if the queue is empty, 0 otherwise
int isEmpty( QueueNodePtr headPtr )
{
return headPtr == NULL;
} // end function isEmpty
// print the queue
void printQueue( QueueNodePtr currentPtr )
{
// if queue is empty
if ( currentPtr == NULL ) {
puts( "List is empty.\n" );
} // end if
else {
puts( "The list is:" );
// while not end of queue
while ( currentPtr != NULL ) {
printf( "%c --> ", currentPtr->data );
currentPtr = currentPtr->nextPtr;
} // end while
puts( "NULL\n" );
} // end else
} // end function printQueue
void reverse(QueueNodePtr currentPtr )
{
QueueNodePtr tempPtr = NULL;
if(currentPtr == NULL)
{
puts(" ");
}
else
{
while(currentPtr != NULL)
{
tempPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
}
printf("\nThe list in reverse is: \n");
currentPtr = tempPtr;
while (currentPtr != NULL)
{
printf(" %c -->", currentPtr->data);
currentPtr = currentPtr->prevPtr;
}
printf("NULL");
printf("\n\n");
}
}