测试框架
#include<string.h>
#include<iostream>
#include"SeqQueue.h"
using namespace std;
void main()
{
/*SeqQueue* Queue_Creat(int capacity);
void Queue_Destory(SeqQueue* queue);
void Queue_Clear(SeqQueue* queue);
int Queue_Append(SeqQueue* queue, void* item);
void Queue_Retrieve(SeqQueue* queue);
void Queue_Header(SeqQueue* queue);
int Queue_Lenght(SeqQueue* queue);
int Queue_Capacity(SeqQueue* queue);*/
int i = 0,ret=0;
int a[5];
SeqQueue* queue = NULL;
queue = Queue_Creat(10);
if (queue==NULL)
{
return;
}
for ( i = 0; i < 5; i++)
{
a[i] = i + 1;
ret = Queue_Append(queue,&a[i]);
}
printf("len: %d\n",Queue_Lenght(queue));
printf("head: %d\n", *((int* )Queue_Header(queue)));
printf("Queue_Capacity: %d\n", Queue_Capacity(queue));
while (Queue_Lenght(queue)>0)
{
int tmp = Queue_Retrieve(queue);
printf("tmp:%d\n",tmp);
}
}
头文件:
SeqQueue.h
#pragma once
#ifndef _MY_SEQQUEUE_H_
#define _MY_SEQQUEUE_H_
#endif // !_MY_SEQQUEUE_H_
typedef void SeqQueue;
SeqQueue* Queue_Creat(int capacity);
void* Queue_Destory(SeqQueue* queue);
void* Queue_Clear(SeqQueue* queue);
int Queue_Append(SeqQueue* queue,void* item);
int Queue_Retrieve(SeqQueue* queue);
void* Queue_Header(SeqQueue* queue);
int Queue_Lenght(SeqQueue* queue);
int Queue_Capacity(SeqQueue* queue);
SeqList.h
#pragma once
#ifdef _MYSEQLIST_H_
#define _MYSEQLIST_H_
#endif // _MYSEQLIST_H_
//线性链表的顺序存储
//void指针表示未知类型的指针,可以将任意类型的指针直接赋值给void指针,好比是C#、Java中的object引用,可以把任意 类型的对象赋值给它。但把void类型赋值给特定类型的指针时,就需要进行强制转换,因为C++为类型语言,尽可能保证类型安全,如果使用了强制类型转 换,编译器就认为程序员知道他(她)在干什么,程序也应该负起这样做的责任。
typedef void Seqlist;
typedef void SeqListNode;
Seqlist * List_Create1(int capacity);
void* List_Destory1(Seqlist* list);
void* List_Clear1(Seqlist* list);
int List_Length1(Seqlist* list);
int List_Capacity1(Seqlist* list);
SeqListNode* List_Get1(Seqlist* list, int pos);
int List_Inster1(Seqlist* list, SeqListNode* node, int pos);
int List_Delete1(Seqlist* list,int pos);
实现:
SeqList.cpp
#include"SeqList.h"
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct TSeqList
{
int length;
int capacity;
unsigned int **node;
};
/*创建链表并为链表分配空间*/
Seqlist* List_Create1(int capacity)
{
TSeqList* tmp = NULL;
tmp = (TSeqList*)malloc(sizeof(TSeqList));
if (tmp==NULL)
{
return NULL;
}
tmp->node =(unsigned int**)malloc(sizeof(unsigned int*)*capacity);
tmp->length = 0;
tmp->capacity = capacity;
return tmp;
}
/*销毁链表并释放链表的内存空间*/
void* List_Destory1(Seqlist* list)
{
TSeqList* tlist = NULL;
if (list==NULL)
{
printf("List_Destory() err:");
return NULL;
}
tlist = (TSeqList*)list;
if (tlist->node!=NULL)
{
free(tlist->node);
}
if (tlist->length!=0)
{
free(tlist);
}
return NULL;
}
/*清空链表,回到初始链表的状态*/
void* List_Clear1(Seqlist* list)
{
TSeqList* tlist = NULL;
if (list==NULL)
{
printf("List_Clear() err:");
return NULL;
}
tlist = (TSeqList*)list;
tlist->length = 0;
return NULL;
}
/*链表长度大小*/
int List_Length1(Seqlist* list)
{
TSeqList* tlist = NULL;
if (list==NULL)
{
printf("List_Length() err:");
return -1;
}
tlist = (TSeqList*)list;
return tlist->length;
}
/*链表大小获取*/
int List_Capacity1(Seqlist* list)
{
TSeqList* tlist = NULL;
if (list==NULL)
{
printf("List_Capacity() err:");
return -1;
}
tlist = (TSeqList*)list;
return tlist->capacity;
}
/*元素获取*/
SeqListNode* List_Get1(Seqlist* list, int pos)
{
TSeqList* tlist = NULL;
if (list == NULL||pos<0)
{
printf("List_Get() err:");
return NULL;
}
tlist = (TSeqList*)list;
return tlist->node[pos];
}
/*元素插入*/
int List_Inster1(Seqlist* list, SeqListNode* node, int pos)
{
TSeqList* tlist = NULL;
if (list == NULL || pos<0||node==NULL)
{
printf("List_Inster() err:");
return -1;
}
int i = 0;
SeqListNode* ret = NULL;
tlist = (TSeqList*)list;
//判断是不是链表中已经满了
if (tlist->length==tlist->capacity)
{
printf("List_Inster() err: yiman");
return -2;
}
//容错修正,比如 6歌长度 容量为20,用户在10位置插入,则进行容错
if (pos>=tlist->length)
{
pos = tlist->length;
}
//数据元素后移 头插法
for ( i = tlist->length; i > pos; i--)
{
tlist->node[i] = tlist->node[i-1];
}
//插入元素
tlist->node[i] = (unsigned int *)node;
tlist->length++;
return 0;
}
/*元素删除*/
int List_Delete1(Seqlist* list, int pos)
{
TSeqList* tlist = NULL;
if (list==NULL||pos<0)
{
printf("List_Delete() err: ");
return -1;
}
tlist = (TSeqList*)list;
SeqListNode* ret = NULL;
ret = tlist->node[pos];
int i = 0;
for ( i =pos+1; i <tlist->length; i++)
{
tlist->node[i - 1] = tlist->node[i];
}
tlist->length--;
return *(int*)ret;
}
SeqQueue.cpp
#include<string.h>
#include<iostream>
#include"SeqQueue.h"
#include"SeqList.h"
//创建一个队列相当于创建一个顺序存储线性表
SeqQueue* Queue_Creat(int capacity)
{
return List_Create1(capacity);
}
//销毁队列相当于销毁一个线性表
void* Queue_Destory(SeqQueue* queue)
{
return List_Destory1(queue);
}
//清空队列相当于清空一个线性表
void* Queue_Clear(SeqQueue* queue)
{
return List_Clear1(queue);
}
//向队列中添加元素 相当于向线性表中添加一个元素
int Queue_Append(SeqQueue* queue, void* item)
{
return List_Inster1(queue,item,Queue_Lenght(queue));
}
//出队列相当于从线性表中删除0号位置元素
int Queue_Retrieve(SeqQueue* queue)
{
return List_Delete1(queue, 0);;
}
//获取对头元素相当于获取线性表0号元素
void* Queue_Header(SeqQueue* queue)
{
return List_Get1(queue, 0);
}
//获取队列长度相当于获取线性表长度
int Queue_Lenght(SeqQueue* queue)
{
return List_Length1(queue);
}
//获取队列容量相当于获取线性表容量
int Queue_Capacity(SeqQueue* queue)
{
return List_Capacity1(queue);
}