堆栈代码实践

1.顺序堆栈

#include<stdio.h>
#include<stdlib.h>
 
#define MAX 100
typedef int ElementType;
ElementType ERROR=-1;
typedef struct SNode *Stack;
struct SNode{
	ElementType data[MAX];
	int top;
};

Stack Creat() //初始化 
{
	Stack p = (Stack)malloc(sizeof(struct SNode));
	p->top = -1;
	return p;
}

void Push(Stack PtrS,ElementType  item)  //入栈 
{
	if(PtrS->top == MAX-1)
	{
		printf("堆栈满");
		return ;	
	}	
	else
	{
		PtrS->data[++(PtrS->top)] = item;
		return ;
	}
} 

ElementType Pop(Stack PtrS)  //出栈 
{
	if(PtrS->top == -1)
	{
		printf("堆栈空");
		return ERROR;
	}
	else
		return (PtrS->data[(PtrS->top)--]);
}

ElementType GetTop(Stack PtrS)  //读栈顶元素 
{
	if(PtrS->top == -1)
	{
		printf("堆栈空");
		return ERROR;
	}
	else
		return (PtrS->data[(PtrS->top)]);
}

bool Empty(Stack PtrS) // 判空
{
	if(PtrS->top == -1){
        return true;
    }else{
        return false;
    }
} 

void clear(Stack PtrS)
{
	PtrS->top = -1;	
} 

int main()
{
	Stack S=Creat();
	Push(S,5);
	Push(S,15);
	printf("%d\n",Pop(S));
	printf("%d\n",GetTop(S));
	Push(S,25);
	if(!Empty(S))
		printf("当前栈不为空\n");
	else
		printf("当前栈为空\n");
	clear(S);
	if(!Empty(S))
		printf("当前栈不为空\n");
	else
		printf("当前栈为空\n");		
}

2.双端堆栈

#include<stdio.h>
#include<stdlib.h>

#define MAX 100
typedef int ElementType; 
ElementType ERROR=-1;
typedef struct DSNode{
	ElementType data[MAX];
	int top1;
	int top2;
}DStack;

DStack *Creat()
{
	DStack* p = (DStack*)malloc(sizeof(DStack));
	p ->top1 = -1;
	p->top2 = MAX-1;
	return p;
}

void Push(DStack* S,ElementType item,int Tag)
{
	if(S->top2 - S->top1 == 1)
	{
		printf("堆栈满");
		return ; 
	}
	if(Tag == 1)
		S->data[++(S->top1)] = item;
	else 
		S->data[--(S->top2)] = item;	
}

ElementType Pop(DStack* S, int Tag)
{
	if(Tag == 1)
	{
		if(S->top1 == -1)
		{
			printf("堆栈1为空");
			return ERROR;
		}
		else
			return S->data[S->top1--];
	}	
	else
	{
		if(S->top2 == MAX-1)
		{
			printf("堆栈2为空");
			return ERROR;
		}
		else
		return S->data[S->top2++];	
	}
}

ElementType GetTop(DStack* S,int Tag)
{
	if(Tag == 1)
	{
		if(S->top1 == -1)
		{
			printf("堆栈1为空");
			return ERROR;
		}
		else
			return S->data[S->top1];
	}	
	else
	{
		if(S->top2 == MAX-1)
		{
			printf("堆栈2为空");
			return ERROR;
		}
		else
		return S->data[S->top2];	
	}
}

bool Empty(DStack* S,int Tag)
{
	if(Tag == 1)
	{
		if(S->top1 == -1)
			return true;	
	}
	if(Tag == 2)
	{
		if(S->top2 == MAX - 1)
			return true;	
	}	
	return false;
}  

void Clear(DStack* S,int Tag)
{
	if(Tag == 1)
		S->top1 == -1;
	if(Tag == 2)
		S->top2 == MAX - 1;	
}

int main()
{
	DStack *S = Creat();
	Push(S,15,1);
	Push(S,25,2);
	printf("%d\n",GetTop(S,2)); 
	printf("%d\n",GetTop(S,1)); 
	Pop(S,1);
	Pop(S,2);
	printf("%d\n",GetTop(S,2)); 
	printf("%d\n",GetTop(S,1)); 
	if(!Empty(S,1))
		printf("当前栈1不为空\n");
	else
		printf("当前栈1为空\n");
	Push(S,15,1);
	Push(S,25,2);	
	if(!Empty(S,2))
		printf("当前栈2不为空\n");
	else
		printf("当前栈2为空\n");
	
}

3.链表堆栈

#include<stdio.h>
#include<stdlib.h>

typedef int ElementType;
ElementType ERROR = -1;

typedef struct SNode *Stack;
struct SNode{
	ElementType data;
	struct SNode *next; 
}; 

Stack Creat()
{
	Stack S;
	S = (Stack)malloc(sizeof(struct SNode));
	S->next = NULL;
	return S;
}

int IsEmpty(Stack S)
{
	return S->next == NULL;
}

void Push(Stack S,ElementType item)
{
	Stack NewNode = (Stack)malloc(sizeof(struct SNode));
	NewNode->data = item;
	NewNode->next = S->next;
	S->next = NewNode;
}

ElementType Pop(Stack S)
{
	Stack temp;
	ElementType result;	
	if(!IsEmpty(S))
	{
		temp = S->next;
		S->next = temp->next;
		result = temp->data;
		free(temp);
		return result;	
	}
	else
	{
		printf("当前栈为空\n");
		return ERROR;	
	}
		
}

void Clear(Stack S)
{
	S->next = NULL;
}

int main()
{
	Stack S;
	S = Creat();
	Push(S,5);
	Push(S,15);
	printf("%d\n",Pop(S));
	printf("%d\n",Pop(S));
	Push(S,15);
	if(!IsEmpty(S))
		printf("当前栈不为空\n");
	else
		printf("当前栈为空\n");
	Clear(S);
	if(!IsEmpty(S))
		printf("当前栈不为空\n");
	else
		printf("当前栈为空\n");
	
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值