2月22日作业l链式队列

main.c部分

#include <stdio.h>
#include "./fun.h"
int main(int argc, const char *argv[])
{
    linkPos* pos=create_linkqueue();
    insert_linqueue(pos,3);
    insert_linqueue(pos,89);
    insert_linqueue(pos,34);
    show_linqueue(pos);
    out_linqueue(pos);
    show_linqueue(pos);
    return 0;
}

fun.h部分

#ifndef __FUN_H__
#define __FUN_H__
typedef int datatype;
union msg{
    datatype data;
    int len;
};
typedef struct node{
    union msg text;
    struct node* next;
}linkQueue;

typedef struct{
    linkQueue* front;
    linkQueue* rear;
}linkPos;
linkPos* create_linkqueue();
void insert_linqueue(linkPos* pos,datatype num);
void show_linqueue(linkPos* pos);
void out_linqueue(linkPos* pos);
#endif

功能部分

#include <stdio.h>
#include <stdlib.h>
#include "./fun.h"
linkPos* create_linkqueue()
{
    linkPos* pos=(linkPos*)malloc(sizeof(linkPos));

    pos->front=(linkQueue*)malloc(sizeof(linkQueue));
    if(NULL == pos->front)
    {
        printf("创监控的队列失败\n");
        return NULL;
    }

    pos->front->text.len=0;
    pos->front->next=NULL;
    pos->rear=pos->front;

    return pos;
}
int isEmpty(linkPos* pos)
{
    return pos->rear==NULL ?1:0;

}
void insert_linqueue(linkPos* pos,datatype num)
{
    linkQueue* temp=(linkQueue*)malloc(sizeof(linkQueue));
    if(NULL == temp)
    {
        printf("创建的结点失败,入队失败\n");
        return;
    }
    temp->next=NULL;
    temp->text.data=num;

    temp->next=pos->rear->next;
    pos->rear->next=temp;

    pos->rear=pos->rear->next;

    pos->front->text.len++;
    return;
}

void show_linqueue(linkPos* pos)
{
while(pos->front->next!=NULL)
    {
            printf("%d ",pos->front->next->text.data);
            pos->front=pos->front->next;
    }
    printf("\n");

}


void out_linqueue(linkPos* pos)
{
    if(isEmpty(pos)==1)
    {
        printf("队列为空出队失败\n");
        }
        linkQueue* temp=pos->front->next;
    pos->front->next=temp->next;
    free(temp);
    temp=NULL;
    return ;
}

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值