c语言实现链表

来源:百度知道 编辑:UC知道 时间:2024/09/27 05:44:43
#include <stdio.h>
#include <stdlib.h> // 包含exit();

#define QElemType int

typedef struct QNode{
QElemType data; //队列数据
struct QNode *next; //队列下一节点
}*QueuePtr,QNode;

typedef struct{
QueuePtr front; // 队头指针
QueuePtr rear; // 队尾指针
}LinkQueue;

// 初始化
void InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}

// 删除队列
void DestoryQueue(LinkQueue &Q)
{
while(Q.front){
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return ok;
}

// 插入队列
void EnQueue(LinkQueue &Q, QElemType e)
{
p = (QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p->data = e; p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return ok;
}

同学,看来你还是没有理解数据结构。上述程序在c和c++上实现是不一样的。
c++上可以用&传值(实际是共有一个空间),在c上是通不过的(c中不能这么用),在vc++中建立文件是文件尾不加.c就可以了。但是除了上述错误,你的程序中的问题不少,比如:变量不定义就用,赋值错误,你的OK返回值,一会大写,一会小写,并且没有声明。。。。等。
看来你还是没有理解数据结构的思想。

编译通过了:

#include <stdio.h>
#include <stdlib.h> // 包含exit();

#define QElemType int
#define OVERFLOW -1
#define OK 1
#define ERROR 0

typedef struct QNode{
QElemType data; //队列数据
struct QNode *next; //队列下一节点
}*QueuePtr,QNode;

typedef struct{
QueuePtr front; // 队头指针
QueuePtr rear; // 队尾指针
}LinkQueue;

// 初始化
int InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}

// 删除队列
int DestoryQueue(LinkQueue &Q)
{
while(Q.front){
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;