【C语言学习】栈的操作模板
#include<stdio.h>
typedef struct node
{
int data;
struct node *next;
}Node;
typedef struct stack
{
Node * stackTop;
int stackSize;
}Stack;
Node * CrateNode(int data)
{
Node* newNode=(Node*)malloc(sizeof(Node));
newNode->data=data;
newNode->next=NULL;
return newNode;
}
Stack * CreatStacke()
{
Stack* mystack=(Stack*)malloc(sizeof(Stack));
mystack->stackSize=0;
mystack->stackTop=NULL;
return mystack;
}
void push(Stack mystack,int data)
{
Node *a=CrateNode(data);
a->next=mystack.stackTop;
mystack.stackTop=a;
mystack.stackSize++;
}
int pop(Stack mystack)
{
if(mystack.stackSize==0)
{
exit(0);
}
Node* tempnode=mystack.stackTop;
int a=mystack.stackTop->data;
mystack.stackTop=mystack.stackTop->next;
mystack.stackSize--;
free(tempnode);
return a;
}
int getpop(Stack mystack)
{
if(mystack.stackSize==0)
{
exit(0);
}
return mystack.stackTop->data;
}
int isempty(Stack mystack)
{
if(mystack.stackSize==0)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
}
#define MaxSize 1000
typedef struct elementtype
{
int tyint;
char tychar;
}ElementType;
typedef struct stack
{
ElementType* top;
ElementType* base;
int stacksize;
}Stack;
Stack* CrateStack()
{
Stack* newStack=(Stack*)malloc(sizeof(Stack));
newStack->base=(ElementType*)malloc(sizeof(ElementType)*MaxSize);
newStack->top=newStack->base;
newStack->stacksize=MaxSize;
return newStack;
}
int Push(Stack* myStack,ElementType data)
{
if(myStack->top-myStack->base==myStack->stacksize)
{
exit(1);
}
*myStack->top=data;
myStack->top++;
return 1;
}
ElementType* Pop(Stack* myStack)
{
if(myStack->top==myStack->base)
{
exit(2);
}
myStack->top--;
return myStack->top;
}
ElementType* GetTop(Stack* mystack)
{
if(mystack->top==mystack->base)
{
exit(3);
}
return mystack->top-1;
}