码迷,mamicode.com
首页 > 编程语言 > 详细

c/c++链队列

时间:2018-06-30 13:03:48      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:c++   简化   scanf   ase   fine   return   def   next   链表   

链队列

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

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\n", 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("*****************************************\n");
    printf("*** [1]   push        [2]  pop        ***\n");
    printf("*** [3]   show_list   [4]  length     ***\n");
    printf("*** [5]   clear       [6]  destroy    ***\n");
    printf("*** [0]   quit                        ***\n");
    printf("*****************************************\n");
    printf("请选择:>");
    scanf("%d", &select);
    if(0 == select)
      break;
    switch(select){
    case 1:
      printf("请输入要插入的数据>\n");
      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\n", length(&list));
      break;
    case 5:
      clear(&list);
      show_list(&list);
      break;
    case 6:
      destroy(&list);
      break;
    default:
      printf("输入的选择错误,请重新选择\n");
      break;
    }
  }
  destroy(&list);
}

c/c++链队列

标签:c++   简化   scanf   ase   fine   return   def   next   链表   

原文地址:https://www.cnblogs.com/xiaoshiwang/p/9246493.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!