以下是用C语言实现的一个栈的结构:
定义一个节点结构

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. typedef struct Node{  
  2.     int data;  
  3.     struct Node*p;  
  4. }NODE,*PNODE;  


定义栈的结构体

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. typedef struct Stack{  
  2.     PNODE top;  
  3.     PNODE bottom;  
  4. }STACK,*PSTACK;  


对栈的初始化函数

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. void initStack(PSTACK pStack)  
  2. {  
  3.     pStack->top = (PSTACK)malloc(sizeof(NODE));  
  4.     if (pStack->top == NULL)  
  5.     {  
  6.         printf("maoolc error\n");  
  7.         exit(-1);  
  8.     }  
  9.     else  
  10.     {  
  11.         pStack->bottom = pStack->top;  
  12.         pStack->top->p = NULL;  
  13.     {  
  14. }  


入栈函数

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
    1. void pushStack(PSTACK pStack,int val)  
    2. {  
    3.     PNODE pNew = (PNODE)malloc(sizeof(NODE));  
    4.     if (pNew == NULL)  
    5.     {  
    6.         printf("maoolc error\n");  
    7.         exit(-1);  
    8.     }  
    9.     pNew->data =val;  
    10.     pNew->next = pStack->top;  
    11.     pStack->top = pNew;  
    12.     return;  
    13. }  
posted on 2015-01-18 20:11  知了112  阅读(150)  评论(0编辑  收藏  举报