• 链表模拟堆栈


    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct stack
    {
     int data;
     struct stack *next;
    }STACK;
    STACK *head,*pr;
    int nodeNum = 0;
    STACK *Createnote(int num);
    STACK *PushStack(int num);
    int PopStack(void);
    int main()
    {
     int pushNum[5] = {111,222,333,444,555},popNum[5],i;
     for(i = 0;i < 5;i++)
     {
      PushStack(pushNum[i]);
      printf_s("Push %dth Data: %d ",i+1,pushNum[i]);
     }
     for(i = 0;i < 5;i++)
     {
      popNum[i] = PopStack();
      printf_s("Pop %dth Data: %d ",5-i,popNum[i]);
     }
     system("pause");
     return 0;
    }
    STACK *CreateNode(int num)
    {
     STACK *p;
     p = (STACK *)malloc(sizeof(STACK));
     if(p == NULL)
     {
      printf_s("No enough memory! ");
      exit(0);
     }
     p ->next = NULL;
     p ->data = num;
     return p;
    }
    STACK *PushStack(int num)
    {
     if(nodeNum == 0)
     {
      head = CreateNode(num);
            pr = head;
      nodeNum++;
     }
     else
     {
      pr ->next = CreateNode(num);
      pr = pr ->next;
      nodeNum++;
     }
     return pr;
    }
    int PopStack(void)
    {
     STACK *p = head;
     int result;
     for(;;)
     {
      if(p ->next ==NULL)
      {
       break;
      }
      else
      {
       pr = p;
       p = p->next;
       nodeNum--;
      }
     }
     pr ->next = NULL;
     result = p ->data;
     free(p);
     return result;
    }

  • 相关阅读:
    L2 L3 L4
    C 语言assert使用
    VIM 命令收藏
    C++继承实例
    关于 WinScp 的一点使用经验
    Boa服务器移植
    Android 去掉标题全屏显示
    sys下gpio操作
    linux下 XGCOM串口助手的安装
    linux中inittab文件详解
  • 原文地址:https://www.cnblogs.com/joyclub/p/4423949.html
Copyright © 2020-2023  润新知