| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1476 人关注过本帖
标题:出栈进栈的简单操作!求帮忙改一下我的程序!
只看楼主 加入收藏
jj369258
Rank: 4
等 级:业余侠客
帖 子:116
专家分:226
注 册:2010-12-2
结帖率:69.57%
收藏
已结贴  问题点数:5 回复次数:3 
出栈进栈的简单操作!求帮忙改一下我的程序!
题目要求:(栈和队列)
输入输出要求如:
输入:5
输入(五个数):4 9 6 8 7
输入:3
输出的则是除了前面3个数之外的数: 8 7
如果输入:2
输出的则是除了前面2个数之外的数:6 8 7
如果输出的数大于或者等于5
则输出:无打印作业!
程序:
#include<iostream.h>
#include<conio.h>
#define Max 100
typedef int ElemType ;
typedef struct
{
 ElemType data[Max];
 int front;
 int rear;
}SQUEUE;
void InitQueue(SQUEUE *SQ)//初始化
{
 SQ->front=0;
 SQ->rear=0;
}
int EnQueue(SQUEUE *SQ,ElemType x)//进栈
{
 if((SQ->rear+1)%Max==SQ->front)
  return 0;
 SQ->rear=(SQ->rear+1)%Max;
 SQ->data[SQ->rear]=x;
 return 1;
}
int Empty(SQUEUE *SQ)//判断是否为空
{
 return(SQ->front==SQ->rear)?1:0;
}
int OutQueue(SQUEUE *SQ)//出栈
{
 if(Empty(SQ))
  return NULL;
 SQ->front=(SQ->front+1)%Max;
}
void main()
{
 int t,n,m,i,j=0;
 cin>>t;
 for(i=0;i<t;i++)
 {
  SQUEUE qu;
  ElemType work;
  cin>>n;
  cout<<endl;
  while(j!=n-1)
  {
   InitQueue(&qu);
      EnQueue(&qu);
   j++;
  }
  cout<<endl;
  cin>>m;
  cout<<endl;
  if(m>=n)
   cout<<"无打印作业!"<<endl;
  else
  {
   while(!Empty(&qu))
   {
   work=OutQueue(&qu);
   cout<<work<<" ";
   }
   getch();
  }
   }
}
 
搜索更多相关主题的帖子: include 
2011-10-13 12:58
ksws0191053
Rank: 2
等 级:论坛游民
帖 子:30
专家分:32
注 册:2010-12-3
收藏
得分:5 
你确定这是栈不是队列?我看了老半天,主函数一堆错误,每次循环都初始化一次队列,循环乱七八糟。个人建议,推到重写吧。挺简单的一个程序,修改反而麻烦。
2011-10-15 15:53
leizisdu
Rank: 2
等 级:论坛游民
帖 子:22
专家分:24
注 册:2011-10-17
收藏
得分:0 
楼主:
    1. 您的入栈函数中,SQ->data[SQ->rear]=x;应在SQ->rear=(SQ->rear+1)%Max;前面啊;
    2. 为了便于出栈数据的传递,出栈函数可以再加个参数,从而出栈函数原型:int OutQueue(SQUEUE *SQ, ElemType *x);当然也可以不用出栈函数。
其他的栈操作函数我认为都是对的。另外,楼主,您的main函数怎么写的,很明显,队列变量qu的位置不对啊。
修改后的代码如下,请您测试,看看是否能满足要求:
程序代码:
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

#define Max 100
typedef int ElemType ;
typedef struct
{
   ElemType data[Max];
   int front;
   int rear;
} SQUEUE;

void InitQueue(SQUEUE *SQ)          //初始化
{
   SQ->front = 0;
   SQ->rear = 0;
}
int EnQueue(SQUEUE *SQ,ElemType x)  //进栈
{
   if((SQ->rear + 1)%Max == SQ->front)
   {
      return 0;
   }
   SQ->data[SQ->rear] = x;          // 此句在先! SLL, 2011-10-24
   SQ->rear = (SQ->rear + 1)%Max;
   return 1;
}
int Empty(SQUEUE *SQ)               //判断是否为空
{
   return (SQ->front == SQ->rear)? 1: 0;
}
int OutQueue(SQUEUE *SQ, ElemType *x)//出栈
{
   if(Empty(SQ))
   {
      return 0;
   }
   *x = SQ->data[SQ->front];
   SQ->front = (SQ->front + 1)%Max;
   return 1;
}
int main()
{
   int count = 0; // 数据个数
   int i = 0;     // 循环变量
   int head = 0;  // 用户指定的起始
   ElemType num = 0; // 具体数据

   SQUEUE queue;
   InitQueue(&queue);

   cout << "你要输入几个数? ";
   cin >> count;
   cout << "请输入这" << count << "个数: " << endl;
   for (i=0; i<count; i++)
   {
      cin >> num;
      if (!EnQueue(&queue, num))
      {
         cout << "队列满!" << endl;
         exit(1);
      }
   }

   cout << "请输入起始位置(从0开始算起): ";
   cin >> head;
   if (head >= count)
   {
      cout << "无打印作业!\n";
   }
   else
   {
      queue.front = head;
      while (OutQueue(&queue, &num))
      {
         cout << num << ' ';
      }
      cout << endl;
   }
   return 0;
}
2011-10-24 17:32
凤儿飘飘
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2015-4-11
收藏
得分:0 
回复 3楼 leizisdu
其实我没看懂这程序,能说话这程序的设计理念么,简单一点说,慢慢说,一句一句解释,拜托了
2015-04-12 13:10
快速回复:出栈进栈的简单操作!求帮忙改一下我的程序!
数据加载中...
 
   



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

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