鏈表C語言簡單實現

使用C語言完成的鏈表,分成linkQueue.h,和linkQueue.c兩個文件,後附有測試文件main.c

linkQueue.h

#ifndef LINKQUEUE_H
#define LINKQUEUE_H

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//定義宏常量
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR -1
#define OVERFLOW -1
//設置一個數據類型
typedef int QElemType;
//鏈式結構體
typedef struct QNode
{
	QElemType data;
	struct QNode * next;
}QNode;
typedef QNode* QueuePtr;
//隊列結構體
typedef struct
{
	QueuePtr front;		//頭指針
	QueuePtr rear;		//尾指針
	int queuesize;       //隊列長度
}LinkQueue;				//隊列的鏈式存儲表示

/*鏈隊列函數列表*/
int InitQueue(LinkQueue *Q);	//初始化隊列

void ClearQueue(LinkQueue *Q);	//置空Q

void DestroyQueue(LinkQueue *Q);	//銷燬Q

int QueueEmpty(LinkQueue Q);	//判斷Q是否爲空

int QueueLength(LinkQueue Q);	//返回Q元素的個數

int GetHead(LinkQueue Q,QElemType *e);	//獲取隊頭元素

int EnQueue(LinkQueue *Q,QElemType e);	//元素e入隊

int DeQueue(LinkQueue *Q,QElemType *e);	//元素e出隊

void QueueTraverse(LinkQueue Q, void(visit)(QElemType));	//訪問元素

#endif

 

linkQueue.c

 

/***************************************
author:e-dawn
mail:[email protected]
blog:e-dawn.github.io
desc:隊列的鏈表實現函數

**************************************/

#include "linkQueue.h"

/*
return:
	OK:表示初始化成功
parm:
	*Q:表示要開闢的空間的隊列的地址

*/
int InitQueue(LinkQueue *Q)
{
    assert(NULL != Q);

    Q->front = Q->rear = NULL;
    Q->queuesize = 0;
    return OK;
}

/*
return:
	無
parm:
	*Q:要清空的隊列

*/
void ClearQueue(LinkQueue *Q)
{
    assert(NULL != Q);
	//釋放已分配空間
    QueuePtr p = Q->front;
    while(NULL != p)
    {
        QueuePtr q = p->next;
        free(p);
        p = q;
    }
	//置空
    Q->front = Q->rear = NULL;
    Q->queuesize = 0;
}
/*
return:
	無
parm:
	*Q:要銷燬的隊列

*/
void DestroyQueue(LinkQueue *Q)
{
    assert(NULL != Q);

    ClearQueue(Q);
    Q = NULL;
}

/*
return:
	TRUE:爲空
	FALSE:表示非空
parm:
	*Q:表示要判斷的隊列

*/
int QueueEmpty(LinkQueue Q)
{
    if(Q.front == Q.rear)
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}


/*
return:
	隊列的元素個數
parm:
	*Q:要獲得個數的隊列

*/
int QueueLength(LinkQueue Q)
{
    return Q.queuesize;
}
/*
return:
	OK:表示取隊首元素成功
	ERROR:表示取隊首元素失敗
parm:
	*Q:表示要隊列
	*e:存放隊列首元素的變量
*/
int GetHead(LinkQueue Q,QElemType *e)
{
    if(NULL == Q.front)
    {
        printf("Q is empty!\n");
        return *e = ERROR;
    }
    else
    {
        *e = Q.front->data;
    }
    return OK;
}
/*
return:
	OK:表示入隊成功
	OVERFLOW:表示入隊失敗
parm:
	*Q:表示隊列
	e:表示要入隊的元素
*/
int EnQueue(LinkQueue *Q,QElemType e)
{
    assert(NULL != Q);

    QueuePtr p = (QueuePtr) malloc( sizeof(QNode) );
	if(NULL == p)
	{
		printf("init memory overflow\n");
		return	OVERFLOW;
	}

	p->data = e;
	p->next = NULL;

	if(Q->rear == NULL)
    {
        Q->front = Q->rear = p;
    }
    else
    {
        Q->rear->next = p;
        Q->rear = p;
    }

    Q->queuesize++;
    return OK;
}
/*
return:
	OK:表示出隊成功
	ERROR:表示出隊失敗
parm:
	*Q:表示要出隊的隊列
*/
int DeQueue(LinkQueue *Q,QElemType *e)
{
    assert(NULL != Q);

    if(NULL == Q->front)
    {
        printf("Q is empty!\n");
        return *e = ERROR;
    }
	//front指向下一個結點
    *e = Q->front->data;
    QueuePtr p = Q->front->next;
	
    free(Q->front);
    Q->queuesize--;
    
	Q->front = p;
	
    return OK;
}
/*
return:
	無
parm:
	*Q:表示要遍歷的隊列
*/
void QueueTraverse(LinkQueue Q, void(visit)(QElemType))
{
    QueuePtr p = Q.front;
    while(NULL != p)
    {
        visit(p->data);
        p = p->next;
    }
}

測試代碼main.c

#include "linkQueue.h"

void visit(QElemType e)
{
    printf("%d\n", e);
}


int main()
{
    LinkQueue q;
    InitQueue(&q);
    puts("隊列是否爲空");
    printf("%s\n", QueueEmpty(q) ? "is empty" : "is not empty");
    puts("當前隊列長度");
    printf("%d\n", QueueLength(q));

    printf("Please input 6 elements:\n");
    int i,e;
    for(i = 0; i < 6; i++)
    {
        scanf("%d", &e);
        EnQueue(&q, e);
        puts("當前隊列長度");
        printf("%d\n", QueueLength(q));
    }

    QueueTraverse(q, visit);

    puts("刪除元素");
    for(i = 0; i < 2; i++)
    {
        DeQueue(&q, &e);
        puts("刪除元素的值爲:\n");
        printf("%d\n", e);
    }

    puts("當前隊列長度");
    printf("%d\n", QueueLength(q));

    puts("當前棧頂:");
    GetHead(q, &e);
    printf("%d\n", e);

    puts("請空隊列中");
    ClearQueue(&q);

    puts("當前隊列長度");
    printf("%d\n", QueueLength(q));

    puts("當前棧頂:");
    GetHead(q, &e);
    printf("%d\n", e);

    puts("隊列是否爲空");
    printf("%s\n", QueueEmpty(q) ? "is empty" : "is not empty");

    puts("銷燬隊列中");
    DestroyQueue(&q);
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章