可复用的顺序栈


#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,int 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"


typedef void SeqStack;


SeqStack* SeqStack_Create(int capacity)
{
return SeqList_Create();
}


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)
{
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"
#include"SeqList.h"




int main(int argc, char *argv[]) 
{
SeqStack* Stack = (SeqStack*)Stack;
int a[10];
int i ;
for(i = 0;i<10;i++)
{
a[i] = i;
}
printf("TOP: %d\n ",*(int*)SeqStack_Top(Stack));
printf("Length: %d\n ",SeqStack_Size(Stack));
printf("capacity: %d\n "SeqStack_Capacity(Stack));

while(SeqStack_Size(Stack) > 0)
{
printf("Pop: %d\n ",*(int*)SeqStack_Pop(Stack));
}

SeqStack_Destroy(Stack);

printf("Press any key to continue");
getchar();

return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值