数据结构中有关队列的链式及顺序存储使用问题

首先我在main中简单写了几行简单的代码验证代码可行,但是当我给整个程序加了一个操作菜单后频频出现0xC0000005的报错,后来发现错误出现在Q=InitQueue();及Sq=InitQueueSq();这两行代码,实在想不明白为什么加了个操作菜单就出现问题了(使用Code::Blocks编写)最底下注释是那几行简单的验证代码
感谢解答

 #include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAXQSIZE 100

typedef int ElementType;

//链式存储类型定义
typedef struct Node *PtrToNode;
struct Node
{
    ElementType Data;
    PtrToNode Next;
};
typedef PtrToNode QNode;
typedef struct
{
    PtrToNode Head;
    PtrToNode Rear;
}*Queue;


//顺序存储类型定义
typedef struct
{
    ElementType *Base;
    int Head;
    int Rear;
}*SqQueue;

//链式存储部分函数
Queue InitQueue();
void PrintQueue(Queue Q);
void EnQueue(Queue Q,ElementType e);
ElementType DeQueue(Queue Q);

//顺序存储部分函数
SqQueue InitQueue_Sq();
int EnQueue_Sq(SqQueue Sq,ElementType e);
ElementType DeQueue_Sq(SqQueue Sq);
void PrintQueue_Sq(SqQueue Sq);


int main()
{
    int a,b,c;
    ElementType x;
    Queue Q=NULL;
    SqQueue Sq;
    printf("******************************************************\n");
    printf("***     请选择要进行的操作,输入数字后按回车键     ***\n");
    printf("******************************************************\n");
    printf("\n");
    printf("  1 队列的链式存储应用\n");
    printf("  2 队列的顺序存储应用\n");
    printf("\n");
    printf("*****************0 结束操作并退出*********************\n");
    printf("\n");
    scanf("%d",&a);

    while(a)
    {
        switch(a)
        {
        case 1:
            Q=InitQueue();
            printf("\n");
            printf("***********链式队列已成功初始化,请继续选择操作************\n");
            printf("\n");
            printf("  1 入队\n");
            printf("  2 出队\n");
            printf("  3 重置队列\n");
            printf("\n");
            printf("*********0 回到主菜单*********-1 结束操作退出系统*********\n");
            printf("\n");
            scanf("%d",&b);

            while(b)
            {
                switch(b)
                {
                case 1:
                    printf("请输入需要入队的整数\n");
                    printf("\n");
                    scanf("%d",&x);
                    printf("\n");
                    EnQueue(Q,x);
                    printf("\n");
                    printf("更新后的队列为\n");
                    PrintQueue(Q);
                    printf("\n");
                    break;
                case 2:
                    printf("出队元素为:%d\n",DeQueue(Q));
                    printf("\n");
                    printf("更新后的队列为\n");
                    PrintQueue(Q);
                    printf("\n");
                    break;
                case 3:

                    printf("\n");
                    printf("队列已重置\n");
                    printf("\n");
                    break;
                case -1:exit(0);break;
                default:printf("输入错误\n");break;
                }
                printf("***请继续选择操作***\n");
                scanf("%d",&b);
        }
        break;
        case 2:
            Sq=InitQueue_Sq();
            printf("\n");
            printf("***********顺序循环队列已成功初始化,请继续选择操作************\n");
            printf("\n");
            printf("  1 入队\n");
            printf("  2 出队\n");
            printf("  3 重置队列\n");
            printf("\n");
            printf("********0 回到主菜单********-1 结束操作退出系统********\n");
            printf("\n");
            scanf("%d",&c);
            while(c)
            {
                switch(c)
                {
                case 1:
                    printf("请输入需要入队的整数\n");
                    printf("\n");
                    scanf("%d",&x);
                    printf("\n");
                    EnQueue_Sq(Sq,x);
                    printf("\n");
                    printf("更新后的队列为\n");
                    PrintQueue_Sq(Sq);
                    printf("\n");
                    break;
                case 2:
                    printf("出队元素为:%d\n",DeQueue_Sq(Sq));
                    printf("\n");
                    printf("更新后的栈为\n");
                    PrintQueue_Sq(Sq);
                    printf("\n");
                    break;
                case 3:
                    Sq=InitQueue_Sq();
                    printf("\n");
                    printf("队列已重置\n");
                    printf("\n");
                    break;
                case -1:exit(0);break;
                default:printf("输入错误\n");break;
                }
                printf("***请继续选择操作***\n");
                scanf("%d",&c);
            }break;
        default:printf("输入错误\n");break;
        }
        printf("******************************************************\n");
        printf("***     请选择要进行的操作,输入数字后按回车键     ***\n");
        printf("******************************************************\n");
        printf("\n");
        printf("  1 队列的链式存储应用\n");
        printf("  2 队列的顺序存储应用\n");
        printf("\n");
        printf("*****************0 结束操作并退出*********************\n");
        printf("\n");
        scanf("%d",&a);
    }
    return 0;
}

//链式存储部分函数
Queue InitQueue()
{
    Queue Q;
    Q->Head=Q->Rear=(QNode)malloc(sizeof(QNode));
    if(!Q->Head) return NULL;
    Q->Head->Next=NULL;
    return Q;
}


void PrintQueue(Queue Q)//队头为头结点的下一个结点
{
    QNode p;
    p=Q->Head->Next;
    if(Q->Head->Next == NULL)
    {
        printf("队列为空\n");return;
    }
    printf("队头<-");
    while(p)
    {
       printf("%d<-",p->Data);
       p=p->Next;
    }
    printf("队尾\n");

}


void EnQueue(Queue Q,ElementType e)
{
    QNode temp;
    temp=(QNode)malloc(sizeof(QNode));
    temp->Next=NULL;
    temp->Data=e;
    Q->Rear->Next=temp;
    Q->Rear=temp;
}

ElementType DeQueue(Queue Q)
{
    QNode temp;
    ElementType e;
    if(Q->Head->Next == NULL)
        return 0;
    temp=Q->Head->Next;
    e=temp->Data;
    Q->Head->Next=temp->Next;
    free(temp);
    return e;
}

//顺序存储部分函数

SqQueue InitQueue_Sq()
{
    SqQueue Sq;
    Sq->Base=(ElementType*)malloc(MAXQSIZE*sizeof(ElementType));
    if(!Sq->Base) return NULL;
    Sq->Head=Sq->Rear=0;
    return Sq;
}

int EnQueue_Sq(SqQueue Sq,ElementType e)
{
    if((Sq->Rear+1)%MAXQSIZE == Sq->Head)
        return 0;
    Sq->Base[Sq->Rear]=e;
    Sq->Rear=(Sq->Rear+1)%MAXQSIZE;
    return 1;
}

ElementType DeQueue_Sq(SqQueue Sq)
{
    ElementType e;
    if(Sq->Head == Sq->Rear)
    {
        printf("队列为空\n");return 0;
    }
    e=Sq->Base[Sq->Head];
    Sq->Head=(Sq->Head+1)%MAXQSIZE;
    return e;
}

void PrintQueue_Sq(SqQueue Sq)
{
    int i=Sq->Head;
    if(Sq->Head == Sq->Rear)
    {
        printf("队列为空\n");return;
    }
    printf("队头<-");
    while(i != Sq->Rear)
    {
        if(i<0)
            i=i+MAXQSIZE;
        printf(" %d <-",Sq->Base[i]);i++;
    }
    printf("队尾\n");
}

 /*
    SqQueue Sq;
    Sq=InitQueue_Sq();
    EnQueue_Sq(Sq,1);PrintQueue_Sq(Sq);
    EnQueue_Sq(Sq,2);PrintQueue_Sq(Sq);
    EnQueue_Sq(Sq,3);PrintQueue_Sq(Sq);
    DeQueue_Sq(Sq);PrintQueue_Sq(Sq);
    EnQueue_Sq(Sq,4);PrintQueue_Sq(Sq);
    DeQueue_Sq(Sq);PrintQueue_Sq(Sq);
    EnQueue_Sq(Sq,5);PrintQueue_Sq(Sq);
    DeQueue_Sq(Sq);PrintQueue_Sq(Sq);
        DeQueue_Sq(Sq);PrintQueue_Sq(Sq);
        DeQueue_Sq(Sq);PrintQueue_Sq(Sq);

        //链式
    Queue Q;//带头结点
    Q=InitQueue();
    EnQueue(Q,1);PrintQueue(Q);
    EnQueue(Q,2);PrintQueue(Q);
    EnQueue(Q,3);PrintQueue(Q);
    EnQueue(Q,4);PrintQueue(Q);
    DeQueue(Q);PrintQueue(Q);
    EnQueue(Q,5);PrintQueue(Q);
    DeQueue(Q);PrintQueue(Q);
    DeQueue(Q);PrintQueue(Q);
    DeQueue(Q);PrintQueue(Q);
    DeQueue(Q);PrintQueue(Q);
*/

3个回答

不能返回堆栈上的局部变量,因为用指针。

每个scanf语句之后要加一个getchar()函数来消除多余的字符,也就是回车符。
否则下次执行scanf时会读取这个多余的回车符。

如果没有解决问题欢迎继续回复我。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问