C 栈顺序存储
// seqstack.h #ifndef _MY_SEQSTACK_H_
#define _MY_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 //_MY_SEQSTACK_H_
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#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) - );
} //求栈的大小 相当于 链表的长度
int SeqStack_Size(SeqStack* stack)
{
return SeqList_Length(stack);
} //求栈的容量 相当于 求链表的容量
int SeqStack_Capacity(SeqStack* stack)
{
return SeqList_Capacity(stack);
}
#ifndef __MY_SEQLIST_H__
#define __MY_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 //__MY_SEQLIST_H__
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "seqlist.h" //用数组来模拟线性表
typedef struct _tag_SeqList
{
int capacity;
int length;
//int *node[100];
int **node; //int node[capacity] //
//int *node[capacity];
//int *node; // int node[i]===> *(node+i)
}TSeqList; //链表 创建
SeqList* SeqList_Create(int capacity) //O(1)
{
int ret;
TSeqList *tmp = NULL;
tmp = (TSeqList *)malloc(sizeof(TSeqList));
if (tmp == NULL)
{
ret =;
printf("func SeqList_Create() err :%d \n", ret);
return NULL;
}
memset(tmp, , sizeof(TSeqList));
tmp->capacity = capacity;
tmp->length = ;
tmp->node = (int **)malloc(sizeof(void *) * capacity);
if (tmp->node == NULL)
{
ret = ;
printf("func SeqList_Create() malloc err :%d \n", ret);
return NULL;
}
memset(tmp->node, , sizeof(void *) * capacity); return tmp;
} //链表 创建
int SeqList_Create2(int capacity, SeqList**handle)
{
int ret = ;
TSeqList *tmp = NULL;
tmp = (TSeqList *)malloc(sizeof(TSeqList));
if (tmp == NULL)
{
ret =;
printf("func SeqList_Create2() err :%d \n", ret);
return ret;
}
memset(tmp, , sizeof(TSeqList));
tmp->capacity = capacity;
tmp->length = ;
tmp->node = (int **)malloc(sizeof(void *) * capacity);
if (tmp->node == NULL)
{
ret = ;
printf("func SeqList_Create2() malloc err :%d \n", ret);
return ret;
} *handle = tmp;
return ret;
} //链表 销毁
void SeqList_Destroy(SeqList* list) //O(1)
{
TSeqList *tmp = NULL;
if (list == NULL)
{
return ;
} tmp = (TSeqList *)list; if (tmp->node != NULL)
{
free(tmp->node);
}
free(tmp);
return ;
} ////链表 清空
void SeqList_Clear(SeqList* list) //O(1)
{
TSeqList *tmp = NULL;
if (list == NULL)
{
return ;
} tmp = (TSeqList *)list;
tmp->length = ;
memset(tmp->node, , (tmp->capacity * sizeof(void *)) ); return ;
} //链表 长度
int SeqList_Length(SeqList* list) //O(1)
{
TSeqList *tmp = NULL;
if (list == NULL)
{
return -;
}
tmp = (TSeqList *)list; return tmp->length;
} //链表 容量
int SeqList_Capacity(SeqList* list) //O(1)
{
TSeqList *tmp = NULL;
if (list == NULL)
{
return -;
}
tmp = (TSeqList *)list;
return tmp->capacity;
} //链表 在某一个位置 插入元素
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) //O(n)
{
TSeqList *tList = NULL;
int i = ;
if (list == NULL || node==NULL)
{
return -;
}
tList = (TSeqList *)list;
//如果满了
if (tList->length >= tList->capacity)
{
return -;
} //pos位置的容错处理
if (pos > tList->length )
{
pos = tList->length;
} for (i=tList->length; i>pos; i--) //n
{
tList->node[i] = tList->node[i-];
} tList->node[i] = (int* )node; //ok
tList->length ++; return ;
} //获取某一个位置的链表结点
SeqListNode* SeqList_Get(SeqList* list, int pos) //O(1)
{
TSeqList *tList = NULL;
SeqListNode *tmp = NULL; tList = (TSeqList *)list; if (list == NULL || pos< || pos >=tList->length )
{
return NULL;
}
tmp = tList->node[pos]; return tmp;
} //删除某一个位置的结点
SeqListNode* SeqList_Delete(SeqList* list, int pos) ////O(n)
{
int i = ;
TSeqList *tList = NULL;
SeqListNode *tmp = NULL; tList = (TSeqList *)list;
if (list == NULL || pos < || pos >= tList->length)
{
return NULL;
}
tmp = tList->node[pos]; // pos = 3
for (i=pos+; i<tList->length; i++)
{
tList->node[i-] = tList->node[i]; }
tList->length --;
return tmp;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h> #include "seqstack.h" void main()
{
int i = ;
SeqStack *stack = NULL; int a[];
for (i=; i<; i++)
{
a[i] = i+;
} stack = SeqStack_Create(); //入栈
for (i=; i<; i++)
{
SeqStack_Push(stack, &a[i]);
} //栈的属性
printf("len:%d \n", SeqStack_Size(stack));
printf("capacity:%d \n", SeqStack_Capacity(stack)); printf("top:%d \n", *((int *)SeqStack_Top(stack) ) ) ; //元素出栈 while (SeqStack_Size(stack) > )
{
printf("%d ", *( (int *)SeqStack_Pop(stack) ) );
} SeqStack_Destroy(stack); printf("hello...\n");
system("pause");
return ;
}
C 栈顺序存储的更多相关文章
- javascript实现数据结构与算法系列:栈 -- 顺序存储表示和链式表示及示例
栈(Stack)是限定仅在表尾进行插入或删除操作的线性表.表尾为栈顶(top),表头为栈底(bottom),不含元素的空表为空栈. 栈又称为后进先出(last in first out)的线性表. 堆 ...
- C语言实现栈(顺序存储方式)
#include <stdio.h> #include <stdlib.h> //提供malloc()原型 #include <stdbool.h> //提供tru ...
- 栈的C语言实现
在C++中,可以直接使用std::stack C语言实现如下: stack.c /** * @file stack.c * @brief 栈,顺序存储. * * * */ #include <s ...
- 栈(顺序栈)----C语言
栈 栈是一种运算受限的线性表,是一种先进后出的数据结构,限定只能在一端进行插入和删除操作,允许操作的一端称为栈顶,不允许操作的称为栈底 顺序栈(顺序结构) 顺序栈:用一段连续的存储空间来存储栈中的数据 ...
- c++实验4 栈及栈的应用+回文+中、后缀表达式
栈及栈的应用+回文+中.后缀表达式 1.栈顺序存储结构的基本操作算法实现 (1)栈顺序存储结构的类定义: class SeqStack { private: int maxsize; DataType ...
- C# 数据结构 基础 论述
问题: 信息世界中,计算机是加工处理的信息的载体,在这个过程中面临着三个问题: 1.如何方便高效的组织数据 2.如何在计算机中存储数据(内存和外存) 3.如何对存储的数据进行高效的操作 目的: 我们都 ...
- javascript实现数据结构与算法系列
1.线性表(Linear list) 线性表--简单示例及线性表的顺序表示和实现 线性表--线性链表(链式存储结构) 线性表的静态单链表存储结构 循环链表与双向链表 功能完整的线性链表 线性链表的例子 ...
- 栈的顺序存储 - 设计与实现 - API实现
Stack基本概念 栈是一种 特殊的线性表 栈仅能在线性表的一端进行操作 栈顶(Top):允许操作的一端 栈底(Bottom):不允许操作的一端 Stack的常用操作 创建栈 销毁栈 清空栈 进栈 出 ...
- 栈的顺序存储和链式存储c语言实现
一. 栈 栈的定义:栈是只允许在一端进行插入或删除操作的线性表. 1.栈的顺序存储 栈顶指针:S.top,初始设为-1 栈顶元素:S.data[S.top] 进栈操作:栈不满时,栈顶指针先加1,再到栈 ...
随机推荐
- SDOI2008仪仗队
这题应该注意到与b2818的不同 一个点能被看见当且仅当它与(1,1)的横纵坐标的距离gcd为1 所以问题转化为x,y<=n-1,求gcd(x,y)=1的方案数 最后要加上2 代码: var i ...
- POJ 3678 Katu Puzzle (2-SAT,常规)
题意:给出n个点,每个点上有一个数字可以0或1,然后给出m条限制,要求a和b两个点上的数字满足 a op b = c,op和c都是给定.问是否能够有一组解满足所有限制?(即点上的数字是0是1由你决定) ...
- ssh-keygen+ssh-copy-id 在linux下实现ssh无密码登录访问
环境: 192.168.2.10 192.168.2.11 实现:2.10 ssh无需密码登录到2.11 在2.10 ssh到2.11机器上,需要密码,这样对一些脚本工作不方便,因为需要密码,也就是需 ...
- 异常处理 Exception
一.异常类 1.在C#中所有的异常都是使用一个异常类型的示例对象表示的,这些异常类型都是继承自System.Exception类型,或者直接使用System.Exception类型的实例对象: 2.在 ...
- 自杀程序&递归删除目录
自杀程序,删掉自身,包括执行程序所在的文件夹. 仅在windows下进行了测试,使用语言C# string fileName = Path.GetTempPath() + "killmyse ...
- A Fast Priority Queue Implementation of the Dijkstra Shortest Path Algorithm
http://www.codeproject.com/Articles/24816/A-Fast-Priority-Queue-Implementation-of-the-Dijkst http:// ...
- CentOS 7 nfs客户端挂载问题
配置nfs服务器时,服务器端挂载没有问题,但是客户端挂载时提示:mount.nfs: Connection timed out 解决方法1: 关闭firewalld # systemctl stop ...
- 使用 EasyBCD 安装Ubuntu 14.04 Error 15: file not found错误的解决方法
今天安装Window7 和 Ubuntu 14.04 双系统时,出现如下异常,记录一下. 安装过程是参考 http://www.linuxidc.com/Linux/2014-04/100369.ht ...
- HDU 1024 DP Max Sum Plus Plus
题意:本题的大致意思为给定一个数组,求其分成m个不相交子段和最大值的问题. kuangbin专题. dp[i][j]=Max(dp[i][j-1]+a[j] , max( dp[i-1][k] ) + ...
- 基于samba实现win7与linux之间共享文件_阳仔_新浪博客
基于samba实现win7与linux之间共享文件_阳仔_新浪博客 然后启动samba执行如下指令: /dev/init.d/smb start 至此完成全部配置.