首页 > 其他分享 >2008秋季-计算机软件基础-循环链队列

2008秋季-计算机软件基础-循环链队列

时间:2023-11-08 10:03:40浏览次数:26  
标签:node head 计算机软件 struct head1 队列 next 2008 queueNode

/*---------------------------------------------------------

 Title: Link Queue(链队列) 链队列-链式存储结构的队列 

  请先阅读教材74-77页, 2.4.1-2.4.4节, 队列的定义及基本运算

 (注意:以下程序为简化后的,仅供入门学习之用)

----------------------------------------------------------*/

#include<stdio.h>

#include<stdlib.h>
//定义队列的结构
struct queueNode

{

   int data;//存放数据元素
   struct queueNode * next;//指针,指向下一个结点
};
struct queue

{

    struct queueNode * front;

    struct queueNode * rear;

};

//初始化队列
struct queue * InitialQueue()

{

 struct queue * head;

 struct queueNode * node;

 node=(struct queueNode *)malloc(sizeof(struct queueNode ));

 head=(struct queue *)malloc(sizeof(struct queue ));

 node->next=NULL;

 head->front=node;

 head->rear=node;

 return head;

}

//入队列
void EnterIntoQueue(struct queue * head, int value)

{

    struct queueNode * node;

    node=(struct queueNode *)malloc(sizeof(struct queueNode ));

    node->data=value;

    node->next=NULL;

    head->rear->next=node;

    head->rear=node;

    

 }

//出队列
void DeleteFromQueue(struct queue * head)

{

    struct queueNode * node;

 if(head->front==head->rear)

 {

     printf("Queue is empty, Delete failed\n");

 }

 else

    {

    node=head->front->next;

    head->front->next=node->next;

    free(node);

    // when there is only one element, the following is necessary.
     if(head->front->next==NULL)

     head->rear=head->front;

    }

}

//显示队列中所有元素
void ShowAllElements(struct queue * head)

{

 struct queueNode * node;

 printf("\n Show all elements: \n");

 node=head->front->next;

 while(node!=NULL)

 {

  printf(" %d ",node->data);

  node=node->next;

 }

}

void main()

{

    struct queue * head1;

    head1=InitialQueue();


    ShowAllElements(head1);

    EnterIntoQueue(head1,11);

    ShowAllElements(head1);

    EnterIntoQueue(head1,22);

    ShowAllElements(head1);

    DeleteFromQueue(head1);

    ShowAllElements(head1);

}

标签:node,head,计算机软件,struct,head1,队列,next,2008,queueNode
From: https://blog.51cto.com/emanlee/8245245

相关文章

  • 2008秋-计算机软件基础-第三章- 二叉排序树
    /*---------------------------------------------------------Title:二叉排序树(BinarySortingTree)请先阅读教材91-93,96-99页,3.2.3,3.2.7节,(注意以下程序为简化后的,仅供入门学习之用)----------------------------------------------------------*/#includ......
  • 【面试题】消息队列面试题总结(RocketMQ版)
    自己整理、总结了一些消息队列相关面试题,并想了一些RocketMQ面试过程中可能会问的知识点。使用消息队列的优点系统解耦比如系统A产生的某个事件,系统B需要感知,简单实现就是在系统A产生事件之后,调用系统B的接口通知系统B,如果此时再增加一个系统C,还需要修改系统A的代码,再加入调用......
  • 手写简单生产者消费者阻塞队列
    主要实现生产者定时生产,消费者只要队列消息中有就消费。importjava.util.*;importjava.util.concurrent.atomic.AtomicInteger;importjava.util.concurrent.locks.Condition;importjava.util.concurrent.locks.ReentrantLock;publicclassProductConsumerQueue<E>{......
  • [左神面试指南] 栈和队列篇
    CD5设计一个有getMin功能的栈/**维护一个最小栈minStack*dataStack每压入一个数,minStack也压入一个当前状态的最小值*/publicclassCD5_1{publicstaticclassSolution{publicStack<Integer>dataStack=newStack<>();publicSt......
  • Visual Studio 2008安装ASP.NET MVC 2 RTM
    1首先,要安装VisualStudio2008SP1,下载地址http://www.microsoft.com/en-us/download/details.aspx?id=109862下载ASP.NETMVC2RTM(英文版,2.5M,AspNetMVC2_VS2008.exe)下载地址http://www.microsoft.com/en-us/download/details.aspx?id=220793双击AspNetMVC2_VS2008.e......
  • 数据结构-队列和栈
    栈和队列是两种不同的数据形式,区别就是栈是先进后出,但是队列先进先出,可以用数据结构模拟这两种形式。1、队列完整代码如下:#include<stdio.h>#include<stdlib.h>#if0/*顺序队列*/intenQueue(int*a,intrear,intdata){a[rear]=data;rear++;retur......
  • 数据结构-队列
    一、概念1、队列的定义队列是仅限在一端进行插入,另一端进行删除的线性表。队列又被称为先进先出(FirstInFirstOut)的线性表,简称FIFO。2、队首允许进行元素删除的一端称为队首3、队尾允许进行元素插入的一端称为队尾二、接口1、可写接口(1)数据入队队列的插入操作,叫做入队。它是......
  • 单调队列学习笔记
    Menu单调队列(MonotonicQueue)简介代码模板例题单调栈(MonotonicStack)简介代码模板例题......
  • 队列(阻塞队列、非阻塞队列)的详解
    队列的详解什么是队列?用来存储一条条消息(线程)的容器是一个对列。队列是一种特殊的线性表,遵循先入先出、后入后出的基本原则什么是阻塞队列,什么是非阻塞队列?阻塞队列:添加元素时,超过总数则会进行等待(阻塞)。删除元素时,队列为空则会进行等待(阻塞)。非阻塞队列:不管什么情况下都......
  • 栈和队列的应用
    栈和队列的应用栈的应用逆序输出栈的逆序输出应该是栈最简单的应用了,由于栈的先进后出的特点,我们很自然地想到将输入序列按顺序压入栈中,在将所有元素压入栈中以后,再从栈顶依次弹出所有元素,这样就得到了一个被逆置的序列。下面我们进行一个约定:用<表示栈顶,用]表示栈底,如\(<1,......