• 2008秋季计算机软件基础0922课堂用例(2)


    /*---------------------------------------------------------
     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);
    }



  • 相关阅读:
    JAVA学习日报 12/15
    JAVA学习日报 12/14
    JAVA学习日报 12/13
    如何在idea上配置meaven和tomcat!(idea2020)
    关于win10共存多个版本jdk,并如何进行最简单的切换
    Java面试知识点
    一些基本html标签的使用案例
    线性代数复习笔记
    考前加分项
    Javaweb学习12.18
  • 原文地址:https://www.cnblogs.com/emanlee/p/1297324.html
Copyright © 2020-2023  润新知