当前位置: 代码迷 >> C语言 >> 急!求助!
  详细解决方案

急!求助!

热度:155   发布时间:2008-04-13 09:46:13.0
急!求助!
对栈 的操作,不知道错哪里!
#include<stdio.h>
#define maxsize 20
struct stack
{int data[maxsize];
  int top[2];
};
typedef struct stack STACK

void initstack(STACK *s)
{s->top[0]=-1;
s->top[1]=maxsize;
}

int push(STACK *s,int x,int k)
{if(s->top[0]==s->top[1])
   {printf("\nstack is full!");
    return 0;
   }
  if(k==0)
    {s->top[0]++;
     s->data[s->top[0]]=x;
    }
  else{s->top[1]--;
       s->data[s->top[1]]=x;
      }
return 1;
}

int pop(STACK *s,int k,int *x)
{if((k==0)&&(s->top[0]=-1))
   {printf("\nstack is free!");
    return 0;
   }
if((k==1)&&(s->top[1]=maxsize))
   {printf("\nstack is free!");
    return 0;
   }
if(k==0)
  {*x=s->data[s->top[0]];
   s->top[0]--;
  }
else{*x=s->data[s->top[1]];
      s->top[1]++;
     }
return 1;
}

void print(STACK *s)
{printf("The num of s1 are:\n");
while(s->top[0]!=-1)
{printf("%d ",s->data[s->top[0]]);
  s->top[0]++;
}
printf("\n");
printf("The num of s2 are:\n");
while(s->top[1]!=maxsize)
{printf("%d",s->data[s->top[1]]);
  s->top[1]--;
}
printf("\n");
}

void main()
{int i=0,x,k,*p;
STACK *s;
initstack(*s);
printf("input 8 num into s1:\n");
while(i<8)
{scanf("%d",&s->data[i]);
  s->top[0];
  i++;
}
printf("input 8 num into s2:\n");
while(i<8)
{scanf("%d",&s->data[maxsize-1-i]);
  s->top[1]--;
  i++;
}
print(*s);
printf("input the num pushed into s1:\n")
scanf("%d",&x);
k=0;
push(*s,x,k);
print(*s);
printf("now,delete the num of s2:\n");
k=1;
pop(*s,k);
print(*s);
}
----------------解决方案--------------------------------------------------------
程序代码:
#include<stdio.h>
#define maxsize 20
struct stack
{int data[maxsize];
  int top[2];
};
typedef struct stack STACK         //这里缺少了什么?

void initstack(STACK *s)
{s->top[0]=-1;
s->top[1]=maxsize;
}

int push(STACK *s,int x,int k)
{if(s->top[0]==s->top[1])
   {printf("\nstack is full!");
    return 0;
   }
  if(k==0)
    {s->top[0]++;
     s->data[s->top[0]]=x;
    }
  else{s->top[1]--;
       s->data[s->top[1]]=x;
      }
return 1;
}

int pop(STACK *s,int k,int *x)
{if((k==0)&&(s->top[0]=-1))       //等号问题;
   {printf("\nstack is free!");
    return 0;
   }
if((k==1)&&(s->top[1]=maxsize))  //等号问题;
   {printf("\nstack is free!");
    return 0;
   }
if(k==0)
  {*x=s->data[s->top[0]];
   s->top[0]--;
  }
else{*x=s->data[s->top[1]];
      s->top[1]++;
     }
return 1;
}

void print(STACK *s)
{printf("The num of s1 are:\n");
while(s->top[0]!=-1)
{printf("%d ",s->data[s->top[0]]);
  s->top[0]++;
}
printf("\n");
printf("The num of s2 are:\n");
while(s->top[1]!=maxsize)
{printf("%d",s->data[s->top[1]]);
  s->top[1]--;
}
printf("\n");
}

void main()
{int i=0,x,k,*p;     //p作用?
STACK *s;
initstack(*s);         //参数错误,并且指针未初始化;
printf("input 8 num into s1:\n");
while(i<8)
{scanf("%d",&s->data[i]);
  s->top[0];
  i++;
}
printf("input 8 num into s2:\n");
while(i<8)
{scanf("%d",&s->data[maxsize-1-i]);
  s->top[1]--;
  i++;
}
print(*s);         //参数错误;
printf("input the num pushed into s1:\n")    //分号?
scanf("%d",&x);
k=0;
push(*s,x,k);       //参数错误;
print(*s);         //参数错误;
printf("now,delete the num of s2:\n");
k=1;
pop(*s,k);       //参数错误;
print(*s);       //参数错误;
}

仅指出语法错误,建议LZ自己修改,才会有进步,。
----------------解决方案--------------------------------------------------------
回复 2# 的帖子
谢谢!我会自己解决剩下的问题!
----------------解决方案--------------------------------------------------------
  相关解决方案