1 实现原理,复用了线性表的顺序结构。 栈最大特点:后进先出。
具体代码如下:
#ifndef _SEQLIST_H_
#define _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
#include <stdio.h>
#include <malloc.h>
#include "SeqList.h"
typedef unsigned int TSeqListNode;
typedef struct _tag_SeqList
{
int capacity;
int length;
TSeqListNode* node;
} TSeqList;
SeqList* SeqList_Create(int capacity) // O(1)
{
TSeqList* ret = NULL;
if( capacity >= 0 )
{
ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(TSeqListNode) * capacity);
}
if( ret != NULL )
{
ret->capacity = capacity;
ret->length = 0;
ret->node = (TSeqListNode*)(ret + 1);
}
return ret;
}
void SeqList_Destroy(SeqList* list) // O(1)
{
free(list);
}
void SeqList_Clear(SeqList* list) // O(1)
{
TSeqList* sList = (TSeqList*)list;
if( sList != NULL )
{
sList->length = 0;
}
}
int SeqList_Length(SeqList* list) // O(1)
{
TSeqList* sList = (TSeqList*)list;
int ret = -1;
if( sList != NULL )
{
ret = sList->length;
}
return ret;
}
int SeqList_Capacity(SeqList* list) // O(1)
{
TSeqList* sList = (TSeqList*)list;
int ret = -1;
if( sList != NULL )
{
ret = sList->capacity;
}
return ret;
}
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) // O(n)
{
TSeqList* sList = (TSeqList*)list;
int ret = (sList != NULL);
int i = 0;
ret = ret && (sList->length + 1 <= sList->capacity);
ret = ret && (0 <= pos);
if( ret )
{
if( pos >= sList->length )
{
pos = sList->length;
}
for(i=sList->length; i>pos; i--)
{
sList->node[i] = sList->node[i-1];
}
sList->node[i] = (TSeqListNode)node;
sList->length++;
}
return ret;
}
SeqListNode* SeqList_Get(SeqList* list, int pos) // O(1)
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = NULL;
if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
{
ret = (SeqListNode*)(sList->node[pos]);
}
return ret;
}
SeqListNode* SeqList_Delete(SeqList* list, int pos) // O(n)
{
TSeqList* sList = (TSeqList*)list;
SeqListNode* ret = SeqList_Get(list, pos);
int i = 0;
if( ret != NULL )
{
for(i=pos+1; i<sList->length; i++)
{
sList->node[i-1] = sList->node[i];
}
sList->length--;
}
return ret;
}
#ifndef _SEQSTACK_H_
#define _SEQSTACK_H_
typedef void SeqStack;
SeqStack* SeqStack_Create(int capacity);
void SeqStack_Destroy(SeqStack* stack);
void SeqStack_Clear(SeqStack* stack);
int SeqStack_Push(SeqStack* stack, void* item);
void* SeqStack_Pop(SeqStack* stack);
void* SeqStack_Top(SeqStack* stack);
int SeqStack_Size(SeqStack* stack);
int SeqStack_Capacity(SeqStack* stack);
#endif
#include "SeqStack.h"
#include "SeqList.h"
SeqStack* SeqStack_Create(int capacity)
{
return SeqList_Create(capacity);
}
void SeqStack_Destroy(SeqStack* stack)
{
SeqList_Destroy(stack);
}
void SeqStack_Clear(SeqStack* stack)
{
SeqList_Clear(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);
}
int SeqStack_Size(SeqStack* stack)
{
return SeqList_Length(stack);
}
int SeqStack_Capacity(SeqStack* stack)
{
return SeqList_Capacity(stack);
}
测试代码:
#include <stdio.h>
#include <stdlib.h>
#include "SeqStack.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
SeqStack* stack = SeqStack_Create(20);
int a[10];
int i = 0;
for(i=0; i<10; i++)
{
a[i] = i;
SeqStack_Push(stack, a + i);
}
printf("Top: %d\n", *(int*)SeqStack_Top(stack));
printf("Capacity: %d\n", SeqStack_Capacity(stack));
printf("Length: %d\n", SeqStack_Size(stack));
while( SeqStack_Size(stack) > 0 )
{
printf("Pop: %d\n", *(int*)SeqStack_Pop(stack));
}
SeqStack_Destroy(stack);
return 0;
}