zoukankan      html  css  js  c++  java
  • 栈(stack)--c实现(使用双链表)

    是不是直接贴代码不太好,我竟然不知道说什么。

    写这个考虑的问题,或者是纠结的问题是这个头指针怎么处理,也就是栈的顶部,最后采用的是初始化第一个栈空间浪费掉,栈顶是有元素的。好像应该去学习下画图,没图不太好说。

    写数据结构栈的时候发现自己的2个地方需要进一步补习,用的时候心中没谱---指针和内存分配,看来我还是太水,代码里有3个warning没改出来。

      1 #include <stdio.h>
      2 
      3 
      4 typedef struct node
      5 {
      6     int data;
      7     struct node* next;
      8     struct node* pre;
      9 }Node;
     10 
     11 //Stack 一直指向栈顶
     12 typedef Node* Stack;
     13 
     14 void InitStack(Stack* stack);
     15 void DeleteStack(Stack* stack);
     16 int TopStack(Stack*);
     17 void PushStack(Stack* stack, int elem);
     18 int PopStack(Stack *stack);
     19 int IsNull(Stack*);
     20 void PrintStack(Stack *);
     21 
     22 void PrintStack(Stack *stack)
     23 {
     24     Node* preNode = *stack;
     25     while((preNode->pre != NULL)
     26           &&preNode)
     27     {
     28         printf("%d", preNode->data);
     29         preNode = preNode->pre;
     30         if(preNode->pre != NULL){
     31             printf("<==");
     32         }
     33     }
     34     printf("
    ");
     35 
     36 }
     37 //1 means NULL
     38 //stack-->node-->datamem
     39 //这个地方第一次写传的是Stack stack,为什么报错?因为穿的是值,就是一个copy,pre不能确定一定是NULL
     40 int IsNull(Stack *stack)
     41 {
     42      return ((*stack)->pre == NULL);
     43 }
     44 
     45 int PopStack(Stack *stack)
     46 {
     47     if(IsNull(stack))
     48     {
     49         printf("empty stack.
    ");
     50         return -1;
     51     }
     52     int popData = -1;
     53     popData = (*stack)->data;
     54     Node* popNode = NULL ;
     55     popNode = *stack;
     56     *stack = (*stack)->pre;
     57     (*stack)->pre = popNode->pre->pre;
     58     (*stack)->next = NULL;
     59 
     60     popNode->pre->pre->next = (*stack);
     61 
     62     free(popNode);//warning
     63 
     64     return popData;
     65 }
     66 void InitStack(Stack* stack)
     67 {
     68     (*stack)->next = NULL;
     69     (*stack)->pre = NULL;
     70     (*stack)->data = -1;
     71     return ;
     72 }
     73 
     74 int TopStack(Stack *stack)
     75 {
     76     return (*stack)->data;
     77 }
     78 
     79 void PushStack(Stack *stack, int elem)
     80 {
     81     //Node *pushNode = (Node*)malloc(sizeof(Node));
     82     Node *pushNode = (Node*) malloc(sizeof(Node));//warning
     83     if(pushNode == NULL)
     84      {
     85          printf("malloc error at line %d.", __LINE__);
     86          return;
     87      }
     88 
     89     pushNode->data = elem;
     90     pushNode->next = NULL;
     91     pushNode->pre = *stack;
     92     (*stack)->next = pushNode;
     93     *stack = (*stack)->next;
     94 
     95 }
     96 
     97 void DeleteStack(Stack* stack)
     98 {
     99 
    100     Node* preNode = stack;
    101     while((*stack)->pre != NULL)
    102     {
    103         PopStack(stack);
    104 
    105     }
    106     free(*stack);
    107 
    108     return ;
    109 }
    110 
    111 
    112 int main(void)
    113 {
    114     printf("Hello World!
    ");
    115     Stack *stack =NULL;
    116     stack = (Stack*)malloc(sizeof(stack));//warning
    117     *stack = (Node*)malloc(sizeof(Node));
    118     InitStack(stack);
    119     IsNull(stack);
    120     printf("isNull=[%d]
    ", IsNull(stack));
    121     printf("the top ele is [%d]
    ", (*stack)->data);
    122     printf("push elem  1, 2, 3, 4, 3
    ");
    123     PushStack(stack, 1);
    124     PrintStack(stack);
    125     PushStack(stack, 2);
    126     PushStack(stack, 3);
    127     PushStack(stack, 4);
    128     PushStack(stack, 3);
    129     printf("print the curr stack.
    ");
    130     PrintStack(stack);
    131     printf("Pop the stack and print the curr stack.
    ");
    132     PopStack(stack);
    133     PrintStack(stack);
    134 
    135     printf("the top elem is %d
    ", TopStack(stack));
    136 
    137 
    138     return 0;
    139 }

    是不是也把运行结果贴上去??

    QT运行的,懒得去开虚拟机在linux跑,太费事,还要改MAKEFILE.

    运行结果:/*******************begin

    Hello World!
    isNull=[1]
    the top ele is [-1]
    push elem  1, 2, 3, 4, 3
    1
    print the curr stack.
    3<==4<==3<==2<==1
    Pop the stack and print the curr stack.
    4<==3<==2<==1
    the top elem is 4

    ************end*******/

    下一篇写队列吧,想了下定义头尾指针好像会好用点。

  • 相关阅读:
    致命错误 RC1121: RC : fatal error RC1121 : I/O error reading file
    Windows 右键 添加 “使用CMD打开”
    C2061: syntax error : identifier &apos;THIS_FILE&apos;
    delphi 产生随机数
    使用Code::Blocks编译VC程序
    Delphi中用来截取字符的函数,以及使用方法
    VC6.0 最新的 SDK platform sdk xpsp2 官方下载地址
    ubuntu下codeblocks起步
    VC中Error spawning cl.exe错误的解决方法
    Delphi如何获取时间月份
  • 原文地址:https://www.cnblogs.com/ashen/p/5713161.html
Copyright © 2011-2022 走看看