我负责在C中创建一个队列数据结构作为链表。我们的讲师给了我们大量的代码来实现一个堆栈,但我们必须调整它来创建一个队列。我们的讲师给我们的代码最终不会像我为队列写的代码那样完全编译和分段。我对结构,malloc和C一般都很陌生,所以可能会有一些我忽略的东西显而易见。使用结构和动态内存分配的队列
这里是我使用的代码:
#include <stdio.h>
#include <stdlib.h>
struct node{
int data; //contains the actual data
struct node *prev; //pointer to previous node (Closer to front)
struct node *next; //pointer to next node (Closer to back)
};
typedef struct node *Nodepointer;
struct queue{
Nodepointer front;
Nodepointer back;
};
typedef struct queue *Queuepointer;
main(){
Queuepointer myqueue; //create a queue called myqueue
init(myqueue); //initialise the queue
Nodepointer new = (Nodepointer)malloc(sizeof(struct node));
myqueue->front = new;
}
int init(Queuepointer q){
q = (Queuepointer)malloc(sizeof(struct queue));
q->front = NULL;
q->back = NULL;
}
的想法是,队列结构“包含”在队列中的第一和最后一个节点,并创建一个节点时,myQueue中被更新。但是,我甚至无法达到那个部分(流行和推动都是书面的,但为了简洁省略)。该代码是在段错误行
myqueue->front = new;
用以下GDB输出:
Program received signal SIGSEGV, Segmentation fault.
0x08048401 in main() at queue.c:27
27 myqueue->front = new;
任何想法,我做错了吗?
只是一个评论:避免使用关键字'新'。这是C++的一个保留字,如果稍后使用C++程序使用代码,会遇到一些麻烦。啊,并且总是检查malloc返回一个非NULL值(最佳做法)。 – Pierre 2010-03-21 20:19:26
关于术语的一个注意事项:这里所说的更适当地称为*双链表*,因为每个节点都有向前和向后指针。在*单链接列表中,每个节点只有一个转发指针。当一个人谈到一个*链表时*有时是一个好主意,指定你在谈论哪种。 – crazyscot 2010-03-21 20:30:44
皮埃尔,谢谢......我相信那是在讲师的代码中,但是我由于某种原因省略了它。 Crazyscot,你是对的,我的坏。今后会记住这一点,谢谢。 – 2010-03-21 20:32:57