체인 스 택 의 기본 조작 - 데이터 구조
2016 단어 데이터 구조
예 코드:
#include
#include
#include
#define OK 1
#define ERROR 0
#define VOERFLOW -1
typedef int Status;
typedef int Elemtype;
typedef struct LNode
{
Elemtype data;
struct LNode *next;
}LNode,*Linklist;
bool EmptyStack(Linklist s)
{
if(s->next==NULL) return true;
else return false;
}
Status InitStack(Linklist &s)
{
s=(LNode*)malloc(sizeof(LNode));
s->next=NULL;
return OK;
}
Status Pushstack(Linklist &s,Elemtype e)
{
Linklist p;
p=(LNode*)malloc(sizeof(LNode));
p->data=e;
p->next=s->next;
s->next=p;
return OK;
}
Status DisplayStack(Linklist s)
{
Linklist p;
p=s->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("
");
return OK;
}
int StackLength(Linklist s)
{
int i;
Linklist p;
p=s->next;
i=0;
while(p!=NULL)
{
i++;
p=p->next;
}
return i;
}
Elemtype GetTopelem(Linklist s,Elemtype &e)
{
if(s->next==NULL) return ERROR;
e=s->next->data;
return OK;
}
Status PopStack(Linklist &s)
{
if(EmptyStack(s)) return ERROR;
Linklist p;
p=(LNode*)malloc(sizeof(LNode));
p=s->next;
s->next=p->next;
free(p);
}
Status ClearStack(Linklist &s)
{
Linklist p,q;
p=s->next;
while(p!=NULL)
{
q=p;
s->next=p->next;
p=p->next;
free(q);
}
return OK;
}
int main()
{
Linklist stack;
int n,i,len;
Elemtype e;
InitStack(stack);
printf("Input n and the elem:");
scanf("%d",&n);
for(i=0;i