25072班8.26日数据结构作业
实现链式队列和其相关操作
实现结果:(入队/输出/出队/销毁 效果展示)
要实现链式队列,要像实现普通的队列那样,设计一个队头和队尾,那么在链式队列里,队头队尾是在同一个结构体中的两个指针,在输入输出上类似于链表的尾插和头删详细请看代码(时间问题,省略了一部分判断)
#include "head.h"//创建队列节点
link_p create_queue()
{ //申请队列空间link_p queue=(link_p)malloc(sizeof(link));if(queue==NULL){printf("创建队列:申请空间失败\n");return NULL;}//初始化队列节点空间queue->front=NULL;queue->rear=NULL;return queue;
}//创建节点
node_p create_node(link_p q,int element)
{node_p new=(node_p)malloc(sizeof(node));if(new==NULL){printf("创建节点:申请空间失败\n");return NULL;}new->data=element;if(q->rear==NULL){//如果当前队列为空,写入为第一个节点q->front=new;q->rear=new;}else{//不为空的情况下,进行尾插q->rear->next=new;q->rear=new;}
}//判空
int empty(link_p q)
{if(q==NULL){printf("入参为空!\n");return -1;}return q->rear==NULL;
}
//输入
void join_queue(link_p q,int element)
{if(q==NULL){printf("入参为空\n");return ;}//调用节点创建node_p new=create_node(q,element);
}
//输出
void output(link_p q)
{if(q==NULL){printf("入参为空\n");return;}node_p p=q->front;while(p!=NULL){printf("%-4d",p->data);p=p->next;}putchar(10);
}
//出队
int exit_queue(link_p q)
{if(q==NULL){printf("入参为空\n");return -1;}//num存入要出队的数据方便返回int num=q->front->data;node_p del=q->front;q->front=q->front->next;free(del);del=NULL;return num;
}
//销毁
void destory(link_p q)
{if(q==NULL){printf("入参为空\n");return ;}node_p p=q->front;//循环清空每个节点while(p!=NULL){node_p temp=p;p=p->next;free(temp);temp=NULL;}free(q);q=NULL;
}