数据结构:链式队列尝试;0826
链式队列
尝试了 没写出来。代码存档,如下
queuelink.c
#include"queue.h"
//1.申请 链式队列
queuelink_p creatlink()
{//申请 链表 头尾指针 结构体空间queuelink_p H=(queuelink_p)malloc(sizeof(queuelink));if(H==NULL)//申请空间失败{printf("申请空间失败\n");return NULL;}H->front=NULL;H->rear=NULL;return H;
}
//1.1申请 节点
queue_p creat_node(int V)
{//申请 链表 头尾指针 结构体空间queue_p N=(queue_p)malloc(sizeof(queue));if(N==NULL)//申请空间失败{printf("申请空间失败\n");return NULL;}N->data=V;N->next==NULL;return N;
}
//2.判空
int empty(queuelink_p H)
{ if(H==NULL)//入参为空{ printf("入参为空\n");return -1;}return H->front==NULL&&H->rear==NULL;
}
//4.入队
void push_rear(queuelink_p H,int V)
{ if(H==NULL)//入参为空{printf("入参为空\n");return ;}//申请节点queue_p N=creat_queue(V);//节点链接 入队 队尾if(H->front=NULL){H->front=N;}if(H->rear!=NULL){H->rear->next=N;}H->next=N;
}
//5.出队
int pop_que(queue_p H)
{ if(H==NULL)//入参为空{printf("入参为空\n");return ;}//判断队列为空if(empty(H)){printf("队列为空\n");return;}//出队 队头出queue_p D=H->front;//队头 指针 指向下一个H->front=H->front->next;//保留 出队的值int ret=D->data;//释放空间free(D);return ret;
}
//6.输出
void show_que(queuelink_p H)
{if(H==NULL)//入参为空{printf("入参为空\n");return ;}//判断队列为空if(empty(H)){printf("队列为空\n");return;}//S指针 输出值queue_p S=H->front;while(S!=NULL){printf("%d ",S->data);S=S->next;}
}
//7.求队列元素个数
int count_que(queuelink_p H);
//8.释放循环队列
void free_que(queuelink_p *H);
main.c
#include"queue.h"
int main()
{//1.申请队列queuelink_p H =creatlink();//4.入队for(int i=9;i>4;i--){push_rear(H,i);}//5.出队printf("出队的值为%d\n",pop_que(H));//6.输出show_que(H);return 0;
}
queue.h
#ifndef __QUEUE_H_
#define __QUEUE_H_ 1#include<stdio.h>
#include<stdlib.h>//链栈的节点 结构体
typedef struct node
{int data;struct node *next;
}queue,*queue_p;//链式队列 头 结构体
typedef struct queuelink
{struct node *front;struct node *rear;
}queuelink,*queuelink_p;//1.申请队列
queuelink_p creatlink();
//1.1申请节点
queue_p creat_node(int);
//2.判空
int empty(queuelink_p);
//4.入队
void push_rear(queuelink_p,int);
//5.出队
int pop_que(queuelink_p);
//6.输出
void show_que(queuelink_p);
//7.求队列元素个数
int count_que(queuelink_p);
//8.释放循环队列
void free_que(queuelink_p *);#endif
兄弟,是报错
牛客刷题