看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) cygwin + gcc 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 在練習寫 queue ,但問題不是在 queue 上 為什麼在 scanf 輸入 char 時需要輸入 \n,例:scanf( "\n%c", &data ); 而我用類似的方法試寫一個 stack 如果是在 int 時不需要加 \n,例:scanf( "%d", &data ); 餵入的資料(Input): 單一的 char 例如 u, n, c, ... 預期的正確結果(Expected Output): u --> n --> c --> NULL 照書上範例的列印方式 錯誤結果(Wrong Output): --> --> --> --> NULL 程式碼(Code):(請善用置底文網頁, 記得排版) 很長 請包涵 = = #include<stdio.h> #include<stdlib.h> struct queueNode { char data; struct queueNode *nextPtr; }; typedef struct queueNode QueueNode; typedef QueueNode *QueueNodePtr; void enqueue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr, char value ); char dequeue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr ); int isEmpty( QueueNodePtr headPtr ); void printQueue( QueueNodePtr currentPtr ); void instructions( void ); int main( void ){ QueueNodePtr headPtr = NULL; QueueNodePtr tailPtr = NULL; int choice; char value; instructions(); printf( "? " ); scanf( "%d", &choice ); while ( choice != 3 ) { switch ( choice ) { case 1: printf( "Please input a character: " ); scanf( "\n%c", &value ); enqueue( &headPtr, &tailPtr, value ); printQueue( headPtr ); break; case 2: if ( !isEmpty( headPtr ) ) { value = dequeue( &headPtr, &tailPtr ); printf( "%c has been dequeued.\n", value ); } printQueue( headPtr ); break; default: printf( "Invalid choice.\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 the program.\n" ); } 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 *headPtr, QueueNodePtr *tailPtr ) { QueueNodePtr tempPtr; char value; tempPtr = *headPtr; value = tempPtr->data; *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( "The queue is empty.\n" ); } else { printf( "The queue is: \n" ); while (currentPtr != NULL ) { printf( "%c --> ", currentPtr->data ); currentPtr = currentPtr->nextPtr; } printf( "NULL\n" ); }} 補充說明(Supplement):※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.58.141.8 ※ 編輯: Neisseria 來自: 61.58.141.8 (08/03 20:44)
diabloevagto:很長怎麼不用置頂網址? 08/03 21:45
lsc36:如果前面有\n先把它吃掉 不然你得到的字元會是\n 08/03 22:42
lsc36:scanf(" %c ", &value) 也可以 08/03 22:43
dibery:scanf不會略過換行符號,用scanf("%*c")可以憑空"吃"掉一個 08/04 00:00
dibery:字元,可以這樣把換行符號吃掉,但\n比較直覺啦 08/04 00:01
※ 編輯: Neisseria (112.104.128.230), 12/24/2018 06:34:04