数据结构c语言之链式队列(源码奉上)


初始化队列


Status InitQueue(LinkQueue *p)
    {
     p->front=p->rear=(Node *)malloc(sizeof(Node));
     if(p->front==NULL)
     return NO;
     p->front->next=NULL;
     p->length=0;
     return YES;
    } 

求队列中元素的个数


int SizeQueue(LinkQueue *p)
    {
     return p->length;
    } 

进队


Status EnQueue(LinkQueue *p,ElemType value)
    {
     Node *q;
     q=(Node *)malloc(sizeof(Node));
     if(!q)
     return NO;
     q->date=value;
     q->next=NULL;
     p->rear->next=q;
     p->rear=q;
     p->length++;
     return YES; 
    } 

出队


Status DeQueue(LinkQueue *p,ElemType *value)
    {
     Node *q;
     if(SizeQueue(p)==0)
     return NO;
     q=p->front->next;
     *value=q->date;
     p->front->next=q->next; 
     p->length--;
     return YES;
    } 

获取队头元素


Status GetHead(LinkQueue *p,ElemType *value)
    {
     Node *q;
     if(SizeQueue(p)==0)
     return NO;
     q=p->front->next;
     *value=q->date;
     return YES;
    } 

遍历


void PutQueue(LinkQueue *p)
    {
     Node *q=p->front;
     for(;q->next!=NULL;q=q->next)
     printf("%d  ",q->next->date);

综上,写成工程


status.h


#ifndef STATUS_H
#define STATUS_H
#define  YES  1
#define    NO   0
typedef int Status;
typedef int ElemType;
#endif

list.h


#ifndef LIST_H
#define LIST_H
#include <stdio.h>
#include "status.h"
typedef struct Node
   {
    ElemType date;
    struct Node *next;
   }Node;
typedef struct 
   {
    int length;
    Node *front;//队头指针
    Node *rear;//队尾指针 
   }LinkQueue;
Status InitQueue(LinkQueue *p);
Status EnQueue(LinkQueue *p,ElemType value);
void PutQueue(LinkQueue *p);
int SizeQueue(LinkQueue *p);
Status GetHead(LinkQueue *p,ElemType *value);
#endif

list.c


#include <stdio.h>
#include "list.h"
//初始化队列
Status InitQueue(LinkQueue *p)
    {
     p->front=p->rear=(Node *)malloc(sizeof(Node));
     if(p->front==NULL)
     return NO;
     p->front->next=NULL;
     p->length=0;
     return YES;
    } 
//进队(队尾)
Status EnQueue(LinkQueue *p,ElemType value)
    {
     Node *q;
     q=(Node *)malloc(sizeof(Node));
     if(!q)
     return NO;
     q->date=value;
     q->next=NULL;
     p->rear->next=q;
     p->rear=q;
     p->length++;
     return YES; 
    } 
//遍历
void PutQueue(LinkQueue *p)
    {
     Node *q=p->front;
     for(;q->next!=NULL;q=q->next)
     printf("%d  ",q->next->date);
    } 
//求元素的个数
int SizeQueue(LinkQueue *p)
    {
     return p->length;
    } 
//出队(队头)
Status DeQueue(LinkQueue *p,ElemType *value)
    {
     Node *q;
     if(SizeQueue(p)==0)
     return NO;
     q=p->front->next;
     *value=q->date;
     p->front->next=q->next; 
     p->length--;
     return YES;
    } 
//获取队头元素
Status GetHead(LinkQueue *p,ElemType *value)
    {
     Node *q;
     if(SizeQueue(p)==0)
     return NO;
     q=p->front->next;
     *value=q->date;
     return YES;
    } 

main.c


#include <stdio.h>
#include "list.h"
int main()
{
   LinkQueue que;
   LinkQueue *p=&que;
   ElemType value;
   int n,t,i;

   printf("================初始化队列=====================\n");
   t=InitQueue(p);
   if(t=YES)
   printf("初始化成功");
   else
   printf("初始化失败"); 


   printf("\n===========================进队=========================\n");
   printf("请输入进队元素的个数:");
   scanf("%d",&n); 
   printf("请依次输入%d个元素:",n);
   for(i=0;i<n;i++)
   {
    scanf("%d",&value);
    EnQueue(p,value);
   }
   PutQueue(p);


   printf("\n====================队的长度====================\n");
   printf("此时元素个数为:%d",SizeQueue(p));


   printf("\n===================出队=====================\n");
   DeQueue(p,&value);
   printf("出队的元素为:%d\n",value);
   PutQueue(p);



   printf("\n===================获取队头元素=====================\n");
   GetHead(p,&value);
   printf("%d\n",value);
}

结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱睡觉的小馨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值