栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言实现链队列代码

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言实现链队列代码

本文实例为大家分享了C语言实现链队列的具体代码,供大家参考,具体内容如下

#include 


typedef int DataType;
#define NODE_LEN sizeof(NODE) 


typedef struct stNode
{
  DataType data;
  struct stNode* next;
}NODE;


typedef struct stQueue
{
  NODE* head; //队列的头
  NODE* tail; //队列的尾
}QUEUE;


int initQueue(QUEUE* INQueue)
{

  INQueue->head = NULL;
  INQueue->tail = NULL;

  return 0;
}


int enQueue(QUEUE* InQueue,DataType InData)
{
  NODE* pNewNode = (NODE*)malloc(NODE_LEN);
  if (pNewNode == NULL)
  {
    return -1;
  }

  pNewNode->data = InData;
  pNewNode->next = NULL;

  
  if (InQueue->head == NULL)
  {
    InQueue->head = pNewNode;
    InQueue->tail = pNewNode;
  }
  else
  {
    InQueue->tail->next = pNewNode;
    InQueue->tail = pNewNode;
  }

  return 0;
}


int visitQueue(QUEUE InQueue)
{
  QUEUE* pstTemp = &InQueue;

  
  if (pstTemp->head == NULL)
  {
    printf("visitQueue: this queue is emptyn");
    return -1;
  }

  
  while (pstTemp->head->next != NULL)
  {
    printf("%d ", pstTemp->head->data);
    pstTemp->head = pstTemp->head->next;
  }
  printf("%d n", pstTemp->head->data);

  return 0;
}


int delQueue(QUEUE* InQueue,DataType* OutData)
{
  if (InQueue->head == NULL)
  {
    printf("delQueue: this queue is emptyn");
    return -1;
  }

  *OutData = InQueue->head->data;

  NODE* pstTemp = InQueue->head;
  InQueue->head = InQueue->head->next;

  delete pstTemp;
  return 0;
}


int isEmptyQueue(QUEUE InQueue)
{
  if (InQueue.head == NULL)
  {
    return 0; //是空队列
  }
  return 1; //不是空队列
}

int main()
{
  
  QUEUE queue;
  DataType data;

  initQueue(&queue);

  
  enQueue(&queue, 12);
  enQueue(&queue, 11);
  enQueue(&queue, 2);
  visitQueue(queue);

  
  delQueue(&queue, &data);
  visitQueue(queue);
  printf("data = %dn", data);

  visitQueue(queue);

  if (0 == isEmptyQueue(queue))
  {
    printf("This is empty queuen");
  }
  else
  {
    printf("This is not empty queuen");
  }
  return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/62824.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号