单循环链表表示队列,出队操作会出错

#include "stdio.h"
#include "malloc.h"
typedef int DataType;
typedef struct node
{
    DataType data;
    struct node *next;
}linklistq;
typedef struct
{
    linklistq *rear,*front;
}linkqueue;
linkqueue *Initqueue()
{
    linklistq *p = (linklistq *)malloc(sizeof(linklistq));
    linkqueue *q = (linkqueue *)malloc(sizeof(linkqueue));
    q->front = p;
    q->rear = p;
    return q;
}
int emptyqueue(linkqueue *q)
{
    if(q->rear = q->front){return 1;}
    else{return 0;}
}
int inqueue (linkqueue *q,DataType x)
{
    linklistq *p =  (linklistq *)malloc(sizeof(linklistq));
    p->data = x;
    p->next = NULL;
    q->rear->next = p;
    q->rear = p;
}
int dequeue(linkqueue *q,DataType *x)
{
    linklistq *p;
    if(emptyqueue(q))
    {
        printf("队空,不能出队\n");
        return 0;
    }
    else 
    {
        p = q->front->next;
        *x = p->data;
        q->front->next = p->next;
        if(p->next == NULL)
        {
            q->rear = q->front;
        }
        free(p);
        return 1;
    }
}
void showqueue(linkqueue *q)
{
    
    linklistq *p = q->front->next;//有头指针 
    if(p == NULL)
    {
        printf("队列为空");
    }
    else
    {
        printf("队列中的元素依次为:");
        while(p != NULL)
        {
            printf("%5d",p->data);
            p = p->next;
        }
    }
}
int menuqueue()
{  int n;
       printf("\n               队列子系统");
       printf("\n==========================================");
       printf("\n |          1-初始化队列                 |");
       printf("\n |          2-入队操作                   |");
       printf("\n |          3-出队操作                   |");
       printf("\n |          4-遍历操作                   |");
       printf("\n |          0-退出                       |"); 
       do
       { printf("\n\t\t please number 0---4 select:");
          scanf("%d",&n);
       } while((n<0)||(n>4));
       return n;
}
int main()
{
    DataType x;
    linkqueue *q;
    int i,n,flag;
    int kk;
    do
    {
        kk = menuqueue();
        switch(kk)
        {
            case 1:
                q = Initqueue();
                printf("队列的初始化完成!");
                break;
            case 2:
                printf("请输入要入队的元素个数:");
                scanf("%d",&n);
                printf("请输入%d个整数进行入队:",n);
                for(i = 0;iscanf("%d",&x);
                    inqueue(q,x);
                }
                printf("入队操作完成!");
                break;
            case 3:
                printf("请输入要出队的元素个数:");
                scanf("%d",&n);
                printf("出队的元素顺序依次为:");
                for(i = 0;idequeue(q,&x);
                    printf("%5d",x);
                }
                if(flag == 1){printf("出队成功!");}
                else
                {
                printf("出队失败!");
                }
                break;
            case 4:
                showqueue(q);
                break;
            case 0:
                printf("退出");
                break;
            }
        }while(kk != 0);
}

img

#include "stdio.h"
#include "malloc.h"
typedef int DataType;
typedef struct node
{
    DataType data;
    struct node *next;
}linklistq;
typedef struct
{
    linklistq *rear,*front;
}linkqueue;
linkqueue *Initqueue()
{
    linklistq *p = (linklistq *)malloc(sizeof(linklistq));
    linkqueue *q = (linkqueue *)malloc(sizeof(linkqueue));
    q->front = p;
    q->rear = p;
    return q;
}
int emptyqueue(linkqueue *q)
{
    if(q->rear == q->front){return 1;}
    else{return 0;}
}
int inqueue (linkqueue *q,DataType x)
{
    linklistq *p =  (linklistq *)malloc(sizeof(linklistq));
    p->data = x;
    p->next = NULL;
    q->rear->next = p;
    q->rear = p;
}
int dequeue(linkqueue *q, int n)
{
    linklistq *p;
    if(emptyqueue(q))
    {
        printf("队空,不能出队\n");
        return 0;
    }
    else 
    {
        int count = 0;
        printf("出队的元素顺序依次为:");
        while (count < n)
        {
            p = q->front->next;
            if (p != NULL)
            {
                printf("%5d",p->data);
                count++;
            }
            q->front->next = p->next;
            if(p->next == NULL)
            {
                q->rear = q->front;
            }
        }
        free(p);
        return 1;
    }
}
void showqueue(linkqueue *q)
{
    
    linklistq *p = q->front->next;//有头指针 
    if(p == NULL)
    {
        printf("队列为空");
    }
    else
    {
        printf("队列中的元素依次为:");
        while(p != NULL)
        {
            printf("%5d",p->data);
            p = p->next;
        }
    }
}
int menuqueue()
{  int n;
       printf("\n               队列子系统");
       printf("\n==========================================");
       printf("\n |          1-初始化队列                 |");
       printf("\n |          2-入队操作                   |");
       printf("\n |          3-出队操作                   |");
       printf("\n |          4-遍历操作                   |");
       printf("\n |          0-退出                       |"); 
       do
       { printf("\n\t\t please number 0---4 select:");
          scanf("%d",&n);
       } while((n<0)||(n>4));
       return n;
}
int main()
{
    DataType x;
    linkqueue *q;
    int i,n,flag;
    int kk;
    do
    {
        kk = menuqueue();
        switch(kk)
        {
            case 1:
                q = Initqueue();
                printf("队列的初始化完成!");
                break;
            case 2:
                printf("请输入要入队的元素个数:");
                scanf("%d",&n);
                printf("请输入%d个整数进行入队:",n);
                for(i = 0;i<n;i++){
                    scanf("%d",&x);
                    inqueue(q,x);
                }
                printf("入队操作完成!");
                break;
            case 3:
                printf("请输入要出队的元素个数:");
                scanf("%d",&n);
                flag = dequeue(q, n);
//                printf("出队的元素顺序依次为:");
//                for(i = 0;i<n;i++);
//                {
//                    flag = dequeue(q);
//                    printf("%5d",x);
//                }
                if(flag == 1){printf("出队成功!");}
                else
                {
                printf("出队失败!");
                }
                break;
            case 4:
                showqueue(q);
                break;
            case 0:
                printf("退出");
                break;
            }
        }while(kk != 0);
}