zoukankan      html  css  js  c++  java
  • 41深入理解C指针之---指针与栈

      一、借助第40指针与链表的相关内容,稍微修改即可:

       1、定义头文件stack.h代码如下:

     1 #include <stdlib.h>
     2 #include <stdio.h>
     3
     4 #ifndef stack_h
     5 #define stack_h
     6 typedef int DataType;
     7
     8 typedef struct _node{
     9     DataType data;
    10     struct _node *next;
    11 } Node;
    12
    13 typedef struct _stack{
    14     Node *head;
    15     Node *tail;
    16 } Stack;
    17
    18 void initStack(Stack *);
    19 void pushStack(Stack *, DataType);
    20 void popStack(Stack *);
    21 int getLength(Stack *);
    22 void dispStack(Stack *);
    23
    24 #endif

      头文件中依旧是完成数据类型的声明和数据的操作函数的声明。

       2、头文件对应的实现文件stack.c代码如下:

     1 #include "stack.h"
     2
     3 //栈初始化
     4 void initStack(Stack *stack){
     5     stack->head = NULL;
     6     stack->tail = NULL;
     7 }
     8
     9 //栈入
    10 void pushStack(Stack *stack, DataType iData){
    11     Node *node = (Node *)malloc(sizeof(Node));
    12     node->data = iData;
    13     node->next = NULL;
    14
    15     if(stack->head == NULL){
    16         stack->tail = node;
    17     }else{
    18         node->next = stack->head;
    19     }
    20     stack->head = node;
    21
    22     return;
    23
    24 }
    25
    26 //栈出
    27 void popStack(Stack *stack){
    28     if(stack->head->next == NULL){
    29         stack->head = NULL;;
    30     }else{
    31         stack->head = stack->head->next;
    32     }
    33
    34     return;
    35 }
    36
    37 //栈长度
    38 int getLength(Stack *stack){
    39     Node *node = stack->head;
    40     int i = 0;
    41     while(node != NULL){
    42         node = node->next;
    43         i++;
    44     }
    45
    46     return i;
    47 }
    48
    49 //栈输出
    50 void dispStack(Stack *stack){
    51     Node *node = stack->head;
    52     int i = 0;
    53     while(node != NULL){
    54         printf("the %dth node: %d
    ", i + 1, node->data);
    55         node = node->next;
    56         i++;
    57     }
    58     printf("disp finished!
    ");
    59
    60     return;
    61 }

      代码说明:

        1、入栈函数使用链表的头插法

        2、出栈函数直接将链表的头节点删除即可实现出栈功能

       3、stack.c对应的测试文件testStack.c代码如下:

     1 #include "stack.h"
     2
     3 int main(int argc, char **argv)
     4 {
     5     Stack *stack1 = (Stack *)malloc(sizeof(Stack));
     6     printf("the first:
    ");
     7     initStack(stack1);
     8     pushStack(stack1, 1);
     9     pushStack(stack1, 3);
    10     pushStack(stack1, 5);
    11     pushStack(stack1, 7);
    12     pushStack(stack1, 9);
    13     printf("The length: %d
    ", getLength(stack1));
    14     dispStack(stack1);
    15     printf("the second:
    ");
    16     popStack(stack1);
    17     printf("The length: %d
    ", getLength(stack1));
    18     dispStack(stack1);
    19     popStack(stack1);
    20     dispStack(stack1);
    21     printf("The length: %d
    ", getLength(stack1));
    22     pushStack(stack1, 11);
    23     dispStack(stack1);
    24     printf("The length: %d
    ", getLength(stack1));
    25
    26     return 0;
    27 }

      测试函数完成所有函数的功能测试,功能通过就OK!

  • 相关阅读:
    C#网络编程(异步传输字符串)
    C#网络编程(同步传输字符串)
    C#网络编程(基本概念和操作)
    Asp.Net 构架(HttpModule 介绍)
    Asp.Net 构架(Http Handler 介绍)
    Asp.Net构架(Http请求处理流程)
    XML的应用 ---- 从一个范例看xml数据、xsd验证、xslt样式
    jQuery的ajax跨域实现
    常见26个jquery使用技巧详解
    常用Request对象获取请求信息
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/6960857.html
Copyright © 2011-2022 走看看