栈的基本,数组链表存储

数组存储
代码一

#include<stdio.h>
#include<stdlib.h>
#define MaxSize 20
typedef char ElementType;
typedef struct SNode *Stack;
struct SNode{
	ElementType Data[MaxSize];
	int Top;
};
//栈的顺序存储实现
Stack InitStack()
{
	Stack S;
	if((S=(Stack)malloc(sizeof(SNode)))==NULL)
	{
		printf("分配空间失败");
	} 
	S->Top=-1;
	return S;
}

void Push( Stack PtrS, ElementType item ) 
{
 	if ( PtrS->Top == MaxSize-1 ) 
 	{ 
	 	printf("堆栈满"); 
		return; 
	}
 	else
  	{
 		PtrS->Data[++(PtrS->Top)] = item;
 		return;
 	} 
}

ElementType Pop( Stack PtrS ) 
{
 	if ( PtrS->Top == -1 ) 
	{
 		printf("堆栈空");
		return 0; /* ERROR是ElementType的特殊值,标志错误*/
	} 
 	else 
 	return ( PtrS->Data[(PtrS->Top)--] );
}
int IsEmpty(Stack PtrS)
{
	if(PtrS->Top == -1)
	{
		return 0;
	 } 
	else 
		return 1;
}
ElementType Top(Stack PtrS)
{
	return PtrS->Top == -1 ? 0 : PtrS->Data[PtrS->Top];
}
int main()
{
	Stack s;
	s=InitStack();
	
	return 0;
}

代码二

typedef struct stack1
{
	char sign[1000];
	int top;
}*Stack1;

//有头结点
Stack1 create()
{
	Stack1 head = (Stack1)malloc(sizeof(struct stack1));
	head->top = -1;
	return head;
}

//入栈
void push1(char x, Stack1 stk)
{
	stk->top++;
	stk->sign[stk->top] = x;
}

//出栈并返回出栈元素
char pop1(Stack1 stk)
{
	char x = stk->sign[stk->top];
	stk->top--;
	return x;
}

char GetTop1(Stack1 stk)
{
	return stk->sign[stk->top];
}

链表存储

struct StackRecord;
typedef struct StackRecord *Stack;
typedef char ElementType;
 
#define EmptyTOS (-1)
#define MinStackSize (5)
 
struct StackRecord
{
    int Capacity;
    int TopOfStack;
    ElementType *Array;
};
 
void MakeEmpty(Stack S)
{
    S->TopOfStack = EmptyTOS;
}
 
Stack CreateStack(int MaxElements)
{
    Stack S;
 
    if (MaxElements < MinStackSize)
        printf("Stack size is too small");
 
    S = malloc(sizeof(struct StackRecord));
    if (S == NULL)
        printf("Out of space!!!");
 
    S->Array = malloc(sizeof(ElementType) * MaxElements);
    if (S->Array == NULL)
        printf("Out of space!!!");
    S->Capacity = MaxElements;
    MakeEmpty(S);
 
    return S;
}
 
void DisposeStack(Stack S)
{
    if (S != NULL)
    {
        free(S->Array);
        free(S);
    }
}
 
int IsEmpty(Stack S)
{
    return S->TopOfStack == EmptyTOS;
}
 
int IsFull(Stack S)
{
    return S->TopOfStack == S->Capacity - 1;
}
 
void Push(ElementType X, Stack S)
{
    if (IsFull(S))
        printf("Full stack");
    else
        S->Array[++S->TopOfStack] = X;
}
 
ElementType Top(Stack S)
{
    if (!IsEmpty(S))
        return S->Array[S->TopOfStack];
    printf("Empty stack");
    return 0;
}
 
void Pop(Stack S)
{
    if (IsEmpty(S))
        printf("Empty stack");
    else
        S->TopOfStack--;
}
 
ElementType TopAndPop(Stack S)
{
    if (!IsEmpty(S))
        return S->Array[S->TopOfStack--];
    printf("Empty stack");
    return 0;
}

代码二

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 100
typedef struct stack
{
   int topofstack;
   char *array;
}stack;

stack *create(void);
void push(char ch, stack *s);
void pop(stack *s);
char top(stack *s);

stack* create(void)
{
    stack*s=(stack*)malloc(sizeof(stack));
    s->array=(char*)malloc(sizeof(char)*max);
    s->topofstack=-1;
    return s;
}
void push(char ch, stack *s)
{
    s->array[++s->topofstack]=ch;
}
void pop(stack*s)
{
    if (s->topofstack==-1)
    return;
    s->topofstack--;
}
char top(stack *s)
{
    return s->topofstack==-1?0: s->array[s->topofstack];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值