user-0 提问: 顺序栈的创建

回复:
user: user-1
content: 稍微修改了下，你试试  void create(Stack *S, int stacksize) {     S->base = (int*)malloc((stacksize+1)*sizeof(int)); //多分配一个空间     if (!S->base)         exit(0);     S->top = S->base; //base中不放置栈元素，用于判断空栈等     S->size = stacksize; }  void push(Stack *S, int elem) {     if (S->top - S->base >= S->size)     {         S->base = (int*)realloc(S->base, (S->size + 1 + 10)*sizeof(int)); //多分配一个空间         if (!S->base)             exit(0);         S->top = S->base + S->size;         S->size += 10;     }     *(++S->top) = elem; }  void pop(Stack *S) {     int popnum;     if (S->top == S->base)     {         printf("栈空");         return;     }     popnum = *(S->top--);     printf("%d", popnum); }

user: user-1
content: 好多问题  1：  void push(Stack*S,int elem) { if(S->top - S->base>=S->size) {        S->base=(int*)realloc(S->base,(S->size+10)*sizeof(int));    if(!S->base)    exit(0);    S->top=S->base+S->size;    S->size+=10; } *S->top++ = elem; //你的栈顶永远指向真实栈顶的下一个地方 }  2： void push(Stack*S,int elem) { if(S->top - S->base>=S->size) //这个判断，除非发生在你数组已经越界的情况下 {        S->base=(int*)realloc(S->base,(S->size+10)*sizeof(int));    if(!S->base)    exit(0);    S->top=S->base+S->size;    S->size+=10; }  3：  void show(Stack*S) {    printf("The stack is:");    while(S->top!=S->base)    {    printf("%d  ",*S->top--); //你循环打印一遍，top一直减到栈顶，下一次你还怎么取得栈顶元素？    }    printf("\n");  }

user: user-2
content: #include <stdio.h> #include<stdlib.h>  #define MAXSIZE 10  typedef struct stack {     int *top;     int *base;     int size; }Stack;  enum {     PUSH = 1,     POP,     EXIT };  Stack *create(void) {     Stack *s = (Stack *)malloc(sizeof(Stack));     if (!s) {         fprintf(stderr, "%d, %smalloc error!\n", __LINE__, __func__);         return NULL;     }     s->base = NULL;     s->top = NULL;     s->size = 0;      return s; } int is_empty(Stack *s) {     if (s->size == 0)         return 1;     else         return 0; } int is_full(Stack *s) {     if (s->size >= MAXSIZE)         return 1;     else         return 0; }  void push(Stack *s, int elem) {     if (is_full(s)) {         fprintf(stdout, "stack is full!\n");         return;     }     s->base = (int *)realloc(s->base, (s->size +1) * sizeof(int));     if (!(s->base)) {         fprintf(stderr, "%d, %smalloc error!\n", __LINE__, __func__);         return;     }     //printf("s->size = %d\n", s->size);     //printf("s->base = %p\n", s->base);     *(s->base + s->size) = elem;     s->top = s->base + s->size+1;     //printf("len = %lu\n", (s->size+1)*sizeof(int));     //printf("s->top = %p, s->base = %p\n", s->top, s->base);     s->size += 1; }  void pop(Stack *s, int *elem) {     if (is_empty(s)) {         fprintf(stdout, "stack is empty!\n");         return;     }     *elem = *(s->top - 1);     s->top -= 1;     s->size--; }  void show(Stack *s) {     printf("stack list: \n");     while (s->base != s->top - 1) {         printf("%d\t", *(s->top - 1));         s->top -= 1;     }     printf("%d\n", *(s->base)); } static void menu(void) {         printf("-----------------------------------------------\n");         printf("                main     menu                  \n");         printf("             1    ---   入栈（Update）         \n");         printf("             2    ---   出栈（Delete）         \n");         printf("             3    ---   退出（ exit ）         \n");         printf("-----------------------------------------------\n");         printf("请输入所要进行的操作序号:  "); }  int main(void) {     Stack *s = NULL;     int num;     int ch;      s = create(); #if 0     while (1) {         menu();         scanf("%d", &ch);         switch (ch) {             case PUSH:                 printf("Please input a number: ");                 scanf("%d", &num);                 push(s, num);                 break;              case POP:                 pop(s, &num);                 printf("num = %d\n", num);                 break;              case EXIT:                 exit(0);                 break;              default:                 fprintf(stdout, "Error input!\n");                 break;         }     }  #else     while (scanf("%d", &num) && num > 0) {         push(s, num);     }     while (!is_empty(s)) {         pop(s, &num);         printf("num = %d\n", num);     } #endif     return 0; }  /* void create(Stack *S, int stacksize) {     S->base = (int*)malloc(stacksize*sizeof(int));     if(!S->base)         exit(0);     S->top = S->base;     S->size = stacksize; } void push(Stack *S, int elem) {     if(S->top - S->base >= S->size)     {         S->base=(int*)realloc(S->base,(S->size+10)*sizeof(int));         if(!S->base)             exit(0);         S->top = S->base + S->size;         S->size+=10;     }     *S->top++ = elem; } void pop(Stack*S) {     int popnum;     if(S->top == S->base) {         printf("栈空");         return;     }     popnum= *(--S->top);     printf("%d",popnum);  } void show(Stack*S) {     printf("The stack is:");     while(S->top!=S->base)     {         printf("%d  ",*S->top--);     }     printf("\n");  } int main() {     Stack SS;     //create(&SS,MAXSIZE);     int j;     do     {         printf("       -----------------------------------------------\n");      //显示一个简易菜单         printf("                       main     menu                  \n");         printf("                     1      ---     创建（Insert）     \n");         printf("                    2      ---     入栈（Update）     \n");         printf("                     3      ---     出栈（Delete）     \n");         printf("                     4      ---     退出（ Show ）     \n");         printf("        ---------------------------------------------\n");         printf("请输入所要进行的操作序号:  ");          scanf("%d",&j);         switch(j)         {             case 1:                 {                     create(&SS, MAXSIZE);                     printf("创建成功；");                     break;                 }             case 2:                 {                     printf("请输入要入栈的值:");                     int pushnum;                     scanf("%d",&pushnum);                     //printf("%d",popnum);                     push(&SS,pushnum);                     show(&SS);                     break;                 }             case 3:                 {                     pop(&SS);                     show(&SS);                     break;                 }             case 4:                 {                     break;                 }         }     } while(j!=4);      return 0; } */  另一种实现方法，建议去搜一下栈的类型，栈可以分为不同类型，基本上分为：空递增，满递增，空递减，满递减。 作为一个参考吧

user: user-3
content: 单步类的实例“构造”或“复制”或“作为函数参数”或“作为函数返回值返回”或“参加各种运算”或“退出作用域”的语句对应的汇编代码几步后，就会来到该类的“构造函数”或“复制构造函数”或“运算符重载”或“析构函数”对应的C/C++源代码处。  VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化，这样过一遍不就啥都明白了吗。 对VC来说，所谓‘调试时’就是编译连接通过以后，按F10或F11键单步执行一步以后的时候，或者在某行按F9设了断点后按F5执行停在该断点处的时候。

user: user-0
content: 初学者 ，请大家多赐教。。。