当前位置: 代码迷 >> C语言 >> 关于栈中的 realloc()的问题
  详细解决方案

关于栈中的 realloc()的问题

热度:532   发布时间:2006-09-28 13:01:06.0
关于栈中的 realloc()的问题

#include <string.h >
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#define len 5
#define t 5
typedef struct stack { // 栈的结构
int *base ;
int *top ;
int stacksize ;
}stack;
struct stack liststack(stack newstack ) // 栈的初始化函数

{

newstack.base=(int *)malloc(sizeof(int ));
if (!newstack.base) exit (1);
newstack.top=newstack.base;
//scanf ("%d",newstack.stacksize);
newstack.stacksize=len;
return(newstack);
}

void display (stack newstack) // 输出函数
{ int *p;
p=newstack.base;
if (newstack.top-newstack.base==0)
{ printf ("zhu shi kong de \n");}
else
{ printf ("shu chu zhan :\n");
for(int i=0;i<newstack.top-newstack.base ;i++)
{
printf ("%d-->",*p);
p++;
}
}
printf ("\n");
}

stack push(stack newstack , int e) //进栈函数
{
static int count=0;
if (newstack.top-newstack.base>=newstack.stacksize )
{ printf (" \nzhuan shi man de:\n");
newstack.base=(int *)realloc (newstack.base ,( newstack.stacksize + t ) * sizeof (int )); //此句有错,那位大哥能指 正
if (!newstack.base) exit (1);
newstack.top=newstack.base + newstack.stacksize;
newstack.stacksize=newstack.stacksize+ t;

}
else{ *(newstack.top)=e;

count++;
printf ("\nshu chu ge shu ");
printf ("%d",count);
printf ("shu chu zhi ");
printf ("%d",*newstack.top);

( newstack.top) ++;}

return newstack;
}

stack pop (stack newstack ,int e);
stack pop (stack newstack ,int e) // 出栈函数
{
if (newstack.base==newstack.top)
{
printf("zhu shi kong de :");
exit (1);
}
newstack.top--;
e=*newstack.top;
printf ("zhan ding de zhi shi : %d\n",e);
return newstack;
}

void main()
{int e;
stack q ,newstack;
newstack=liststack(newstack);
q=push(newstack,1);
q=push(q,2);
q=push(q,3);
q=push(q,4);
q=push(q,5);
q=push(q,6);
q=push(q,7);
display(q);
q=pop( q,e );
q=pop( q,e );
q=pop( q,e );
q=pop( q,e );
display(q);
}

搜索更多相关的解决方案: realloc  

----------------解决方案--------------------------------------------------------
我知道错在那了, 谢谢各位关注
----------------解决方案--------------------------------------------------------