zoukankan      html  css  js  c++  java
  • 数据结构之__队列

    老样子,代码就是一切,首先queueg.h

     1 #ifndef queueg_h
     2 #define queueg_h
     3 
     4 #include <stdio.h>
     5 #include <stdlib.h>
     6 
     7 typedef int DataType;
     8 
     9 typedef struct node_{
    10     DataType data;
    11     struct node_ *next;
    12 } Node;
    13 
    14 typedef struct list_{
    15     Node *head;
    16     Node *tail;
    17     Node *current;
    18 } List;
    19 
    20 void initList(List *);
    21 void inQueue(List *, DataType);
    22 void outQueue(List *);
    23 Node *frontQueue(List *);
    24 int getLength(List *);
    25 void dispList(List *);
    26 
    27 #endif

    接着queueg.c

     1 #include "queueg.h"
     2 
     3 void initList(List *list){
     4     list->head = NULL;
     5     list->tail = NULL;
     6     list->current = NULL;
     7 
     8     return;
     9 }
    10 
    11 void inQueue(List *list, DataType data){
    12     //1、创建一个节点
    13     Node *node = (Node *)malloc(sizeof(Node));
    14     node->data = data;
    15     node->next = NULL;
    16 
    17     //2、插入节点准备
    18     if(list->head == NULL){
    19         list->head = node;
    20     }else{
    21         //node->next = list->head;
    22         list->tail->next = node;
    23     //3、插入节点
    24     }
    25     list->tail = node;
    26 
    27     return;
    28 }
    29 
    30 void outQueue(List *list){
    31     list->head = list->head->next;
    32 
    33     return;
    34 }
    35 
    36 Node *frontQueue(List *list){
    37     Node *node = (Node *)malloc(sizeof(Node));
    38     node = list->head;
    39 
    40     return node;;
    41 }
    42 
    43 int getLength(List *list){
    44     Node *node = (Node*)malloc(sizeof(Node));
    45     node = list->head;
    46     int i = 0;
    47     while(node != NULL){
    48         node = node->next;
    49         i++;
    50     }
    51 
    52     return i;
    53 }
    54 
    55 void dispList(List *list){
    56     Node *node = (Node *)malloc(sizeof(Node));
    57     node = list->head;
    58     int i = 0;
    59     while(node != NULL){
    60         printf("the %dth node: %d
    ", i + 1, node->data);
    61         node = node->next;
    62         i++;
    63     }
    64     printf("display finished
    ");
    65 
    66     return;
    67 }

    最后,测试代码testQueue.c

     1 #include "queueg.h"
     2 
     3 int main(int argc, char **argv)
     4 {
     5     List *list = (List *)malloc(sizeof(List));
     6     initList(list);
     7     inQueue(list, 4);
     8     inQueue(list, 6);
     9     inQueue(list, 8);
    10     inQueue(list, 10);
    11     dispList(list);
    12     Node *tmp = frontQueue(list);
    13     printf("getTop result: %d
    ", tmp->data);
    14     outQueue(list);
    15     dispList(list);
    16     outQueue(list);
    17     dispList(list);
    18     printf("the list: %d
    ", getLength(list));
    19 
    20     return 0;
    21 }

    整体还是比较简单,主要是便于学生从表的角度看待栈和队列了

  • 相关阅读:
    进阶之路 | 奇妙的View之旅
    进阶之路 | 奇妙的Window之旅
    进阶之路 | 奇妙的Activity之旅
    Laravel5.5 邮件发送报错:stream_socket_client()
    ThinkPHP中使用PHPMailer发送邮件
    php 实现密码错误三次锁定账号10分钟
    自定义函数实现字符串数组互转
    自定义函数实现判断一个字符串是否包含另外一个字符串
    PHP一个for循环输出9*9乘法表
    中国地区表SQL语句
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/13916267.html
Copyright © 2011-2022 走看看