1 Realization Ideas
State after inserting 4 elements into a stack
Perform process analysis:
2 Code implementation
Clib.h Interface Definition
1typedefstructCstashtag2 {3 intEle_size;//The number of bytes occupied by each element in the stack4 intcapacity;//The capacity of the stack, the number of elements that the stack currently (does not expand) can hold5 intNext//equivalent to the stack pointer (marks the next empty index), the number of current elements in the stack6UnsignedChar* STORAGE;//Stack storage space character pointer, dynamically allocated byte array7 } Cstash;8 9 voidInitalize (cstash* S,intsize);Ten voidCleanup (cstash*s); One intAdd (cstash* s,Const void*element); A void* FETCH (cstash* s,intindex); - intCount (cstash*s); - voidInflate (cstash* s,intIncrease =Ten);
2 Clib.cpp Function implementation
1#include <string>2#include <iostream>3#include <cassert>4#include"Clib.h"5 6 using namespacestd;7 8 voidInitalize (cstash* S,intsz)9 {TenS->ele_size =sz; OneS->capacity =0; AS->next =0; -S->storage =0; - } the - intAdd (cstash* s,Const void*Element) - { - if(S->next >= s->capacity) + { - inflate (s); + } A intStartbytes = S->next * s->ele_size; atUnsignedChar* E = (unsignedChar*) element; - - for(intI=0; i<s->ele_size; i++) -S->storage[startbytes + i] =E[i]; -s->next++; - in returnS->next-1; - } to + //Remove the Stack element at index - void* FETCH (cstash* s,intindex) the { *Assert0<=index); $ if(Index >= s->next)Panax Notoginseng { - return 0; the } + return& (S->storage[index * s->ele_size]); A } the + //returns the number of elements in the stack - intCount (cstash*s) $ { $ returnS->Next; - } - the //Expand stack space, add increase element space - voidInflate (cstash* s,intIncrease)Wuyi { theprintf"Inflate Increase%d\n", increase); - WuASSERT (Increase >0); - About //original stack length + number of stack elements added $ intnewcapacity = s->capacity +increase; - intNewbytes = newcapacity * s->ele_size;//number of new stack space bytes - intOldbytes = s->capacity * s->ele_size;//bytes of old stack space - AUnsignedChar* B =NewUnsignedChar[Newbytes];//allocating new stack space on the heap + the if(!oldbytes) - { $ //Copy the contents of the old stack space into the new stack space and free up the old stack space the //Copy the data from the old memory block to the newly allocated memory block the for(intI=0; i<oldbytes; i++) theB[i] = s->Storage[i]; theDelete [] (s->storage);//freeing old memory blocks - } in the theS->storage = b;//make stack storage space character Pointer S->storage point to newly allocated memory block Abouts->capacity = newcapacity;//capacity of the update stack the } the the + //cleanup Stack storage space character Pointer - voidCleanup (cstash*s) the {Bayi if(S->storage! =0) the { thecout<<"Freeing Storage"<<Endl; -Delete []s->storage; - } the } the the the intMainintargcChar*argv[]) - { the the Cstash Stash; the 94 CharStr1[] ="d1111"; the CharStr2[] ="d2222"; the CharStr3[] ="d3333"; the CharStr4[] ="d4444";98 AboutInitalize (&stash, -); - 101Add (&Stash, str1);102Add (&Stash, str2);103Add (&Stash, STR3);104Add (&Stash, STR4); the 106UnsignedChar* result = (unsignedChar*) Fetch (&stash,2);107printf"Fetch result%s\n", result);108 109 return 0; the};
Output:
Inflate Increase 10
Fetch result d3333
Implement stacks with one-dimensional arrays (C + + programming thought p120)