数据结构—链队列

#include "StdAfx.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

//定义链结点和队列类型
struct Node
{
	int data;//数据域
	struct Node *next;//指针域
};
typedef struct Node *PNode;
struct Queue
{
	PNode f;//f指向头结点
	PNode r;//r指向尾结点
};
typedef struct Queue *LinkQueue;



//创空
LinkQueue SetNullQueue()
{
	LinkQueue lqueue;
	lqueue=(LinkQueue)malloc(sizeof(struct Queue));//为队列lqueue分配队列结构体那么大空间
	if(lqueue!=NULL)//分配成功则使f,r指空
	{
		lqueue->f=NULL;
		lqueue->r=NULL;
	}
	else
		printf("alloc failure!");
	return lqueue;
}



//判空
int IsNullQueue(LinkQueue lqueue)
{
	return(lqueue->f==NULL);//若头指针指空则为空
}



//入队
void InQueue(LinkQueue lqueue,int x)//x入队
{
	PNode p;
	p=(PNode)malloc(sizeof(struct Node));//申请一个结点空间p用于存放输入数据,由p指向
	if(p!=NULL)
	{
		p->data=x;//放入数据至p
		p->next=NULL;
		if(IsNullQueue(lqueue))//若空队列则特殊处理
		{
			lqueue->f=p;
			lqueue->r=p;
		}
		else//不是空队列则按以下操作
		{
			lqueue->r->next=p;//队尾结点指针指向p
			lqueue->r=p;//使队尾指针指向p指向的结点空间p
		}
	}
	else
		printf("alloc failure!");
}



//出队
void OutQueue(LinkQueue lqueue)
{
	PNode p;
	if(IsNullQueue(lqueue))//判空
		printf("empty!");
	else
	{
		p=lqueue->f;//使p指向头结点
		lqueue->f=lqueue->f->next;//头指针指向头结点的next所指
		free(p);//释放头结点
	}
}



//取队头元素
int FrontQueue(LinkQueue lqueue)
{
	if(lqueue->f==NULL)//判空
	{
		printf("empty!");
		return 0;
	}
	else
		return (lqueue->f->data);//返回头结点数据域
}




//打印链队列
void printQueue(LinkQueue lqueue)
{
	PNode p;
	if (IsNullQueue(lqueue))//判空
		printf("\n The Linklist is NULL !\n");
	p = lqueue->f;
	while (p != NULL)
	{
		printf("%d", p->data);
		p = p->next;
	}
	printf("\n");
}

//主函数
int main()
{
	LinkQueue queue1=SetNullQueue();
	InQueue(queue1,1);
	InQueue(queue1,2);
	printQueue(queue1);
	OutQueue(queue1);
	printQueue(queue1);
	system("pause");
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值