数据结构day6链式队列

主程序

#include "fun.h"

int main(int argc, const char *argv[])
{
    que_p Q=create();
    enqueue(Q,10);
    enqueue(Q,20);
    enqueue(Q,30);
    enqueue(Q,40);
    enqueue(Q,50);
    show_que(Q);
    dequeue(Q);
    show_que(Q);
    printf("len:%d\n",que_len(Q));
    free_que(Q);
    Q=NULL;
    show_que(Q);

    return 0;
}

源程序

#include "fun.h"

que_p create()
{
   que_p Q=(que_p)malloc(sizeof(que));
   if(Q==NULL)
   {
       printf("error\n");
       return NULL;
   }
   node_p H=(node_p)malloc(sizeof(node));
   if(H==NULL)
   {
       printf("error\n");
       free(Q);
       Q=NULL;
       return NULL;
   }
   H->next=NULL;
   H->len=0;
   Q->head=H;
   Q->tail=H;
   return Q;
}

int empty_que(que_p Q)
{
    if(Q==NULL)
    {
        printf("error\n");
        return -1;
    }
    return Q->head==Q->tail;
}
node_p create_new(typdata data)
{
    node_p new =(node_p)malloc(sizeof(node));
    if(new==NULL)
    {
        printf("error");
    }
    new->data=data;
    new->next=NULL;
    return new;
}
void  enqueue(que_p Q,typdata data)
{
    if(Q==NULL)
    {
        printf("erro\n");
    }
    node_p new=create_new(data);
    Q->tail->next=new;
    Q->tail=new;
    Q->head->len++;
}

void show_que(que_p  Q)
{
    if(Q==NULL)
    {
        printf("error\n");
        return;
    }
    if(empty_que(Q))
    {
       printf("Queue empty\n");
       return;
    }
    node_p H=Q->head;
    while(H->next!=NULL)
    {
        H=H->next;
        printf("%-4d",H->data);
    }
    putchar(10);
}

void dequeue(que_p Q)
{
    if(Q==NULL)
    {
        printf("error\n");
        return;
    }
    if(empty_que(Q))
    {
        printf("Queue empty\n");
        return;
    }
    printf("dequeue:%d\n",Q->head->next->data);
    node_p p=Q->head->next;
    Q->head->next=Q->head->next->next;
    free(p);
    p=NULL;
    Q->head->len--;
}
int que_len(que_p Q)
{
    if(Q==NULL)
    {
        printf("error\n");
        return-1;
    }
    return Q->head->len;
}
void free_que(que_p Q)
{
    if(Q==NULL)
    {
        printf("error\n");
        return;
    }
    
    while(Q->head->next!=NULL)
    {
        dequeue(Q);
    }
    free(Q->head);
    Q->head=NULL;
    free(Q);
    Q=NULL;
}

  头文件

#ifndef __FUN_H_
#define __FUN_H_
#include <myhead.h>

typedef int typdata;

typedef struct node
{
    union
    {
        int len;
        typdata data;
    };
    struct node *next;

}node,*node_p;

typedef struct queue
{
    node_p  head;
    node_p tail;
}que,*que_p;

que_p create();
int empty_que(que_p Q);
node_p create_new(typdata data);
void  enqueue(que_p Q,typdata data);
void show_que(que_p  Q);
void dequeue(que_p Q);
int que_len(que_p Q);
void free_que(que_p Q);
#endif

相关推荐

  1. 数据结构——队列存储实现

    2024-07-11 10:16:03       53 阅读
  2. 数据结构-----顺序队列队列的应用

    2024-07-11 10:16:03       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 10:16:03       7 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 10:16:03       8 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 10:16:03       7 阅读
  4. Python语言-面向对象

    2024-07-11 10:16:03       10 阅读

热门阅读

  1. 0124__Linux和Unix的Access.conf安全配置

    2024-07-11 10:16:03       6 阅读
  2. Android11 应用启动流程

    2024-07-11 10:16:03       11 阅读
  3. CentOS-6的iso下载地址镜像yum源

    2024-07-11 10:16:03       9 阅读
  4. 什么是CLR

    2024-07-11 10:16:03       9 阅读
  5. 获取线程id

    2024-07-11 10:16:03       9 阅读
  6. 小抄 20240709

    2024-07-11 10:16:03       7 阅读
  7. 24/07/10数据结构(5.1213)链表OJ

    2024-07-11 10:16:03       9 阅读
  8. VUE学习列表

    2024-07-11 10:16:03       10 阅读