当前位置: 代码迷 >> C语言 >> 做了一天没做出来!关于栈的!
  详细解决方案

做了一天没做出来!关于栈的!

热度:169   发布时间:2008-04-14 13:11:51.0
做了一天没做出来!关于栈的!
帮忙看一下!怎么会出现那种结果呢?
#include<stdio.h>
#define maxsize 20
int Num_del;
struct stack
{int a[maxsize];
int top[3];
};

void initstack(struct stack s)
{s.top[1]=-1;
s.top[2]=maxsize;
}

int push(struct stack s,int x,int k)
{if(s.top[1]==s.top[2])
   {printf("\nstack is full!\n");
    return 0;
   }
else {if(k==1)
  {s.a[s.top[1]]=x;
   s.top[1]++;
  }
       else s.a[s.top[2]]=x;
    s.top[2]--;
      }
  return 1;
}

int pop(struct stack s,int k)
{if(k==1)
   {if(s.top[1]!=-1)
      {Num_del=s.a[s.top[1]--];
       return 1;
      }
    else printf("stack_1 can't be poped!\n");
      return 0;
   }
else  {if(s.top[2]!=maxsize)
         {Num_del=s.a[s.top[2]];
          s.top[2]++;
          return 1;
         }
        else printf("STACK_2 can't be poped!\n");
            return 0;
       }
}

void print(struct stack s)
{int i;
if(pop(s,1)==1)
  {printf("The nums of stack_1 are:\n");
   for(i=0;i<s.top[1];i++)
   printf("%d",s.a[s.top[1]]);
   s.top[1]++;
  }
else printf("stack_1 is empty!\n");
if(pop(s,2)==1)
{printf("the nums of stack_2 are:\n");
  for(i=s.top[2];i>0;i--)
  printf("%d",s.a[s.top[2]]);
  s.top[2]--;
}
else printf("stack_2 is empty!\n");
}

void main()
{struct stack s;
int x,k,i;
initstack(s);
printf("input which stack the nums will be pushed:");
scanf("%d",&k);
printf("input 8 nums into stack_%d:\n",k);
for(i=0;i<8;i++)
{if(k==1)
  {scanf("%d",s.a[i]);
   s.top[1]++;
  }
else {if(k==2)
      scanf("%d",s.a[maxsize-1-i]);
      s.top[2]--;
     }
}
print(s);
printf("input which stack the num will be pushed:");
scanf("%d",&k);
printf("input the num  to be pushed into stack_%d:\n",k);
scanf("%d",&x);
push(s,x,k);
print(s);
printf("input which stack the nums will be poped:");
scanf("%d",&k);
printf("input the num  to be poped out of stack_%d:\n",k);
print(s);
printf("%d",Num_del);
}
搜索更多相关的解决方案: int  stack  top  struct  else  

----------------解决方案--------------------------------------------------------
我只看了一点点,
scanf是用时后面的参数要加‘&’
----------------解决方案--------------------------------------------------------
加了“&”,还是一样 ,输出 很多 数字出来,不知道问题出哪里!
----------------解决方案--------------------------------------------------------
  相关解决方案