//seqlist.h
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
typedef void SeqList;
typedef void SeqListNode;
SeqList *SeqList_Create(int capacity);
void SeqList_Clear(SeqList *list);
void SeqList_Destroy(SeqList *list);
int SeqList_Length(SeqList *list);
int SeqList_Capacity(SeqList *list);
int SeqList_Insert(SeqList *list, SeqListNode *node, int pos);
SeqListNode *SeqList_Delete(SeqList *list, int pos);
SeqListNode *SeqList_Get(SeqList *list, int pos);
#endif
//seqstack.h
#pragma once
typedef void SeqStack;
SeqStack *SeqStack_Create(int capacity);
void SeqStack_Clear(SeqStack* stack);
void SeqStack_Destroy(SeqStack* stack);
int SeqStack_Capacity(SeqStack* stack);
int SeqStack_Size(SeqStack* stack);
int SeqStack_Push(SeqStack* stack, void *item);
void *SeqStack_Pop(SeqStack* stack);
void *SeqStack_Top(SeqStack* stack);
//seqlist.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "seqlist.h"
typedef struct _tag_SeqList
{
int capacity;
int length;
unsigned int *node;
}TSeqList;
SeqList *SeqList_Create(int capacity)
{
TSeqList *ret = NULL;
if (capacity < 0)
{
printf("SeqList_Create err:capacity < 0\n");
return NULL;
}
ret = (TSeqList *)malloc(sizeof(TSeqList) + sizeof(unsigned int *)*capacity);
if (ret == NULL)
{
printf("SeqList_Create err:malloc err\n");
return NULL;
}
ret->node = (unsigned int *)(ret + 1);
ret->capacity = capacity;
ret->length = 0;
return ret;
}
void SeqList_Clear(SeqList *list)
{
TSeqList *tlist = NULL;
if (list == NULL)
{
printf("SeqList_Clear err:list == NULL\n");
return;
}
tlist = (TSeqList *)list;
tlist->length = 0;
}
void SeqList_Destroy(SeqList *list)
{
if (list == NULL)
{
printf("SeqList_Destroy err:list == NULL\n");
return;
}
free(list);
}
int SeqList_Length(SeqList *list)
{
TSeqList *tlist = NULL;
if (list == NULL)
{
printf("SeqList_Length err:list == NULL\n");
return -1;
}
tlist = (TSeqList *)list;
return tlist->length;
}
int SeqList_Capacity(SeqList *list)
{
TSeqList *tlist = NULL;
if (list == NULL)
{
printf("SeqList_Capacity err:list == NULL\n");
return -1;
}
tlist = (TSeqList *)list;
return tlist->capacity;
}
int SeqList_Insert(SeqList *list, SeqListNode* node, int pos)
{
int i = 0,ret=0;
TSeqList *tlist = NULL;
if (list == NULL || node == NULL || pos < 0)
{
ret = -1;
printf("SeqList_Insert err%d:list == NULL || node == NULL\n",ret);
return ret;
}
tlist = (TSeqList*)list;
if (tlist->length == tlist->capacity)
{
ret = -2;
printf("SeqList_Insert err%d:tlist->length == tlist->capacity\n", ret);
return ret;
}
if (pos > tlist->capacity)
{
ret = -3;
printf("SeqList_Insert err%d:pos > tlist->capacity\n", ret);
return ret;
}
if (pos > tlist->length)
{
pos = tlist->length;
}
for (i = tlist->length; i > pos; i--)
{
tlist->node[i] = tlist->node[i - 1];
}
tlist->node[pos] = (unsigned int )node;
tlist->length++;
return pos;
}
SeqListNode *SeqList_Delete(SeqList *list, int pos)
{
int i = 0;
SeqListNode *ret = NULL;
TSeqList *tlist = NULL;
if (list == NULL || pos < 0)
{
printf("SeqList_Delete err:list == NULL || pos < 0\n");
return NULL;
}
tlist = (TSeqList*)list;
if (pos > tlist->length)
{
printf("SeqList_Delete err:pos < tlist->length\n");
return NULL;
}
ret = (unsigned int *)tlist->node[pos];
for (i = pos+1; i < tlist->length; i++)//pos取值范围:[0,length-1]
{
tlist->node[i - 1] = tlist->node[i];
}
tlist->length--;
return ret;
}
SeqListNode *SeqList_Get(SeqList *list, int pos)
{
SeqListNode *ret = NULL;
TSeqList *tlist = NULL;
if (list == NULL || pos < 0)
{
printf("SeqList_Get err:list == NULL || pos < 0\n");
return NULL;
}
tlist = (TSeqList*)list;
if (pos > tlist->length)
{
printf("tlist->length:%d\n", tlist->length);
printf("SeqList_Get err:pos < tlist->length\n");
return NULL;
}
return (SeqListNode *)tlist->node[pos];
}
//seqstack.cpp
#include "seqlist.h"
#include "seqstack.h"
SeqStack *SeqStack_Create(int capacity)
{
return SeqList_Create(capacity);
}
void SeqStack_Clear(SeqStack *stack)
{
SeqList_Clear(stack);
}
void SeqStack_Destroy(SeqStack *stack)
{
SeqList_Destroy(stack);
}
int SeqStack_Capacity(SeqStack *stack)
{
return SeqList_Capacity(stack);
}
int SeqStack_Size(SeqStack *stack)
{
return SeqList_Length(stack);
}
int SeqStack_Push(SeqStack *stack, void *item)
{
return SeqList_Insert(stack, item, SeqList_Length(stack));
}
void *SeqStack_Pop(SeqStack *stack)
{
return SeqList_Delete(stack, SeqList_Length(stack) - 1);
}
void *SeqStack_Top(SeqStack *stack)
{
return SeqList_Get(stack, SeqList_Length(stack)-1);
}
//栈的顺序存储设计与实现.cpp
#include"seqstack.h"
#include<stdio.h>
void main()
{
int i = 0;
int a[10];
SeqStack *stack = NULL;
stack = SeqStack_Create(10);
if (stack == NULL)
{
return;
}
for (i = 0; i < 5; i++)
{
a[i] = i + 1;
SeqStack_Push(stack, &a[i]);
}
printf("Capacity:%d \n", SeqStack_Capacity(stack));
printf("length:%d\n", SeqStack_Size(stack));
printf("top:%d\n", *((int *)SeqStack_Top(stack)));
while (SeqStack_Size(stack)>0)
{
int tmp = *((int *)SeqStack_Pop(stack));
printf("tmp:%d", tmp);
}
printf("\n");
}