可复用的链栈

#ifndef _LINKLIST_H_
#define _LINKLIST_H_


typedef void LinkList;
typedef struct _tag_LinkListNode LinkListNode;
struct _tag_LinkListNode
{
    LinkListNode* next;
};


LinkList* LinkList_Create();


void LinkList_Destroy(LinkList* list);


void LinkList_Clear(LinkList* list);


int LinkList_Length(LinkList* list);


int LinkList_Insert(LinkList* list, LinkListNode* node, int pos);


LinkListNode* LinkList_Get(LinkList* list, int pos);


LinkListNode* LinkList_Delete(LinkList* list, int pos);


#endif


#include <stdio.h>
#include <malloc.h>
#include "LinkList.h"


typedef struct _tag_LinkList
{
    LinkListNode header;
    int length;
} TLinkList;


LinkList* LinkList_Create() // O(1)
{
    TLinkList* ret = (TLinkList*)malloc(sizeof(TLinkList));
    
    if( ret != NULL )
    {
        ret->length = 0;
        ret->header.next = NULL;
    }
    
    return ret;
}


void LinkList_Destroy(LinkList* list) // O(1)
{
    free(list);
}


void LinkList_Clear(LinkList* list) // O(1)
{
    TLinkList* sList = (TLinkList*)list;
    
    if( sList != NULL )
    {
        sList->length = 0;
        sList->header.next = NULL;
    }
}


int LinkList_Length(LinkList* list) // O(1)
{
    TLinkList* sList = (TLinkList*)list;
    int ret = -1;
    
    if( sList != NULL )
    {
        ret = sList->length;
    }
    
    return ret;
}


int LinkList_Insert(LinkList* list, LinkListNode* node, int pos) // O(n)

    TLinkList* sList = (TLinkList*)list;
    int ret = (sList != NULL) && (pos >= 0) && (node != NULL);
    int i = 0;
    
    if( ret )
    {
        LinkListNode* current = (LinkListNode*)sList;
        
        for(i=0; (i<pos) && (current->next != NULL); i++)
        {
            current = current->next;
        }
        
        node->next = current->next;
        current->next = node;
        
        sList->length++;
    }
    
    return ret;
}


LinkListNode* LinkList_Get(LinkList* list, int pos) // O(n)
{
    TLinkList* sList = (TLinkList*)list;
    LinkListNode* ret = NULL;
    int i = 0;
    
    if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
    {
        LinkListNode* current = (LinkListNode*)sList;
        
        for(i=0; i<pos; i++)
        {
            current = current->next;
        }
        
        ret = current->next;
    }
    
    return ret;
}


LinkListNode* LinkList_Delete(LinkList* list, int pos) // O(n)
{
    TLinkList* sList = (TLinkList*)list;
    LinkListNode* ret = NULL;
    int i = 0;
    
    if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
    {
        LinkListNode* current = (LinkListNode*)sList;
        
        for(i=0; i<pos; i++)
        {
            current = current->next;
        }
        
        ret = current->next;
        current->next = ret->next;
        
        sList->length--;
    }
    
    return ret;
}


#ifndef _LINKSTACK_H_
#define _LINKSTACK_H_


typedef void LinkStack;


LinkStack* LinkStack_Create();


void LinkStack_Destroy(LinkStack* Stack);


void LinkStack_Clear(LinkStack* Stack);


int LinkStack_Push(LinkStack* Stack,void* item);


void* LinkStack_Pop(LinkStack* Stack);


void* LinkStack_Top(LinkStack* Stack);


int LinkStack_Size(LinkStack* Stack);


#endif


#include"LinkStack.h"
#include"LinkList.h"
#include<malloc.h>


typedef struct _tag_LinkStackNode
{
LinkListNode header;
void* item;
}TLinkStackNode;


LinkStack* LinkStack_Create()
{
return LinkList_Create();
}


void LinkStack_Destroy(LinkStack* Stack)
{
LinkStack_Clear(Stack);
LinkList_Destroy(Stack);
}


void LinkStack_Clear(LinkStack* Stack)
{
while(LinkStack_Size(Stack)>0)
{
LinkStack_Pop(Stack);
}
}


int LinkStack_Push(LinkStack* Stack,void* item)
{
TLinkStackNode* node = (TLinkStackNode*)malloc(sizeof(TLinkStackNode));
int ret = (node != NULL)&&(item != NULL);
if(ret)
{
node->item = item;
ret = LinkList_Insert(Stack,(LinkListNode*)node,0);
}
if(!ret)
{
free(node);
}
return ret;
}


void* LinkStack_Pop(LinkStack* Stack)
{
TLinkStackNode* node = (TLinkStackNode*)LinkList_Delete(Stack,0);
void* ret = NULL;
if(node != NULL)
{
ret = node->item;
free(node);
}
return ret;
}


void* LinkStack_Top(LinkStack* Stack)
{
TLinkStackNode* node = (TLinkStackNode*)LinkList_Get(Stack,0);
void* ret = NULL;
if(node != NULL)
{
ret = node->item;
}
return ret;
}


int LinkStack_Size(LinkStack* Stack)
{
return LinkList_Length(Stack); 
}


#include <stdio.h>
#include <stdlib.h>
#include"LinkStack.h"


/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
LinkStack* Stack = LinkStack_Create();
int a[10];
int i;
for(i = 0;i<10;i++)
{
a[i] = i;
// LinkStack_Push(Stack,a[i]);
}
printf("Top: %d\n",*(int*)LinkStack_Top(Stack));
printf("Length: %d\n",LinkStack_Size(Stack));

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

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

return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值