1.6 链队

链对的节点,指针,顺序

链队的定义

节点定义

指针定义

//节点定义

typedef struct queuenode

{

    int data;

    queuenode* next;

}queuenode,*queueptr;

// 链(指针)定义

typedef struct linkqueue

{

    queueptr front,rear;

}linkqueue;

链队的push

声明

主要行为

行为代码

status queuepush (linkqueue* pq,int e)

创建节点

node的data和next赋值

加入链表,rear指向调整

    queueptr p=(queueptr)malloc(sizeof(queuenode));

    

    p->data=e;

    p->next=NULL;

    

    pq->rear->next=p;

    pq->rear=p;

链队的pop

声明

主要行为

行为代码

补充行为

总体行为代码

 

status queuepop (linkqueue * pq,int*pe)

创建p,并使p指向node1

p的data给pe传出来

头node指向node2

销毁node1

    queueptr p=pq->front->next;

    

    *pe=p->data;

    

    pq->front->next=p->next;

    

    free (p);

空队报错

假如rear指向的node就是被销毁的node,rear指向头node

    if(pq->front==pq->rear)

        return ERROR;

    queueptr p=pq->front->next;

    

    *pe=p->data;

    

    pq->front->next=p->next;

    

    if(p->next==NULL)

        pq->rear=pq->front;

    

    free (p);

代码汇总:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include <string.h>
//-----------------------------
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef int status;
#define MAXSIZE 20
//-----------------------------

//节点定义
typedef struct queuenode
{
	int data;
	queuenode* next;
}queuenode,*queueptr;

// 链(指针)定义
typedef struct linkqueue
{
	queueptr front,rear;
}linkqueue;

status queuepush (linkqueue* pq,int e)
{
	queueptr p=(queueptr)malloc(sizeof(queuenode));
	
	p->data=e;
	p->next=NULL;
	
	pq->rear->next=p;
	pq->rear=p;
}

status queuepop (linkqueue * pq,int*pe)
{
	if(pq->front==pq->rear)
		return ERROR;
	queueptr p=pq->front->next;
	
	*pe=p->data;
	
	pq->front->next=p->next;
	
	if(p->next==NULL)
		pq->rear=pq->front;
	
	free (p);
}


void test()
{
	
}

int main()
{
	test();
	
	system("pause");
	return 0;
}

  • 15
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值