⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch3_lstack.c

📁 本人讲授数据结构课程时的所写的示例程序
💻 C
字号:
/*
栈的链式实现
author: kk.h
date: 2006.9
http://www.cocoon.org.cn
*/
#include "stdio.h"
/* 数据元素的类型 */
typedef int ElemType;

/* 节点的类型(包括头节点) */
typedef struct Node{
  ElemType elem;
  struct Node *next;
}SNode;

/* 初始化,头节点 */
InitStack(SNode* pS)
{
  pS->next=NULL;
}

/* 入栈:在头节点之后插入一个新节点 */
Push(SNode* pS,ElemType e)
{
  SNode* node;
  node = (SNode*)malloc(sizeof(SNode));
  node->elem = e;
  node->next = pS->next;
  pS->next = node;
}

int Pop(SNode* pS,ElemType* pe)
{
  SNode* node;

  if (pS->next==NULL){
    return 0;
  }
  *pe = pS->next->elem;

  node=pS->next;
  pS->next=node->next;
  free(node);
  return 1;
  
}

main()
{
  SNode S;
  ElemType e;
  int N;

  InitStack(&S);
  N=1348;

  while(N){
    e = N % 8;
    Push(&S,e);
    N = N/8;
  }

  while(Pop(&S,&e)){
    printf("%d",e);
  }

  getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -