数据结构-单链表实现队列的基本操作

头文件

typedef char QueueType;
typedef struct QueueNode
{
      struct QueueNode* next;
      QueueType data;
}QueueNode;
//创建新节点
QueueNode* QueueCreat(QueueType value);
//初始化
void QueueInit(QueueNode** pphead);
//打印
void QueuePrint(QueueNode* phead,const char *s);
//入队
void QueuePush(QueueNode** pphead,QueueType value);
//出队列
void QueuePop(QueueNode** pphead);
//取队首元素
int QueueTop(QueueNode* phead,QueueType* value);
//销毁队列
void QueueDestory(QueueNode** pphead);

具体实现

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "linkqueue.h"
//创建节点
QueueNode* QueueCreat(QueueType value)
{
     //申请内存
      QueueNode* newnode=(QueueNode*)malloc(sizeof(QueueNode));
      assert(newnode);
      newnode->data=value;
      newnode->next=NULL;
      return newnode;


}  
//初始化
void QueueInit(QueueNode** pphead)
{
      assert(pphead);
      *pphead=NULL;
}



//打印
void QueuePrint(QueueNode* phead,const char *s)
{
      if(  phead==NULL)
      {
            return;
      }
      printf("%s\n",s);
      //定义一个节点,遍历队列打印
      QueueNode *cur=phead;
      while( cur)
      {
            printf("%c->",cur->data);
            cur=cur->next;
      }
      printf("NULL\n");

}

这里写图片描述


入队列
void QueuePush(QueueNode** pphead,QueueType value)
{
     //非法输入
    if(  pphead==NULL)
    {
          return ;
    }
    //队列为空,直接构造节点,插入队列
    else if(  *pphead==NULL)
    {
          *pphead=QueueCreat(value);

    }
    //正常情况,找到队尾,申请新节点,插入队尾
    else
    {
          QueueNode* cur=*pphead;
          while(cur->next!=NULL)
          {
                cur=cur->next;
          }
          cur->next=QueueCreat(value);
    }

}

这里写图片描述


//出队列
void QueuePop(QueueNode** pphead)
{
     //非法输入
    if(  pphead==NULL)
    {
          return ;
    }
    //空队列无法完成出队列操作
    else if(  *pphead==NULL)
    {
          printf("queue is empty\n");
          return;
    } 
    //非空队列,更新队首节点
    else
    {

          QueueNode* cur=(*pphead)->next;
          free(*pphead);

          *pphead=cur;
    }


}

这里写图片描述


//取队首元素,成功返回1,失败返回0
int QueueTop(QueueNode* phead,QueueType* value)
{
      if(phead==NULL)
      {
            return 0 ;

      }
      //队首元素值由value带回
       *value=(phead->data);

      return 1;
}

这里写图片描述


//销毁队列
void QueueDestory(QueueNode** pphead)
{
      if(  pphead==NULL)
      {
            return ;
      }
      //遍历队列,依次销毁
      QueueNode* cur=*pphead;
      for(  ;cur!=NULL;cur=cur->next)
      {


            cur=next;
      }
      //将头结点置为空
      *pphead=NULL;
}
(由于bug还没有修复,销毁没有得出结果,后续更新)






由*value=phead->data引发的错误


- linkqueue.c:170: warning: initialization makes integer from pointer without a cast
linkqueue。c:170:警告:初始化使整数从指针变为整数。

定义char value时将value初始化成NULL


- linkqueue.c:92: error: subscripted value is neither array nor pointer
linkqueuec:92:错误:下标值既不是数组也不是指针。linkqueuec:92:错误:下标值既不是数组也不是指针。

错误引用value,把data当成指针类型了
*value=phead->data[0]


- linkqueue.c:92: warning: assignment makes pointer from integer without a cast
linkqueue。c:92:警告:赋值使指针从整数开始,而不需要转换。

value=phead->data
“`

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值