队列的学习

队列的实现和栈类似,队列也是一种特殊的线性表,先进先出

先贴上顺序存储的队列

#ifndef  __MY_SEQLIST_H__ 
#define __MY_SEQLIST_H__

// #define  ERR_BASE  0
// #define ERR_PARAM   ERR_BASE -1;

typedef void SeqList;
typedef void SeqListNode;

SeqList* SeqList_Create(int capacity);

void SeqList_Destroy(SeqList* list);

void SeqList_Clear(SeqList* list);

int SeqList_Length(SeqList* list);

int SeqList_Capacity(SeqList* list);

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);

SeqListNode* SeqList_Get(SeqList* list, int pos);

SeqListNode* SeqList_Delete(SeqList* list, int pos);


#endif  //__MY_SEQLIST_H__


#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include "seqlist.h"

typedef struct _tag_SeqList
{
	int capacity;
	int length;
	unsigned int *node ; // unsigned int nodeAarry[100]
}TSeqList;

//typdef的意思 把void 重新命名成SeqList
/*
void * SeqList_Create2(int capacity)
{
	TSeqList *ret = NULL;
	ret = (TSeqList *)malloc(sizeof(TSeqList));
	if (ret == NULL)
	{
		return NULL;
	}
	ret->capacity = capacity;
	ret->node = (unsigned int *)malloc(sizeof(unsigned int ) * capacity);
	if (ret->node == NULL)
	{
		return NULL;
	}
	ret->length = 0;
	return ret;
}
*/

void * SeqList_Create(int capacity)
{
	TSeqList *ret = NULL;

	if (capacity <= 0)
	{
		return NULL;
	}
	ret = (TSeqList *)malloc(sizeof(TSeqList) + sizeof(unsigned int ) * capacity );
	if (ret == NULL)
	{
		return NULL;
	}
	ret->capacity = capacity;
	ret->node = (unsigned int *)(ret + 1);
	
	ret->length = 0;
	return ret;
}

void SeqList_Destroy(SeqList* list)
{
	if (list == NULL)
	{
		return ;
	}
	free(list);
	return ;
}

void SeqList_Clear(SeqList* list)
{
	TSeqList *tlist = NULL;
	if (list == NULL)
	{
		return ;
	}
	tlist = (TSeqList *)list;

	tlist->length = 0;
	return ;
}

int SeqList_Length(SeqList* list)
{
	TSeqList *tlist = list;
	if (list == NULL)
	{
		return -1;
	}
	return tlist->length;
}

int SeqList_Capacity(SeqList* list)
{
	TSeqList *tlist = list;
	if (list == NULL)
	{
		return -1;
	}
	return tlist->capacity;
}

int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
	int i = 0;
	TSeqList *tlist = list;

	if (list == NULL || node== NULL )
	{
		return -1;
	}
	if (pos<0 || pos>=tlist->capacity )
	{
		return -2;
	}
	//判断是否已经man
	if (tlist->length >= tlist->capacity)
	{
		return -3;
	}
	//容错
	if (pos > tlist->length)
	{
		pos = tlist->length;
	}
	
	//插入算法 有两步
	//从插入的位置 后移元素 
	//注意length能表示出现在数组的最后元素位置
	//最后元素的下标为 tlist->node[length-1];
	for (i=tlist->length; i>pos; i--)
	{
		tlist->node[i] = tlist->node[i-1];
	}
	//在pos位置插入元素
	tlist->node[pos] = (unsigned int )node; //20140514这个地方不能加 (unsigned int *)
	//如果你加*,说明你对	unsigned int *node ; // unsigned int nodeAarry[100]还没有理解 
	tlist->length ++;

// 	{
// 		char *p = (char *)malloc(1000);
// 		p[1] = (char *)'a'; //err
// 		p[2] = (char)'b'; //0k
// 	}

	return 0;
}

//pos是数组的下标 我们约定从0开始
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
	int i = 0;
	TSeqList *tlist = list;
	if (list== NULL || pos<0 || pos>=tlist->length)  //node wangbaoming modify
	//if (list== NULL || pos<0 || pos>tlist->length)
	{
		return NULL;
	}
	return (SeqListNode*)tlist->node[pos];
}

SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
	int i = 0;
	TSeqList *tlist = list;
	SeqListNode* ret = NULL;
	if (list == NULL || pos<0 || pos>=tlist->length) //node wangbaoming modify
	{
		return NULL;
	}
	//缓存要删除的结点
	ret = (SeqListNode*)tlist->node[pos];
	//对链表进行移动
	for (i=pos+1; i<tlist->length; i++)
	{
		tlist->node[i-1] = tlist->node[i];
 	}
	tlist->length --;
	return ret;
}

