《数据结构c语言版》之队列的链式存储

1.statu.h
#ifndef STATUS_H
#define STATUS_H
//函数结果状态代码
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR  0
#define INFEASIBLE -1
#define OVERFLOW -2
//Status是函数的类型,其值是函数结果状态代码
typedef int Status;
 typedef char SElemType;
typedef int ElemType;
typedef int QElemType;
#endif

2.DuiLieLian.h

   #ifndef DUILIELIAN_H
    #define DUILIELIAN_H
    #include "status.h"
    typedef struct QNode{
    	QElemType date;
    	struct QNode *next;
    }QNode,*QueuePtr;
    typedef struct{
    	QueuePtr front;  //队头指针
    	QueuePtr rear; 
    	
    }LinkQueue;
    Status IniQueue(LinkQueue *Q);
    Status EnQueue(LinkQueue *Q,QElemType e);
    Status DeQueue(LinkQueue *Q,QElemType *e);
    Status GetHead(LinkQueue Q,QElemType *e);
    Status QueueEmpty(LinkQueue Q);
    Status DestroyQueue(LinkQueue *Q);
    
    //Status QueueTraverse(LinkQueue Q,visit());
    
    int QueueLength(LinkQueue Q);
    Status ClearQueue(LinkQueue *Q);
    void Output(LinkQueue Q);
    
    #endif
***3DuiLieLIan.c***

#include "DuiLieLian.h"
#include <stdio.h>
Status IniQueue(LinkQueue *Q){
	Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
	if( !Q->front)exit(OVERFLOW);
	Q->front->next=NULL;
	return OK; 
}
Status EnQueue(LinkQueue *Q,QElemType e){
	QueuePtr p;
	p=(QueuePtr)malloc(sizeof(QNode));
	if(!p) exit(OVERFLOW);
  
	p->date=e;
	p->next=NULL;
	Q->rear->next=p;
	Q->rear=p;
	return OK;
}
Status DeQueue(LinkQueue *Q,QElemType *e){
	QueuePtr p;//QNode *p;
	if(Q->front==Q->rear)  return ERROR;
	p=Q->front->next;
	*e=p->date;
	Q->front->next=p->next;
	if(Q->rear==p) Q->rear=Q->front;
	free(p);
	return OK; 
}
Status GetHead(LinkQueue Q,QElemType *e){
	if(QueueEmpty(Q)) exit(ERROR);
	*e=Q.front->next->date;
	return *e;
}
Status QueueEmpty(LinkQueue Q){
	if(Q.front==Q.rear)return TRUE;
	return FALSE;
}
Status DestroyQueue(LinkQueue *Q){
     while(Q->front){
     	Q->rear=Q->front->next;
     	free(Q->front);
     	Q->front=Q->rear;
     }	
     return OK;
}
void Output(LinkQueue Q){
	QueuePtr p;
	p=Q.front->next;
	while(p!=Q.rear){
		printf("%d ",p->date);
		p=p->next;
	}
	printf("\n");
}

4.DuiLieLian_main.c

  #include "DuiLieLian.h"
    #include <stdio.h>
    void main(){
    	LinkQueue Q;
      
    	QElemType m,*p,*e;
    	p=&m;
    	IniQueue(&Q);
    	EnQueue(&Q,2);
    	EnQueue(&Q,3);
    	EnQueue(&Q,5);                                                                                                                    
    	EnQueue(&Q,7);
    
    	printf("插入数据后的对列为:");
    	Output(Q);
    	DeQueue(&Q,p);
    	printf("出栈的元素为%d\n",*p);
    	printf("删除一个元素后的对列为:");
    	Output(Q);
    	printf("队列头元素为:%d\n",GetHead(Q,&e));
    	printf("队列是否为空:%d\n",QueueEmpty(Q));
    	 
    	return 0; 
    
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值