链栈的C语言实现

    /*
    功能:栈的链表实现
    Author:lxm
    Date: 20160511
*/

#include <stdio.h>
#include <stdlib.h>

#define ELEMTYPE int
#define STACK_EMPTY -9999
#define N 10

typedef struct Node
{
    ELEMTYPE data;
    struct Node * next;
}LNode;

void initStack(LNode **S);
int isStackEmpty(LNode **S);
void push(LNode **S,ELEMTYPE e);
ELEMTYPE pop(LNode **S);
void printStack(LNode **S);

int main()
{

    LNode *LS;
    initStack(&LS);

    int i;
    for(i=1;i<=N;++i)
    {
            push(&LS,i);
    }
    printStack(&LS);
    LNode *p = LS->next;
    /*while(p!=NULL)
    {
        printf("%d	",p->data);
        p = p->next;
    }
    */
    return 0;
}


void initStack(LNode **S)
{
        (*S) = (LNode*)malloc(sizeof(LNode));
        (*S)->next = NULL;
}

int isStackEmpty(LNode **S)
{

   return ((*S)->next == NULL);

}

void push(LNode **S,ELEMTYPE e)
{

    LNode *p = (LNode*)malloc(sizeof(LNode));
    p->data = e;

    p->next =  (*S)->next;
     (*S)->next = p;
}

ELEMTYPE pop(LNode **S)
{
    if(isStackEmpty(S)) return STACK_EMPTY;

    LNode *p = (*S)->next;
     (*S)->next = p->next;
    ELEMTYPE result = p->data;
    free(p);
    p = NULL;
    return result;
}

void printStack(LNode **S)
{
   while(!isStackEmpty(S))
    {
        printf("%d	",pop(S));
    }
}
原文地址:https://www.cnblogs.com/yldf/p/6249904.html