• c/c++链队列


    链队列

    链队列就是简化了的单链表

    nodequeue.h

    #ifndef __NODEQUEUE__
    #define __NODEQUEUE__
    
    #include <stdio.h>
    #include <malloc.h>
    #include <assert.h>
    #include <memory.h>
    #include <stdbool.h>
    
    #define ElemType int
    
    typedef struct Node{
      ElemType data;
      struct Node* next;
    }Node;
    
    typedef struct NodeQueue{
      Node*  front;
      Node*  tail;
      size_t size;
    }NodeQueue;
    
    void init(NodeQueue*);
    void enQueue(NodeQueue*, ElemType);
    void deQueue(NodeQueue*);
    void show_list(NodeQueue*);
    int length(NodeQueue*);
    void clear(NodeQueue*);
    void destroy(NodeQueue*);
    
    #endif
    

    nodequeue.c

    #include "nodequeue.h"
    
    void init(NodeQueue* queue){
      queue->front = queue->tail = (Node*)malloc(sizeof(Node));
      queue->tail->next = NULL;
      queue->size = 0;
    }
    //入队(尾插)                                                                    
    void enQueue(NodeQueue* queue, ElemType val){
      Node* p = (Node*)malloc(sizeof(Node));
      p->data = val;
      if(queue->front->next == NULL){
        queue->front->next = p;
      }
      else{
        queue->tail->next = p;
      }
      queue->tail = p;
      p->next = NULL;
      queue->size++;
    }
    //出队(头删)                                                                    
    void deQueue(NodeQueue* queue){
      if(queue->size == 0)return;
      Node* tmp = queue->front->next;
      queue->front->next = queue->front->next->next;
      free(tmp);
      queue->size--;
    }
    int length(NodeQueue* queue){
      return queue->size;
    }
    void show_list(NodeQueue* queue){
      Node* p = queue->front;
      while(p->next != NULL){
        printf("%d
    ", p->next->data);
        p = p->next;
      }
    }
    void clear(NodeQueue* queue){
      if(queue->size == 0)return;
      Node* p = queue->front;
      Node* tmp;
      while(p->next != NULL){
        tmp = p->next;
        p = p->next;
        free(tmp);
      }
      queue->tail = queue->front;
      queue->tail->next = NULL;
      queue->size = 0;
    }
    void destroy(NodeQueue* queue){
      clear(queue);
      free(queue->front);
    }
    

    nodequeuemain.c

    include "nodequeue.h"
    
    int main(){
      NodeQueue list;
      init(&list);
      int select = 1;
      ElemType item;
      int index;
      while(select){
        printf("*****************************************
    ");
        printf("*** [1]   push        [2]  pop        ***
    ");
        printf("*** [3]   show_list   [4]  length     ***
    ");
        printf("*** [5]   clear       [6]  destroy    ***
    ");
        printf("*** [0]   quit                        ***
    ");
        printf("*****************************************
    ");
        printf("请选择:>");
        scanf("%d", &select);
        if(0 == select)
          break;
        switch(select){
        case 1:
          printf("请输入要插入的数据>
    ");
          scanf("%d",&item);
          enQueue(&list, item);
          show_list(&list);
          break;
        case 2:
         deQueue(&list);
          show_list(&list);
          break;
        case 3:
          show_list(&list);
          break;
        case 4:
          printf("length is %d
    ", length(&list));
          break;
        case 5:
          clear(&list);
          show_list(&list);
          break;
        case 6:
          destroy(&list);
          break;
        default:
          printf("输入的选择错误,请重新选择
    ");
          break;
        }
      }
      destroy(&list);
    }
    
    
  • 相关阅读:
    解决“已禁用对分布式事务管理器(MSDTC)的网络访问”错误
    C# Freely convert between IList<T> and IEnumerable<T>
    Json MaxJsonLength Error
    [转]Introducing the IE8 Developer Tools JScript Profiler
    页面调试心得
    ASP.NET中Label控件特殊之处
    模板模式
    Android中通过typeface设置字体
    Android 新浪微博授权
    【转】android:网络图片转为bitmap 保存至SD卡中
  • 原文地址:https://www.cnblogs.com/xiaoshiwang/p/9246493.html
Copyright © 2020-2023  润新知