我在C語言中創建了雙佇列(deque),我要求用戶輸入操作代碼來執行以下操作,直到他/她想要。
我在C語言中創建了一個雙佇列(deque)。
所有函式都作業正常,但當我呼叫deque_from_back()時,程式立即退出。我沒有找到任何發生這種情況的原因。
請看deque_from_back()函式。
//使用鏈接串列實作雙佇列。
#include<stdio.h>/span>
#include<stdlib.h>/span>
typedef struct Queue Queue;
struct Queue{>
int data;
佇列 *next;
};
typedef enum STATUS STATUS; /span>
enum STATUS {FAILED, SUCCESS, MEMFAILED, UNDER_FLOW};
//原型宣告
STATUS enque_from_back(Queue **front,Queue **rear,int val);
void deque_from_front(佇列**front)。
STATUS enque_from_front(Queue **front,Queue **rear,int val) ;
void deque_from_back(佇列 ** , 佇列 **)。
void display(Queue *q)。
void menu()。
//驅動代碼
int main(){
佇列 *front = NULL, *rear = NULL;
int num,OPR_CD;
STATUS標志。
while(OPR_CD != 4){
printf("
------------------------------------------------------------------");
menu()。
printf("
選擇一個選項:")。)
scanf("%d",&OPR_cd)。
if(OPR_CD < 0 || opr_cd > 5){
printf("
輸入有效的操作代碼。")。)
continue。
}
//從末端插入資料。
else if(OPR_CD == 1) {
printf("
輸入數字,從后面的enque : ")。
scanf("%d"/span>,&num)。
flag = enque_from_back(&front,&rear,num)。
if(flag == MEMFAILED) puts("記憶體失敗。
")。)
else printf("%d enqued to end.
",num)。
}
//從頭開始插入資料。
else if(OPR_CD == 3){
printf("
從開始輸入數字到enque : ")。
scanf("%d"/span>,&num)。
flag = enque_from_front(&front,&rear,num)。
if(flag == MEMFAILED) puts("記憶體失敗。
")。)
else printf("%d enqued to start.
",num)。
}
// deque data from start[/span]。
else if(OPR_CD == 2){
if(front == NULL){
printf("
Queue is underflow nothing to delete.")。)
繼續。
}
num = front->data;
deque_from_front(&front)。
printf("
%d從佇列中移除",num)。
}
//devque data from end
else if(OPR_CD == 4){
num = rear->資料。
if(front == NULL){
printf("
Queue is underflow nothing to delete.")。)
continue。
}
deque_from_back(&front,&rear)。
printf("
%d從佇列中移除",num)。
}
//顯示佇列中的資料。
else if(OPR_CD == 5){
display(front);
}
else if(OPR_CD == 0){
printf("
EXITTED PROGRAM....")。)
return 0;
}
return 0;
}
// function to insert item in queue from end[/span].
STATUS enque_from_back(Queue **front,Queue **rear,int val){
Queue *tem_queue = (Queue*)malloc(sizeof(Queue))。
if(tem_queue == NULL)
return MEMFAILED;
tem_queue->data = val;
tem_queue->next = NULL;
if(*front == NULL)
*front = tem_queue;
else(*front = NULL)
(*rear)->Next = tem_queue;
*rear = tem_queue;
return SUCCESS;
}
//在佇列中從頭插入專案的函式。
STATUS enque_from_front(Queue **front,Queue **rear,int val){
Queue *tem_node = (Queue*)malloc(sizeof(Queue))。
if(tem_node == NULL)
return MEMFAILED;
tem_node->data = val;
tem_node->next = *front;
if(*rear == NULL)
*rear = tem_node;
*front = tem_node;
return SUCCESS;
}
//函式從開始洗掉資料。
void deque_from_front(佇列**front){
Queue *tem_front = *front;
*front = tem_front->接下來。
free(tem_front)。
}
//函式從末端洗掉。
void deque_from_back(佇列 **front, 佇列 **last){
佇列*sec_last。
Queue *node_to_delete = *front;
while (node_to_delete->next != NULL){
sec_last = node_to_delete;
node_to_delete = node_to_delete->next;
}
sec_last->next = NULL;
*last = sec_last;
free(node_to_delete)。
}
//function to display item in queue.
void display(Queue *q){
if(q == NULL){
printf("
Queue is empty nothing to display")。)
return;
}
printf("佇列中的資料:
")。)
for(; q != NULL; q = q->next)
printf("%d"/span>,q->data)。
}
//顯示選單的功能。
void menu(){
printf("
-------------------------------
|-------------------------------|
|按1 -> ENQUE |
|按2 -> DEQUE |按3 -> ENQUE
|按3 ->從起始點ENQUE |
|按4 ->從頭到尾洗掉 |按4 ->從尾到頭洗掉
|按5 -> DISPLAY QUE |
|按0 -> 退出 |
-------------------------------
")。)
}
uj5u.com熱心網友回復:
看你的while條件: while(OPR_CD != 4)
當deque_from_back被選中時,OPR_CD變成了4,因此它退出了程式。
你的條件應該是 while(OPR_CD!=0)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/315489.html
標籤:
