2-22练习题

本文介绍了链式队列的数据结构及其在C语言中的实现,包括创建链表队列、尾进头出操作、判断队列是否为空以及遍历队列的方法。
摘要由CSDN通过智能技术生成

一、链式队列进队、出队、遍历、判空

1、02_linkqueue.c

#include <stdio.h>
#include <stdlib.h>
#include "./02_linkqueue.h"
linkPos* create_linkqueue(void)
{
    linkPos* pos = (linkPos*)malloc(sizeof(linkPos));
    //pos指向的堆空间中包含linkqueue空间的front和rear两部分,
    pos->front = (linkqueue*)malloc(sizeof(linkqueue));                                                        
    if(pos->front == NULL)
    {
        printf("创建链式队列失败\n");
        return NULL;
    }
    (pos->front)->text.len = 0;
    (pos->front)->next = NULL;
    pos->rear = pos->front;

    return pos;
}
//尾进头删
void push_linkQueue(linkPos* pos,dataType num)
{
    linkqueue* temp = (linkqueue*)malloc(sizeof(linkqueue));
    if(NULL == temp)
    {
        printf("创建新节点失败\n");
        return;
    }
    temp->text.data = num;
    temp->next = NULL;

    temp->next = (pos->rear)->next;
    (pos->rear)->next = temp;

    pos->rear = (pos->rear)->next;
    (pos->front)->text.len++;
}
//判空
int Empty(linkPos* pos)
{
    if(pos->front == pos->rear)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
//(头出)出队
dataType pop_linkQueue(linkPos* pos)
{
    if(Empty(pos) == 1)
    {
        printf("此队列已空,无法进行删除操作\n");
        return (dataType)-1;
    }
    linkqueue* temp;
    dataType n;

    temp = pos->front->next;
    pos->front->next = pos->front->next->next;
    n = temp->text.data;

    free(temp);
    temp = NULL;
    pos->front->text.len--;
    return n;
}


//遍历队列
void show_linkQueue(linkPos* pos)
{
    while(pos->front->next != NULL)
    {
        pos->front = pos->front->next;

        printf("%d ",pos->front->text.data);
    }
    printf("\n");
}

                                                                                                               
                                                                                                               
                                                                                                               
                                                                                                               
                                                                                                               
                                                                                                               
                                                                                                               

2、02_linkqueue.h

#ifndef __LINKQUEUE_H__                                                                                  
#define __LINKQUEUE_H__                                                                                  
typedef int dataType;      //由于实际应用中数据不一定是整型,所以重名方便操作                            
union msg{                                                                                               
    dataType data;        //有效数据结点中的数据使用                                                     
    int len;             //头结点中的数据使用                                                            
};                                                                                                       
                                                                                                         
typedef struct node{     //由于结点中的指针域要指向下一个结点,所以必须用有名结构体                      
    union msg text;     //数据域.若是头结点,则使用text里面的len.若是有效数据结点,就使用text里面的data  
    struct node* next;  //指针域,存储下一个结点的地址                                                   
}linkqueue;                                                                                              
//因为front和rear都需要返回,而return只能返回一个值,                                                    
//所以将front和rear打包在一起,方便封装函数返回                                                          
typedef struct                                                                                           
{                                                                                                        
    linkqueue* front;                                                                                    
    linkqueue* rear;                                                                                     
}linkPos;                                                                                                
                                                                                                         
linkPos* create_linkqueue(void);                                                                         
//尾进头删                                                                                               
void push_linkQueue(linkPos* pos,dataType num);                                                          
//判空                                                                                                   
int Empty(linkPos* pos);                                                                                 
                                                                                                         
//(尾出)出队                                                                                             
dataType pop_linkQueue(linkPos* pos);                                                                    
//遍历队列                                                                                               
void show_linkQueue(linkPos* pos);                                                                       
                                                                                                         
#endif                                                                                                   
                                                                                                         

3、02_main.c

#include <stdio.h>                           
#include "./02_linkqueue.h"                  
int main(int argc, const char *argv[])       
{                                            
    linkPos* pos = create_linkqueue();       
                                             
    push_linkQueue(pos,111);                 
    push_linkQueue(pos,222);                 
    push_linkQueue(pos,333);                 
    int num = pop_linkQueue(pos);            
    printf("%d\n",num);                      
    show_linkQueue(pos);                     
                                             
                                             
    return 0;                                
}                                            
                                             

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值