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

    代码非常简单,直接代码stackg.h

     1 #ifndef stackg_h
     2 #define stackg_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 push(List *, DataType);
    22 void pop(List *);
    23 Node *getTop(List *);
    24 int getLength(List *);
    25 void dispList(List *);
    26 
    27 #endif

    对应的实现文件stackg.c

     1 #include "stackg.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 push(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->tail = node;
    20     }else{
    21         node->next = list->head;
    22     //3、插入节点
    23     }
    24     list->head = node;
    25 
    26     return;
    27 }
    28 
    29 void pop(List *list){
    30     list->head = list->head->next;
    31 
    32     return;
    33 }
    34 
    35 Node *getTop(List *list){
    36     Node *node = (Node *)malloc(sizeof(Node));
    37     node = list->head;
    38 
    39     return node;;
    40 }
    41 
    42 int getLength(List *list){
    43     Node *node = (Node*)malloc(sizeof(Node));
    44     node = list->head;
    45     int i = 0;
    46     while(node != NULL){
    47         node = node->next;
    48         i++;
    49     }
    50 
    51     return i;
    52 }
    53 
    54 void dispList(List *list){
    55     Node *node = (Node *)malloc(sizeof(Node));
    56     node = list->head;
    57     int i = 0;
    58     while(node != NULL){
    59         printf("the %dth node: %d
    ", i + 1, node->data);
    60         node = node->next;
    61         i++;
    62     }
    63     printf("display finished
    ");
    64 
    65     return;
    66 }

    测试文件testStackg.c

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

    直接运行即可,win10下linux子系统ubuntu18.04

  • 相关阅读:
    旋转坐标转换的矩阵推导
    Union的妙用和注意
    OpenGL环境搭建Windows+Mac+Linux
    RichLabel基于Cocos2dx+Lua v3.x
    Cocos2dx+lua中Color参数的坑
    常见开源协议
    lua序列化(支持循环引用)
    lua标签解析器
    Markdown基本语法
    python3爬取墨迹天气并发送给微信好友,附源码
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/13916236.html
Copyright © 2011-2022 走看看