/*ADT 头文件*/
/**********************************
*author:Felix
*last update:Sun Jan 6 09:12:51 EST 2008
*description:LS=List Stack
*
*
*
*/
#ifndef ___STACK___
#define ___STACK___
#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node* LS_PtrNode;
typedef LS_PtrNode LS_Stack;
typedef int LS_Element;
int LS_IsEmpty(LS_Stack sk);
LS_Stack LS_CreateStack();
int LS_DisposeStack(LS_Stack sk);
int LS_Push(LS_Element e,LS_Stack sk);
/*pop the stack and return the top element*/
void LS_Pop(LS_Stack sk);
LS_Element LS_Top(LS_Stack sk);
#endif
/*实现文件 LS_stack.c*/
/**********************************
*author:Felix
*last update:Sun Jan 6 09:12:51 EST 2008
*description:
*
*
*
*/
#include "LS_stack.h"
struct Node
{
LS_Element e;
LS_PtrNode next;
};
static LS_Element LS_Retrieve(LS_PtrNode p)
{
return p->e;
}
int LS_IsEmpty(LS_Stack sk)
{
return (int)(NULL==sk->next);
}
LS_Stack LS_CreateStack()
{
LS_Stack tmp;
tmp=(LS_Stack)malloc(sizeof(struct Node));
if (!tmp)return NULL;
tmp->next=NULL;
return tmp;
}
int LS_DisposeStack(LS_Stack sk)
{
while(!LS_IsEmpty(sk))LS_Pop(sk);
free(sk);
}
int LS_Push(LS_Element e,LS_Stack sk)
{
LS_PtrNode tmp;
tmp=(LS_PtrNode)malloc(sizeof(struct Node));
if (!tmp)return 0;
tmp->e=e;
tmp->next=sk->next;
sk->next=tmp;
return 1;
}
/*pop the top element*/
void LS_Pop(LS_Stack sk)
{
LS_PtrNode p=sk,tmp;
tmp=p->next;
if(!tmp)return;
p->next=tmp->next;
free(tmp);
}
LS_Element LS_Top(LS_Stack sk)
{
return LS_Retrieve(sk->next);
}
/**********************************
*author:Felix
*last update:Sun Jan 6 09:12:51 EST 2008
*description:LS=List Stack
*
*
*
*/
#ifndef ___STACK___
#define ___STACK___
#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node* LS_PtrNode;
typedef LS_PtrNode LS_Stack;
typedef int LS_Element;
int LS_IsEmpty(LS_Stack sk);
LS_Stack LS_CreateStack();
int LS_DisposeStack(LS_Stack sk);
int LS_Push(LS_Element e,LS_Stack sk);
/*pop the stack and return the top element*/
void LS_Pop(LS_Stack sk);
LS_Element LS_Top(LS_Stack sk);
#endif
/*实现文件 LS_stack.c*/
/**********************************
*author:Felix
*last update:Sun Jan 6 09:12:51 EST 2008
*description:
*
*
*
*/
#include "LS_stack.h"
struct Node
{
LS_Element e;
LS_PtrNode next;
};
static LS_Element LS_Retrieve(LS_PtrNode p)
{
return p->e;
}
int LS_IsEmpty(LS_Stack sk)
{
return (int)(NULL==sk->next);
}
LS_Stack LS_CreateStack()
{
LS_Stack tmp;
tmp=(LS_Stack)malloc(sizeof(struct Node));
if (!tmp)return NULL;
tmp->next=NULL;
return tmp;
}
int LS_DisposeStack(LS_Stack sk)
{
while(!LS_IsEmpty(sk))LS_Pop(sk);
free(sk);
}
int LS_Push(LS_Element e,LS_Stack sk)
{
LS_PtrNode tmp;
tmp=(LS_PtrNode)malloc(sizeof(struct Node));
if (!tmp)return 0;
tmp->e=e;
tmp->next=sk->next;
sk->next=tmp;
return 1;
}
/*pop the top element*/
void LS_Pop(LS_Stack sk)
{
LS_PtrNode p=sk,tmp;
tmp=p->next;
if(!tmp)return;
p->next=tmp->next;
free(tmp);
}
LS_Element LS_Top(LS_Stack sk)
{
return LS_Retrieve(sk->next);
}
本文介绍了一个简单的栈数据结构实现方法,使用C语言定义了栈的基本操作,如创建、销毁、压栈、弹栈及获取栈顶元素等。该实现基于链表结构,适合初学者学习数据结构与算法。

被折叠的 条评论
为什么被折叠?