#ifndef _MY_SEQQUEUE_H_
#define _MY_SEQQUEUE_H_

typedef void SeqQueue;

SeqQueue* SeqQueue_Create(int capacity);

void SeqQueue_Destroy(SeqQueue* queue);

void SeqQueue_Clear(SeqQueue* queue);

int SeqQueue_Append(SeqQueue* queue, void* item);

void* SeqQueue_Retrieve(SeqQueue* queue);

void* SeqQueue_Header(SeqQueue* queue);

int SeqQueue_Length(SeqQueue* queue);

int SeqQueue_Capacity(SeqQueue* queue);

#endif //_MY_SEQQUEUE_H_

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "seqqueue.h"
#include "seqlist.h"

SeqQueue* SeqQueue_Create(int capacity)
{
	return	SeqList_Create(capacity);
}

void SeqQueue_Destroy(SeqQueue* queue)
{
	 SeqList_Destroy(queue);
}

void SeqQueue_Clear(SeqQueue* queue)
{
	 SeqList_Clear(queue);
}

int SeqQueue_Append(SeqQueue* queue, void* item)
{
	return SeqList_Insert(queue, item, SeqList_Length(queue));
}

void* SeqQueue_Retrieve(SeqQueue* queue)
{
	return SeqList_Delete(queue, 0);
}

void* SeqQueue_Header(SeqQueue* queue)
{
	return SeqList_Get(queue, 0);
}

int SeqQueue_Length(SeqQueue* queue)
{
	return SeqList_Length(queue);
}

int SeqQueue_Capacity(SeqQueue* queue)
{
	return SeqList_Capacity(queue);
}

下面贴出链式存储的代码

#ifndef _MYLINKLIST_H_
#define _MYLINKLIST_H_

typedef void LinkList;
/*
typedef struct _tag_LinkListNode LinkListNode;
struct _tag_LinkListNode
{
	LinkListNode* next;
};
*/

typedef struct _tag_LinkListNode
{
	struct _tag_LinkListNode* next;
}LinkListNode;

LinkList* LinkList_Create();

void LinkList_Destroy(LinkList* list);

void LinkList_Clear(LinkList* list);

int LinkList_Length(LinkList* list);

int LinkList_Insert(LinkList* list, LinkListNode* node, int pos);

LinkListNode* LinkList_Get(LinkList* list, int pos);

LinkListNode* LinkList_Delete(LinkList* list, int pos);

#endif

#include "stdlib.h"
#include "stdio.h"
#include "string.h"

#include "linklist.h"

typedef struct _tag_LinkList
{
	LinkListNode header;
	int length;
}TLinkList;

LinkList* LinkList_Create()
{
	TLinkList *tList = (TLinkList *)malloc(sizeof(TLinkList));
	if (tList == NULL)
	{
		return NULL;
	}
	tList->header.next = NULL;
	tList->length = 0;
	return tList;
}

void LinkList_Destroy(LinkList* list)
{
	if (list != NULL)
	{
		free(list);
	}
	return ;
}

void LinkList_Clear(LinkList* list)
{
	TLinkList *tList = list;
	if (tList == NULL)
	{
		return ;
	}
	tList->length = 0;
	tList->header.next = NULL;
	return ;
}

int LinkList_Length(LinkList* list)
{
	TLinkList *tList = list;
	if (tList == NULL)
	{
		return -1;
	}
	return tList->length;
}

int LinkList_Insert(LinkList* list, LinkListNode* node, int pos)
{
	int i = 0;
	TLinkList  *tList = (TLinkList *)list;
	LinkListNode  *current = NULL;

	if (tList == NULL || node == NULL || pos<0)
	{
		return -1;
	}
	current = &tList->header;
	//current = (LinkListNode *)list;

	for (i=0; (i<pos)&¤t->next!=NULL; i++ )
	{
		current = current->next;
	}
	//新节点链接后续链表
	node->next = current->next;
	//前面链表链接node
	current->next = node;
	tList->length ++;

	return 0;
}

LinkListNode* LinkList_Get(LinkList* list, int pos)
{
	int i = 0;
	TLinkList  *tList = (TLinkList *)list;
	LinkListNode  *current = NULL;
	LinkListNode  *ret = NULL;

	if (list==NULL || pos<0 || pos>=tList->length)
	{
		return NULL;
	}

	current = &tList->header;
	for (i=0; i<pos; i++)
	{
		current = current->next;
	}
	ret = current->next;
	
	return ret;
}

