2018.08.10

链式队列:

链式队列与顺序队列有相似的地方,也有不同的地方。

链式队列的初始化部分:

struct node                                    //这是结点的信息 
{
    int data;
    struct node *next;
};
typedef struct node Node;
struct queue                                 //这个队列的结构体的信息,只需要定义两个结点,队头结点和队尾结点
{
    Node *front;
    Node *rear;
};

头文件部分:

#ifndef _LIANSHITUI_H
#define _LIANSHITUI_H

#define SUCCESS 10000
#define FAILURE 10001
#define TRUE    10002
#define FALSE   10003

struct node 
{
	int data;
	struct node *next;
};
typedef struct node Node;
struct queue
{
	Node *front;
	Node *rear;
};
typedef struct queue Q;
int queueinit(Q **p);
int insert(Q *p, int e);
int out(Q *p);
int getfirst(Q *p);
int empty(Q *p);
int clear(Q *p);
int destory(Q **p);
#endif

初始化函数 :

int queueinit(Q **p)
{
    (*p) = (Q *)malloc(sizeof(Q));
    if((*p) == NULL)                              //入参判断
    {
        return FAILURE;
    }
        Node *q = (Node *)malloc(sizeof(Node) * 1); //给头结点分配内存
    if(q == NULL)
    {
        return FAILURE;
    }
    (*p)->front = (*p)->rear = q;
    q->next = NULL;
    return SUCCESS;
}

接下来就可以实现插入,清空等操作:

#include "lianshitui.h"
#include <stdlib.h>
#include <stdio.h>

int queueinit(Q **p)
{
	(*p) = (Q *)malloc(sizeof(Q));
	if((*p) == NULL)
	{
		return FAILURE;
	}
		Node *q = (Node *)malloc(sizeof(Node) * 1);
	if(q == NULL)
	{
		return FAILURE;
	}
	(*p)->front = (*p)->rear = q;
	q->next = NULL;
	return SUCCESS;
}
int insert(Q *p, int e)                        //插入操作
{
	if(p == NULL)
	{
		return FAILURE;
	}
	Node *q = (Node *)malloc(sizeof(Node));   //类似单链表的尾插法
	q->data = e;
	q->next = NULL;                            //队列的插入需要移动的是队尾指针
	p->rear->next = q;
	p->rear = q;
	return SUCCESS;
}
int out(Q *p)
{
	if(p ==  NULL || p->rear == p->front)
	{
		return FAILURE;
	}
	Node *q = p->front->next;
	int e;
	e = q->data;
	p->front->next = q->next;
	if(!q->next)                                //在出队的时候需要判断是不是出去了最后一个
	{                                           //结点,如果出去了最后一个结点,此时
		p->rear = p->front;                     //p->rears是野指针,访问了错误内存会报
	}                                           //段错误
	free(q);                                    //需要将p->rear = p->ront
	q = NULL;
	return e;
}

int getfirst(Q *p)                      //队头的元素一定是队头结点的下一个结点
{
	if(p == NULL || p->rear == p->front)
	{
		return FAILURE;
	}
	return p->front->next->data;
}

int empty(Q *p)
{
	if(p == NULL)
	{
		return FAILURE;
	}
	return (p->front == p->rear)? TRUE:FALSE;
}

int clear(Q *p)
{
	if(p == NULL)
	{
		return FAILURE;
	}
	Node *q = p->front->next;
	while(q)
	{
		p->front->next = q->next;
		free(q);
		q = p->front->next;
	}
	p->rear = p->front;
	return SUCCESS;
}
int destory(Q **p)
{
	if((*p) == NULL || p == NULL)
	{
		return FAILURE;
	}
	free((*p)->front);
	free((*p));
	return SUCCESS;
}

主函数:

#include "lianshitui.h"
#include <stdio.h>
#include <stdlib.h>

int main()
{
	Q *queue;
	int ret;

	ret = queueinit(&queue);
	if(ret == SUCCESS)
	{
		printf("init success!\n");
	}
	else
	{
		printf("init failure!\n");
	}
	
	int i;
	for(i = 0; i < 5; i++)
	{
		ret = insert(queue,i);
		if(ret == FAILURE)
		{
			printf("insert failure!\n");
		}
		else
		{
			printf("insert %d success!\n",i);
		}
	}
	for(i = 0; i < 3; i++)
	{
		ret = out(queue);
		if(ret == FAILURE)
		{
			printf("out falure!\n");
		}
		else
		{
			printf("out %d success!\n",ret);
		}
	}
	ret = getfirst(queue);
	if(ret == FAILURE)
	{
		printf("getfront failure!\n");
	}
	else
	{
		printf("get front%d: \n",ret);
	}

	ret = empty(queue);
	if(ret == FALSE)
	{
		printf("queue is not empty!\n");
	}
	else
	{
		printf("queue is empty!\n");
	}
	ret = clear(queue);
	if(ret == SUCCESS)
	{
		printf("clear success!\n");
	}
	else
	{
		printf("clear failure!\n");
	}
	
	ret = destory(&queue);
	if(ret == SUCCESS)
	{
		printf("Destory Success!\n");
	}
	else
	{
		printf("Destory Failure!\n");
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值