如何阻止程序运行?

时间:2016-03-29 17:35:04

标签: c

我包括三个休息;在三种不同的情况下。我测试了案例2,它停止了程序;然而对于案例1,即使它已经破裂,它也会继续下去;我尝试了所有可能的解决方案来解决它。我无法理解。

我该如何解决?

#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, QueueNodePtr *tailPtr );
void enqueue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr, 
   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, &tailPtr, item );
            printQueue( headPtr );
            reverse( headPtr );
            break;
         // dequeue value
         case 2:
            // if queue is not empty

            if ( !isEmpty( headPtr ) ) { 

               item = dequeue( &headPtr, &tailPtr );
               printf( "%c has been dequeued.\n", item );
            } // end if

            printQueue( headPtr );
            reverse( headPtr );
            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, QueueNodePtr *tailPtr, 
   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, QueueNodePtr *tailPtr )
{ 
   char value; // node value
   QueueNodePtr tempPtr; // temporary node pointer
   QueueNodePtr currentPtr; 
   QueueNodePtr previousPtr;


   if(value == ( *headPtr )->data)
   {
        tempPtr = *headPtr;              
        *headPtr = ( *headPtr )->nextPtr;
        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;

    while(currentPtr != NULL)
    {
        tempPtr = currentPtr; 
        currentPtr = currentPtr->nextPtr; 
    }
    printf("\nThe list in reverse is:");
    printf("NULL");

    currentPtr = tempPtr; 
    while(currentPtr != NULL)
    {
        printf(" <-- %c", currentPtr->data);
    }
    printf("NULL\n");
}

1 个答案:

答案 0 :(得分:1)

value下面的if-statment中使用了未经证实的:

 char value; // node value
 QueueNodePtr tempPtr; // temporary node pointer
 QueueNodePtr currentPtr; 
 QueueNodePtr previousPtr;


 if(value == ( *headPtr )->data)

这样做至少会引发随机行为,而不是将其称为未定义的行为。