LinkListNode* LinkList_Delete(LinkList* list, int pos)
{
	int i = 0;
	TLinkList  *tList = (TLinkList *)list;
	LinkListNode  *current = NULL;
	LinkListNode  *ret = NULL;

	if (list==NULL || pos<0 || pos>=tList->length)
	{
		return NULL;
	}
	//没有初始化环境
	current = &tList->header;
	for (i=0; i<pos; i++)
	{
		current = current->next;
	}
	ret = current->next;

	current->next = ret->next;
	tList->length --;
	return ret;
}
#ifndef _MY_LINKQUEUE_H_
#define _MY_LINKQUEUE_H_

typedef void LinkQueue;

LinkQueue* LinkQueue_Create();

void LinkQueue_Destroy(LinkQueue* queue);

void LinkQueue_Clear(LinkQueue* queue);

int LinkQueue_Append(LinkQueue* queue, void* item);

void* LinkQueue_Retrieve(LinkQueue* queue);

void* LinkQueue_Header(LinkQueue* queue);

int LinkQueue_Length(LinkQueue* queue);

#endif //_MY_LINKQUEUE_H_

#include "stdlib.h"
#include "string.h"
#include "stdio.h"
#include "linkqueue.h"
#include "linklist.h"

typedef struct _tag_LinkQueueNode
{
	LinkListNode node;
	void *item;
}TLinkQueueNode;
LinkQueue* LinkQueue_Create()
{
	return LinkList_Create();
}

void LinkQueue_Destroy(LinkQueue* queue)
{
	LinkQueue_Clear(queue); //注意这个地方不要调用错误了
	LinkList_Destroy(queue);
	return ;
}

void LinkQueue_Clear(LinkQueue* queue)
{
	while (LinkQueue_Length(queue) > 0)
	{
		LinkQueue_Retrieve(queue);
	}
	return ;
}

int LinkQueue_Append(LinkQueue* queue, void* item)
{
	int ret = 0;
	TLinkQueueNode *node = NULL;
	if (queue==NULL || item == NULL)
	{
		return -2;
	}
	//把形参item转成linklist的业务节点
	node = (TLinkQueueNode *)malloc(sizeof(TLinkQueueNode));
	if (node == NULL)
	{
		return -1;
	}
	node->item = item;
	//尾叉法
	ret = LinkList_Insert(queue, (LinkListNode*)node, LinkList_Length(queue));
	if (ret != 0)
	{
		free(node);
	}
	return 0;
}

void* LinkQueue_Retrieve(LinkQueue* queue)
{
	void *myItem = NULL;
	TLinkQueueNode *node = NULL;
	if (queue == NULL)
	{
		return NULL;
	}
	node = (TLinkQueueNode *)LinkList_Delete(queue, 0);
	if (node == NULL)
	{
		return NULL;
	}
	myItem = node->item;
	if (node != NULL)
	{
		free(node); //注意这个地方别忘记了释放内存,因为insert的时候分配了内存
	}
	return myItem;
}

void* LinkQueue_Header(LinkQueue* queue)
{
	void *myItem = NULL;
	TLinkQueueNode *node = NULL;
	if (queue == NULL)
	{
		return NULL;
	}
	node = (TLinkQueueNode *)LinkList_Get(queue, 0);
	if (node == NULL)
	{
		return NULL;
	}
	myItem = node->item;
	
	return myItem;
}

int LinkQueue_Length(LinkQueue* queue)
{
	return LinkList_Length(queue);
}


测试代码

#include "stdlib.h"
#include "string.h"
#include "stdio.h"
#include "linkqueue.h"

void main()
{
	int i = 0, a[10], tmp = 0;
	LinkQueue *queue = NULL;
	
	queue =  LinkQueue_Create();

	for (i=0; i<5; i++)
	{
		a[i] = i+1;
		LinkQueue_Append(queue, (void *)&a[i]);
	}
	tmp = *((int *)LinkQueue_Header(queue));
	printf("头元素:%d\n", tmp);
	printf("队列长度:%d\n", LinkQueue_Length(queue));

	while (LinkQueue_Length(queue) > 0)
	{
		tmp = *((int *)LinkQueue_Retrieve(queue));
		printf("tmp:%d \n", tmp);
	}

	LinkQueue_Destroy(queue);
	system("pause");
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值