#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 30
#define OK 1
#define ERROR -1
#define False 0
typedef int SElemType;
typedef int Status;
typedef struct stackNode* LinktackPtr;
typedef struct stackNode
{
SElemType data;
LinktackPtr next;
}stackNode;
typedef struct LinkStack
{
LinktackPtr top;
int count;
}LinkStack;
/*StackEmpty*/
Status StackEmpty(LinkStack* S)
{
if (S->top)
return OK;
else
{
return False;
}
}
/*push*/
Status Push(LinkStack* S, SElemType item)
{
LinktackPtr ptr = (LinktackPtr)malloc(sizeof(stackNode));
ptr->next = S->top;/*把当前的栈顶元素赋值给新结点的直接后继*/
S->top = ptr;/*将新的结点ptr赋值给栈顶指针*/
S->count++;
return OK;
}
/*Pop*/
Status Pop(LinkStack* S, SElemType item)
{
LinktackPtr ptr;
if (StackEmpty(S))
{
return ERROR;
}
item = S->top->data;
ptr = S->top;
S->top = S->top->next;
free(ptr);
S->count--;
return OK;
}
/******************浙大版*************/
typedef struct SNode* Stack;
struct SNode
{
SElemType Data;
struct SNode* Next;
};
/*建立一个堆栈的头结点,返回指针*/
Stack CreateStack()
{
Stack s;
s = (Stack)malloc(sizeof(struct SNode));
s->Next = NULL;
return s;
}
Status ISEmpty(Stack S)
{/*判断堆栈s是否为空,若为空函数返回整数1否则返回0*/
return (S->Next == NULL);
}
/*Push*/
Status Push_(Stack S, SElemType item)
{
struct SNode* TmpCell;
TmpCell = (Stack)malloc(sizeof(struct SNode));
TmpCell->Data = item;
TmpCell->Next = S->Next;
S->Next = TmpCell;
return OK;
}
/*Pop*/
Status Pop_(Stack S)
{
struct SNode* FirstCell;
SElemType TopElem;
if (ISEmpty(S))
{
return ERROR;
}
else
{
FirstCell = S->Next;
S->Next = FirstCell->Next;
TopElem = FirstCell->Data;
free(FirstCell);
return TopElem;
}
}
堆栈-(链式存储)(插入删除操作)
最新推荐文章于 2023-03-02 15:45:41 发布