| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 373 人关注过本帖
标题:急!求助!
只看楼主 加入收藏
jinxilee
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-6
收藏
 问题点数:0 回复次数:2 
急!求助!
对栈 的操作,不知道错哪里!
#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);
}
2008-04-13 09:46
now
Rank: 1
来 自:广州
等 级:新手上路
帖 子:544
专家分:0
注 册:2007-11-9
收藏
得分: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;     //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自己修改,才会有进步,。

GIS
Geographic Information System
你在哪里?——》你的坐标?
2008-04-13 10:17
jinxilee
Rank: 1
等 级:新手上路
帖 子:41
专家分:0
注 册:2008-4-6
收藏
得分:0 
回复 2# 的帖子
谢谢!我会自己解决剩下的问题!
2008-04-13 10:40
快速回复:急!求助!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034943 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved