#ifndef __MY_SEQLIST_H__
#define __MY_SEQLIST_H__
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__
#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_
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "seqlist.h"
//用数组来模拟线性表
typedef struct _tag_SeqList
{
int capacity;
int length;
//int *node[100];
int **node; //int node[capacity] //
//int *node[capacity];
//int *node; // int node[i]===> *(node+i)
}TSeqList;
//链表 创建
SeqList* SeqList_Create(int capacity) //O(1)
{
int ret;
TSeqList *tmp = NULL;
tmp = (TSeqList *)malloc(sizeof(TSeqList));
if (tmp == NULL)
{
ret =1;
printf("func SeqList_Create() err :%d \n", ret);
return NULL;
}
memset(tmp, 0, sizeof(TSeqList));
tmp->capacity = capacity;
tmp->length = 0;
tmp->node = (int **)malloc(sizeof(void *) * capacity);
if (tmp->node == NULL)
{
ret = 2;
printf("func SeqList_Create() malloc err :%d \n", ret);
return NULL;
}
memset(tmp->node, 0, sizeof(void *) * capacity);
return tmp;
}
//链表 创建
int SeqList_Create2(int capacity, SeqList**handle)
{
int ret = 0;
TSeqList *tmp = NULL;
tmp = (TSeqList *)malloc(sizeof(TSeqList));
if (tmp == NULL)
{
ret =1;
printf("func SeqList_Create2() err :%d \n", ret);
return ret;
}
memset(tmp, 0, sizeof(TSeqList));
tmp->capacity = capacity;
tmp->length = 0;
tmp->node = (int **)malloc(sizeof(void *) * capacity);
if (tmp->node == NULL)
{
ret = 2;
printf("func SeqList_Create2() malloc err :%d \n", ret);
return ret;
}
*handle = tmp;
return ret;
}
//链表 销毁
void SeqList_Destroy(SeqList* list) //O(1)
{
TSeqList *tmp = NULL;
if (list == NULL)
{
return ;
}
tmp = (TSeqList *)list;
if (tmp->node != NULL)
{
free(tmp->node);
}
free(tmp);
return ;
}
////链表 清空
void SeqList_Clear(SeqList* list) //O(1)
{
TSeqList *tmp = NULL;
if (list == NULL)
{
return ;
}
tmp = (TSeqList *)list;
tmp->length = 0;
memset(tmp->node, 0, (tmp->capacity * sizeof(void *)) );
return ;
}
//链表 长度
int SeqList_Length(SeqList* list) //O(1)
{
TSeqList *tmp = NULL;
if (list == NULL)
{
return -1;
}
tmp = (TSeqList *)list;
return tmp->length;
}
//链表 容量
int SeqList_Capacity(SeqList* list) //O(1)
{
TSeqList *tmp = NULL;
if (list == NULL)
{
return -1;
}
tmp = (TSeqList *)list;
return tmp->capacity;
}
//链表 在某一个位置 插入元素
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) //O(n)
{
TSeqList *tList = NULL;
int i = 0;
if (list == NULL || node==NULL)
{
return -1;
}
tList = (TSeqList *)list;
//如果满了
if (tList->length >= tList->capacity)
{
return -2;
}
//pos位置的容错处理
if (pos > tList->length )
{
pos = tList->length;
}
for (i=tList->length; i>pos; i--) //n
{
tList->node[i] = tList->node[i-1];
}
tList->node[i] = (int* )node; //ok
tList->length ++;
return 0;
}
//获取某一个位置的链表结点
SeqListNode* SeqList_Get(SeqList* list, int pos) //O(1)
{
TSeqList *tList = NULL;
SeqListNode *tmp = NULL;
tList = (TSeqList *)list;
if (list == NULL || pos<0 || pos >=tList->length )
{
return NULL;
}
tmp = tList->node[pos];
return tmp;
}
//删除某一个位置的结点
SeqListNode* SeqList_Delete(SeqList* list, int pos) ////O(n)
{
int i = 0;
TSeqList *tList = NULL;
SeqListNode *tmp = NULL;
tList = (TSeqList *)list;
if (list == NULL || pos <0 || pos >= tList->length)
{
return NULL;
}
tmp = tList->node[pos];
// pos = 3
for (i=pos+1; i<tList->length; i++)
{
tList->node[i-1] = tList->node[i];
}
tList->length --;
return tmp;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.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));
}
//提取队头元素 相当于 删除链表0号位置元素
void* SeqQueue_Retrieve(SeqQueue* queue)
{
return SeqList_Delete(queue, 0) ;
}
//获取队头元素 相当于 获取 链表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);
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "seqqueue.h"
void main()
{
int i = 0, a[10];
SeqQueue *queue = NULL;
for (i=0; i<10; i++)
{
a[i] = i + 1;
}
queue = SeqQueue_Create(10);
//向队列的尾部 添加元素
for (i=0; i<10; i++)
{
SeqQueue_Append(queue, a + i);
}
//获取队列的属性
printf("capacity:%d \n", SeqQueue_Capacity(queue));
printf("length:%d \n", SeqQueue_Length(queue));
printf("队头: %d \n", *( (int *)SeqQueue_Header(queue) ));
//销毁 队列
while (SeqQueue_Length(queue) > 0 )
{
int tmp;
tmp = *( (int *)SeqQueue_Retrieve(queue) );
printf("%d ", tmp);
}
SeqQueue_Destroy(queue);
printf("hello...\n");
system("pause");
return ;
}