zoukankan      html  css  js  c++  java
  • 链表模拟堆栈

    #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;
    }

  • 相关阅读:
    [USACO07FEB]银牛派对Silver Cow Party
    道路重建
    javascript基础
    css清除浮动
    css水平居中
    块元素与行内(内嵌)元素的区别
    hook
    回调函数
    Web服务API
    Enrolment注册插件
  • 原文地址:https://www.cnblogs.com/joyclub/p/4423949.html
Copyright © 2011-2022 走看看