目录:
- 代码:
- 分析:
- 汇编:
代码:
SeqList.h
#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
SeqList.c
#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) //定义创建表函数
{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) //定义销毁表函数
{free(list);
}void SeqList_Clear(SeqList* list) //定义清空重设表函数
{TSeqList* sList = (TSeqList*)list;if( sList != NULL ){sList->length = 0;//重设置0}
}int SeqList_Length(SeqList* list) //定义获取表当前长度函数
{TSeqList* sList = (TSeqList*)list;int ret = -1;if( sList != NULL ){ret = sList->length;//获取当前表长度}return ret;//返回长度
}int SeqList_Capacity(SeqList* list) //定义获取表容量函数
{TSeqList* sList = (TSeqList*)list;int ret = -1;if( sList != NULL ){ret = sList->capacity;//获取容量}return ret;//返回容量
}int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) //定义插入节点函数
{TSeqList* sList = (TSeqList*)list;int ret = (sList != NULL);int i = 0;ret = ret && (sList->length + 1 <= sList->capacity);//判断表是否为空与表是否有节点插入新节点ret = ret && (0 <= pos);//判断pos是否正常if( ret ){if( pos >= sList->length )//如果pos大于当前长度{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)//定义获取节点数据函数
{TSeqList* sList = (TSeqList*)list;SeqListNode* ret = NULL;if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )//判断表是否为空与pos是否在范围{ret = (SeqListNode*)(sList->node[pos]);//获取节点数据}return ret;
}SeqListNode* SeqList_Delete(SeqList* list, int pos)//定义删除表节点函数
{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;//返回删除节点
}
SeqStack.h
#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
SeqStack.c
#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);
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "SeqStack.h"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;
}
分析:
汇编: