2013-05-28 62 views
1

我寫過一個關於隊列和動態內存分配的程序。這是我的程序需要做的 - 將值插入到隊列中並將其從隊列中移除;那很簡單。 但我的問題是,它只是打印變量的名稱分配值,程序不響應。程序產生錯誤輸出

這裏是我的程序:

#include <stdio.h> 
#define MAX 180 

struct cakes{ 
     int spongecake; 
     int meringue; 
     int chocalate; 
     int red_velvet; 
     struct newcake *next; 
}; 

struct Queue{ 
     int front; 
     int rear; 
     int count; 
     int cake[10]; 
}; 

void init(struct Queue *); 
int isFull(struct Queue *); 
void insert(struct Queue *,int); 
int isEmpty(struct Queue *); 
int removes(struct Queue *); 

void cake_order(struct cakes *); 
void order_out(struct cakes *); 

main() 
{ 
     struct cakes *head; 

     head=(struct cakes *)malloc(sizeof(struct cakes)); 
     cake_order(&head); //this is a seperate function and it works perfectly 
     head->next=(struct cakes *)malloc(sizeof(struct cakes)); 
     order_out(&head->next); 
} 
void init(struct Queue *q) 
{ 
     q->front=0; 
     q->rear=10-1; 
     q->count=0; 
} 

int isFull(struct Queue *q) 
{ 
     if(q->count==10) 
     { 
       return 1; 
     } 
     else 
     { 
       return 0; 
     } 
} 

void insert(struct Queue *q,int x) 
{ 
     if(!isFull(q)) 
     { 
       q->rear=(q->rear+1)%10; 
       q->cake[q->rear]=x; 
       q->count++; 
     } 

} 

int isEmpty(struct Queue *q) 
{ 
     if(q->count==0) 
     { 
       return 1; 
     } 
     else 
     { 
       return 0; 
     } 
} 

int removes(struct Queue *q) 
{ 
     int caked=NULL; 

     if(!isEmpty(q)) 
     { 
       caked=q->cake[q->front]; 
       q->front=(q->front+1)%10; 
       q->count--; 
       return caked; 
     } 
} 

void order_out(struct cakes *order) 
{ 
     struct Queue s; 
     int i; 

     order->spongecake=20; 
     order->meringue=75; 
     order->chocalate=40; 
     order->red_velvet=30; 

     init(&s); 

     for(i=0;i<10;i++) 
     { 
       insert(&s,order->chocalate); 
       insert(&s,order->spongecake); 
       insert(&s,order->meringue); 
       insert(&s,order->red_velvet); 
    } 

     while(!isEmpty(&s)) 
     { 
       printf("%d",removes(&s)); 
     } 
} 

什麼,似乎這裏是什麼問題? 我是C新手,所以用這種語言調試時有點慢。

謝謝你的時間。

這裏是輸出:

enter image description here

+0

'struct newcake'是一個'typedef'嗎? –

+0

其指向新結構的指針 –

+2

然後爲什麼它是這樣的:'head-> next =(struct cakes *)malloc(sizeof(struct cakes));'而不是'head-> next =(struct newcake *)malloc (sizeof(struct newcake));'??你的意思是'struct cakes * next',而不是'struct newcake * next' –

回答

1

很多問題在這裏,首先它會更好,如果main被宣佈適當爲int main(),然後它在結束例如返回的值return 0;像:

int main() 
{ 
    .... // code 

    return 0; // normally 0 is returned if execution has been successful 
} 

似乎有其他問題的代碼,我不能編譯它,例如有在order_out()結束(while循環之後)沒有右大括號。

如果您提供了cake_order()函數,這也會很好。

它也沒有包含說stdlib.h和45行(head=(struct cakes *)malloc(sizeof(struct cakes));)我注意到你投了malloc的結果,which is not necessary

如果我可以進一步添加,請不要記住free()已分配的內存爲malloc()。我在代碼中沒有看到一條free()聲明。

+0

非常感謝回覆,即使在編輯之後,它仍然是 –

+1

我已經給代碼添加了一些其他建議,我建議你考慮一下,至少它會讓你的程序更具可預測性。我不知道你的編譯器是否正在吐出任何警告,但是我相信它確實可以,最好也考慮到這一點,只是因爲編譯的代碼並不意味着它可以正常工作。 – Nobilis