注册 登录
编程论坛 数据结构与算法

利用顺序结构解决队列问题,不知道哪里出错了,请指教

dengdaisky 发布于 2013-05-18 10:55, 643 次点击
利用顺序结构解决队列问题,不知道哪里出错了,请指教
我不懂就是如果我用for循环输出15个数字,不会出错。
但是我只输出一个数字,比如说4。那么则输出4 0 0 0 0.
而且出队列根本没用,这是为什么?
难道输出问题?
// 顺序队列.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
#define  MaxSize 50
typedef struct
{
    int data[MaxSize];
    int front;int rear;
}Sqeuque;
void init(Sqeuque *&l)
{
    l=new Sqeuque();
    l->front=l->rear=-1;
}
bool Enter(Sqeuque *&l,int e)
{
    if(l->rear==MaxSize-1)
        return false;
    else
        l->rear++;
    l->data[l->rear]=e;
    return true;
}
bool Out(Sqeuque *&l,int &e)
{
    if (l->front==l->rear)
      return false;
    else
        l->front++;
    e=l->data[l->front];
    return true;

}
void display(Sqeuque *l)
{
    int i;
    for (i=0;i<=l->data[l->rear];i++)
     cout<<l->data[i]<<"\t";

}
int _tmain(int argc, _TCHAR* argv[])
{
    Sqeuque *l;
    init(l);
    //for(int i=0;i<15;i++)
    Enter(l,4);
    Enter(l,5);
    display(l);
    cout<<"元素出栈:"<<endl;
    int x;
    Out(l,x);
    display(l);
    system("pause");
    return 0;
}
3 回复
#2
azzbcc2013-05-18 13:01
void display(Sqeuque *l)
{
    int i;
    for (i = l->front+1; i <= l->rear;i++)
         cout<<l->data[i]<<"\t";

}
#3
dengdaisky2013-05-18 14:46
回复 2楼 azzbcc
麻烦能不能说一下为什么不,我还是有点不理解为什么要这么写?
#4
邓士林2013-05-19 20:44
循环的条件啊
1