可复用的顺序栈
来源:互联网 发布:mac无损音乐播放器 编辑:程序博客网 时间:2024/06/08 04:24
#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;
}
- 可复用的顺序栈
- 【顺序栈作业】顺序栈的实现
- 顺序栈:栈的顺序存储结构
- 堆栈的顺序存储---顺序栈
- 可复用的顺序表
- 栈的顺序实现
- 栈的顺序存储
- 顺序栈的运算
- 顺序栈的实现
- 顺序栈的子系统
- 顺序栈的操作
- 顺序栈的实现
- 顺序栈的操作
- 顺序栈的操作
- 顺序栈的实现
- 顺序栈的实现
- 顺序栈的实现
- 栈的顺序存储
- Photoshop技巧
- Log4J
- 『数学模型』图
- extjs 云资源平台 2014.2.5-Portal门户页面
- java日期处理
- 可复用的顺序栈
- BigDecimal
- 设计模式之-------------策略模式
- SystemKit 系统分析工具
- Tomcat
- <windows核心编程>动态链接库(一)
- Eclipse
- System.getProperty
- 浅析JBPM工作流引擎