我對編碼很陌生,但是我的所有編譯器和 IDE 都遇到了一些問題。每當我嘗試在 vs 代碼或代碼塊中編譯我的 c 代碼時,它不會給出正確的或特定的所需輸出,當我在在線編譯器上使用相同的代碼時,我會得到完美的輸出。例如,`下面的代碼列印 Hello 并在 VS Code 中退出,但在在線編譯器中它的執行方式完全相同。我不知道是編譯器問題還是 IDE 問題,但如果有人知道這一點,請回答。我只在 c 語言代碼中遇到過這種情況,在 c 中它作業正常。
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int info;
struct node *next;
} node;
typedef struct
{
node *front;
node *rear;
} queue;
void
createqueue (queue * q)
{
q->front = q->rear = NULL;
}
int
isempty (queue * q)
{
if (q->front == NULL)
return 1;
return 0;
}
void
enqueue (queue * q, int val)
{
node *p = (node *) malloc (sizeof (node));
p->info = val;
p->next = NULL;
if (isempty (q))
q->front = q->rear = p;
else
{
q->rear->next = p;
q->rear = p;
}
}
int
dequeue (queue * q)
{
int t;
t = (q->front)->info;
node *p = (node *) malloc (sizeof (node));
p = q->front;
if (q->front == q->rear)
q->front = q->rear = NULL;
else
q->front = (q->front)->next;
free (p);
return t;
}
int
queuefront (queue * q)
{
int t;
t = q->front->info;
return t;
}
void
traverse (queue * q)
{
node *p = (node *) malloc (sizeof (node));
printf ("\n Queue is: ");
p = q->front;
while (p != NULL)
{
printf ("=>%d ", p->info);
p = p->next;
}
}
int
main ()
{
int choice, info, d, val;
queue *q;
printf ("hello\n");
createqueue (q);
do
{
printf ("\n MENU \n");
printf ("\n 1.ENQUEUE \n");
printf ("\n 2.DEQUEUE \n");
printf ("\n 3.FRONT \n");
printf ("\n 4.TRAVERSE \n");
printf ("\n 5.EXIT \n");
printf ("\n ENTER YOUR CHOICE: ");
scanf ("%d", &choice);
switch (choice)
{
case 1:
printf ("enter the value");
scanf ("%d", &val);
enqueue (q, val);
break;
case 2:
if (isempty (q))
{
printf ("Queue is empty");
break;
}
info = dequeue (q);
printf ("%d ", info);
break;
case 3:
if (isempty (q))
{
printf ("Queue is empty");
break;
}
d = queuefront (q);
printf ("front= %d \n", d);
break;
case 4:
if (isempty (q))
{
printf ("Queue is empty");
break;
}
traverse (q);
break;
case 5:
exit (0);
break;
}
}
while (choice > 0 && choice < 5);
return 0;
}
`
uj5u.com熱心網友回復:
queue *q; // uninitialized pointer
createqueue (q); // function tries to dereference it.
你必須在某處q指出一點。
您還應該學習如何使用您的 IDE 進行除錯——除錯器會立即告訴您程式退出的原因(實際上它并沒有退出,而是崩潰了)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/379869.html
上一篇:請更正我的代碼程式C語言
