数据结构严薇敏——队列的顺序存储(C语言)

和栈相反,队列是一种先进先出的线性表。只能在表的一端进行插入,另一端进行删除。(类似与我们排队买东西,先到先得)

队列中,允许插入的一端叫做队尾,允许删除的一端叫队头。

代码实现 :

顺序数据结构定义为:

typedef int ElemType;
typedef struct SqQueue
{
    ElemType data[MAX_SIZE];
    int size;       //用来计数元素个数
}SqQueue;

头文件:#include"SqQueueh.h"

#ifndef SQQUEUEH_H_INCLUDED
#define SQQUEUEH_H_INCLUDED

#include<stdio.h>
#include<stdlib.h>

#define MAX_SIZE 20
#define TRUE 1
#define FALSE 0

typedef int ElemType;
typedef struct SqQueue
{
    ElemType data[MAX_SIZE];
    int size;
}SqQueue;

//初始化一个空队
SqQueue *Init_Queue();
//销毁
void Destory_Queue(SqQueue *S);
//清空队列
void Clear_Queue(SqQueue *S);
//是否为空
int IsEmpty_Queue(SqQueue *S);
//队列的长度
int Length_Queue(SqQueue *S);
//返回队头元素
ElemType GetHead_Queue(SqQueue *S);
//进队
void Enter_Queue(SqQueue *S,ElemType e);
//出队
void Delete_Queue(SqQueue *S);
//遍历
void Traverse_Queue(SqQueue *S);
#endif // SQQUEUEH_H_INCLUDED

对头文件中函数的实现SqCQueue.c

#include"SqQueueh.h"

//以数组的起始位置作为最头,以结尾作为队尾
//在队尾进行插入,在对头进行删除
//初始化一个空队
SqQueue *Init_Queue()
{
    SqQueue *S = (SqQueue *)malloc(sizeof(SqQueue));
    int i;
    for(i = 0; i < MAX_SIZE; i++)
    {
        S->data[i] = 0;
    }
    S->size = 0;
    printf("队列初始化成功!\n");
    return S;

}
//销毁
void Destory_Queue(SqQueue *S)
{
    if(S != NULL)
        free(S);
    else
        return;
    printf("\n队列销毁成功!\n");
}
//清空队列
void Clear_Queue(SqQueue *S)
{
    if(IsEmpty_Queue(S))
    {
        printf("队列为空,不需要进行清空!\n");
        return;
    }
    int i;
    for( i = 0 ; i < S->size ; i++)
    {
        S->data[i] = 0;
    }
    S->size = 0;
    printf("\n清空队列成功!\n");
}
//是否为空
int IsEmpty_Queue(SqQueue *S)
{
    if(S->size == 0)
        return TRUE;
    else
        return FALSE;
}
//队列的长度
int Length_Queue(SqQueue *S)
{
    return S->size;
}
//返回队头元素
ElemType GetHead_Queue(SqQueue *S)
{
    return S->data[0];
}
//进队
void Enter_Queue(SqQueue *S,ElemType e)
{
    if( S == NULL)
    {
        printf("队列不存在,请创建一个队列!\n");
        return;
    }
    S->data[S->size] = e;
    S->size++;
}
//出队
void Delete_Queue(SqQueue *S)
{
    if(IsEmpty_Queue(S))
    {
        printf("队列为空,没有可以删除的元素!\n");
        return;
    }
    int i;
    for( i = 0; i < S->size; i++)
    {
        S->data[i] = S->data[i + 1];
    }
    S->data[i] = 0;
    S->size--;
}
//遍历
void Traverse_Queue(SqQueue *S)
{
    if(S == NULL)
    {
        printf("队列不存在!\n");
        return;
    }
    if(IsEmpty_Queue(S))
    {
        printf("队为空!\n");
        return;
    }
     int i;
     for(i = 0; i < S->size; i++)
     {
         printf("%d\t",S->data[i]);
     }
     printf("\n遍历队列成功!\n");
}

主函数main.c

#include<stdio.h>
#include<stdlib.h>

#include"SqQueueh.h"

void test()              //测试队列
{
    SqQueue *S = Init_Queue();
    int i;
    for(i = 0; i < 5; i++)
    {
        Enter_Queue(S,i);
    }
    Traverse_Queue(S);
    printf("队列当前的长度为:%d\n",Length_Queue(S));
    Delete_Queue(S);
    Traverse_Queue(S);
    printf("队列当前的长度为:%d\n\n",Length_Queue(S));
    printf("当前的队头元素是:%d\n",GetHead_Queue(S));
    Clear_Queue(S);
    printf("队列当前的长度为:%d\n\n",Length_Queue(S));
    Destory_Queue(S);

}

int main()
{
    test();
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/attention_0/article/details/81257073