C语言实现链队列的初始化&进队&出队

Code

/*链表实现队列的一系列操作*/ 

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

#define OK 1
#define ERROR 0

typedef struct node
{
    int data;                            //数据域 
    struct node *next;                  //指针域 
}LinkQueueNode;
typedef struct
{
    LinkQueueNode *front;              //头指针 
    LinkQueueNode *rear;               //尾指针 
}LinkQueue;

/**********************各个子函数的定义*********************/
void initQueue(LinkQueue *Q);          //链队列初始化       
int enterQueue(LinkQueue *Q,int n);    //链队列入队操作 
void deleteQueue(LinkQueue *Q);        //链队列出队操作 
 
int main()
{
    LinkQueue Q;
    int choice;
    while(true)
    {
        printf("*****************Please enter your choice*****************\n\n");
        printf("                choice 1:Queue initialization\n");
        printf("                choice 2:Into the queue\n");
        printf("                choice 3:Out of the queue\n");
        printf("                choice 0:exit\n\n");
         scanf("%d",&choice);
        switch(choice)
        {
            case 1:
                initQueue(&Q);
                break;
            case 2:
                int n;
                printf("Please enter the number into the queue elements:");
                scanf("%d",&n);
                (enterQueue(&Q,n)==1)?printf("%d个元素依次进队成功\n",n):printf("%d个元素依次进队失败\n",n);    
                break;
            case 3:
                deleteQueue(&Q);
                break;    
            case 0:
                exit(0);
                break;
            default:
                printf("ERROR!!\n");
                exit(0);
                break;
        }
    }
    return 0;
}

/**********************各个子函数功能的实现*********************/
void initQueue(LinkQueue *Q)   
{            //将 Q 初始化为一个空链队列 
    Q->front=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));
    Q->rear=Q->front;
    Q->front->next=NULL;
}

int enterQueue(LinkQueue *Q,int n)  //进队列 
{
    LinkQueueNode *temp;
    int n1,n2;
    printf("Please enter into the queue elements in turn:\n");  
    for(n1=0;n1<n;n1++)
    {
        scanf("%d",&n2);
        temp=(LinkQueueNode *)malloc(sizeof(LinkQueueNode));
        if(temp==NULL) return ERROR;  //申请空间失败
        temp->data=n2;
        temp->next=NULL;
        Q->rear->next=temp; 
        Q->rear=temp;                 //队尾指针后移                    
    }
    return OK;
}
 
void deleteQueue(LinkQueue *Q)
{
    int a;
    LinkQueueNode *temp;
    if(Q->front==Q->rear)               //队为空,出栈失败 
    {
        printf("An empty Queue error!!!!\n");
    }
    else
    {
        temp=Q->front->next;
        a=temp->data;
        Q->front->next=temp->next;
        if(Q->front==temp)           //如果队中只有一个元素X,则X出队后成为空队 
        {
            Q->rear=Q->front;
        }
        free(temp);                            //释放存储空间 
        printf("队顶元素%d出队成功.\n",a);   
    }  
}

转载于:https://www.cnblogs.com/qftm/p/10317153.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值