【发布时间】:2014-06-06 20:20:36
【问题描述】:
我有这个 C 代码。我制作了一个模拟队列的结构(在后面插入并从前面提取)。插入似乎可以工作,但是当我想用这段代码删除一个节点时,什么也没有发生。
nodo* dequeue(nodo* head) {
if(head==NULL) {
return NULL; //nothing to extract
}
else {
nodo* temp=malloc(sizeof(nodo *));
temp=head;
head=head->next;
return temp;
}
}
这是结构:
typedef struct coda{
int x;
char *y;
char *t;
int z;
struct coda *next;
}nodo;
这里是主要的
#include "list.h"
int main(void){
nodo * testa;
char* hi="hi";
char* bye="bye";
testa=enqueue(15,hi,bye,1,NULL);
enqueue(16,hi,bye,1,testa);
enqueue(17,hi,bye,1,testa);
enqueue(18,hi,bye,1,testa);
printList(testa);
nodo *newHead = dequeue(&testa);
printList(testa);
}
以及其余的代码
nodo* enqueue(int codArt,char *descrArt,char *indDest,int status,struct coda* head){
if(head==NULL){
nodo *nuovo_nodo=malloc(sizeof(nodo));
nuovo_nodo->x=codArt;
nuovo_nodo->y=descrArt;
nuovo_nodo->t=indDest;
nuovo_nodo->z=status;
nuovo_nodo->next=NULL;
return nuovo_nodo;
}else if(head->next!=NULL)
enqueue(codArt,descrArt,indDest,status,head->next);
else
head->next=enqueue(codArt,descrArt,indDest,status,head->next);
}
void printList(struct coda* head){
struct coda* thead=head;
while(thead!=NULL){
printf("--> %d ",thead->codiceArticolo);
thead=thead->next;
}
printf("\n");
}
【问题讨论】:
-
你能把剩下的代码贴出来吗?
-
是的,我现在就添加到 OP 中