码迷,mamicode.com
首页 > 编程语言 > 详细

用一维数组实现栈(C++编程思想 p120)

时间:2015-06-07 20:04:39      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

1 实现思路

向栈中插入4个元素后的状态

技术分享

执行过程分析:

技术分享

 

2 代码实现

clib.h 接口定义

 1 typedef struct CStashTag
 2 {
 3     int ele_size;                    //栈中每个元素的占用的字节数 
 4     int capacity;                    //栈的容量,栈当前(不扩展)可容纳的元素的个数
 5     int next;                        //相当于栈指针(标记下一个空位索引),栈中当前元素的个数
 6     unsigned char* storage;            //栈存储空间字符指针,动态分配的字节数组
 7 } CStash;
 8 
 9 void initalize(CStash* s, int size);
10 void cleanup(CStash* s);
11 int add(CStash* s, const void* element);
12 void* fetch(CStash* s, int index);
13 int count(CStash* s);
14 void inflate(CStash* s, int increase = 10);

2 Clib.cpp 函数实现

  1 #include <string>
  2 #include <iostream>
  3 #include <cassert>
  4 #include "clib.h"
  5 
  6 using namespace std;
  7 
  8 void initalize(CStash* s, int sz)
  9 {
 10     s->ele_size = sz;     
 11     s->capacity = 0;  
 12     s->next = 0;
 13     s->storage = 0; 
 14 }
 15 
 16 int add(CStash* s, const void* element)
 17 {
 18     if (s->next  >=  s->capacity)
 19     {
 20         inflate(s);
 21     }
 22     int startBytes = s->next * s->ele_size;
 23     unsigned char* e = (unsigned char*)element;
 24 
 25     for (int i=0; i<s->ele_size; i++)
 26         s->storage[startBytes + i] = e[i];
 27     s->next++;
 28 
 29     return s->next - 1;
 30 }
 31 
 32 //取出索引index处的栈元素
 33 void* fetch(CStash* s, int index)
 34 {
 35     assert(0 <= index);
 36     if (index >= s->next)
 37     {
 38         return 0;
 39     }
 40     return &(s->storage[index * s->ele_size]);
 41 }
 42 
 43 //返回栈中元素的个数
 44 int count(CStash* s)
 45 {
 46     return s->next;
 47 }
 48 
 49 //扩展栈空间,增加increase个元素空间
 50 void inflate(CStash* s, int increase)
 51 {
 52     printf("inflate increase %d\n", increase);
 53 
 54     assert(increase > 0);
 55 
 56     //原栈长 + 增加的栈元素个数
 57     int newCapacity = s->capacity + increase;
 58     int newBytes = newCapacity * s->ele_size;   //新的栈空间字节数
 59     int oldBytes = s->capacity * s->ele_size;        //旧的栈空间字节数
 60 
 61     unsigned char* b = new unsigned char[newBytes]; //在堆上分配新的栈空间
 62 
 63     if (!oldBytes) 
 64     {
 65         //拷贝旧的栈空间的内容到新的栈空间,并释放旧的栈空间
 66         //把旧内存块中的数据拷贝到新分配的内存块
 67         for (int i=0; i<oldBytes; i++)
 68             b[i] = s->storage[i];
 69         delete [] (s->storage); //释放旧的内存块
 70     }
 71 
 72 
 73     s->storage = b; //使栈存储空间字符指针s->storage指向新分配的内存块
 74     s->capacity = newCapacity; //更新栈的容量
 75 }
 76 
 77 
 78 //清理栈存储空间字符指针
 79 void cleanup(CStash* s)
 80 {
 81     if (s->storage != 0)
 82     {
 83         cout<<"freeing storage"<<endl;
 84         delete []s->storage;
 85     }
 86 }
 87 
 88 
 89 int main(int argc, char* argv[])
 90 {
 91 
 92     CStash stash;
 93 
 94     char str1[] = "d1111";
 95     char str2[] = "d2222";
 96     char str3[] = "d3333";
 97     char str4[] = "d4444";
 98 
 99     initalize(&stash, 20);
100 
101     add(&stash, str1);
102     add(&stash, str2);
103     add(&stash, str3);
104     add(&stash, str4);
105 
106     unsigned char* result = (unsigned char*)fetch(&stash, 2);
107     printf("fetch result %s\n", result);
108 
109     return 0;
110 };


输出:

inflate increase 10
fetch result d3333

 

用一维数组实现栈(C++编程思想 p120)

标签:

原文地址:http://www.cnblogs.com/asnjudy/p/4558959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!