顺序栈和链栈

今天学了顺序栈和链栈,自己写了一下代码:
//#include <stdio.h>
//#include <stdlib.h>
/*//顺序栈的实现与简单应用
//实现提供操作init_stack(初始化链表).clearstack(制空栈),Push(插入),Pop(删除),Is_empty(判空).Is_full(判满)
//GetTop(读栈顶元素)
//代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
typedef int typestack;
#define stacksize 100
typedef struct 
{
int top;
typestack stack[stacksize];
}seqstack;

bool Is_empty(seqstack *);
bool Is_full(seqstack *);
void Init_stack(seqstack *);
void clear_stack(seqstack *);
void Push(seqstack *,typestack );
typestack Pop(seqstack *);
int Search_stack(seqstack *,typestack );
typestack GetTop(seqstack *);

bool Is_empty(seqstack *p){
	return p->top==-1;
}
bool Is_full(seqstack *p){
	return p->top==stacksize-1;
}
void Init_stack(seqstack *p){
	p->top=-1;
}
void clear_stack(seqstack *p){
	p->top=-1;
}
void Push(seqstack *p,typestack x){
	if(Is_full(p)){
	printf("栈满\n");
	return ;
	}
	p->stack[++p->top]=x;
}
typestack Pop(seqstack *p){
	if(Is_empty(p)){
		printf("栈空\n");
		return 0;
	}
	return p->stack[p->top--];
}
int Search_stack(seqstack *p,typestack x){
	for(int i=0;i<=p->top;i++)
		if(p->stack[i]==x)
			return i+1;
	printf("不存在\n");
	return 0;
}

typestack GetTop(seqstack *p){
	if(Is_empty(p)){
		printf("栈空");
		return 0;
	}
	return p->stack[p->top];
}

int main()
{
	int i;
	seqstack x;
	seqstack *p=&x;
	Init_stack(p);
    for(i=0;i<7;i++)
      Push(p,i);
	while(!Is_empty(p))
		printf("%d ",Pop(p));
return 0;
}*/

//链栈
//提供如下操作:1判空2插入3清空4删除5查看栈顶元素
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef int typestack;
typedef struct seqs{
typestack x;
struct seqs *next;
}*seqstack,seqstackment;

bool Is_empty(seqstack &top){
	return top==NULL;
}
void Push(seqstack &top,typestack v){
	seqstack p=(seqstack)malloc(sizeof(seqstackment));
	p->x=v;
	p->next=top;
	top=p;
}
typestack Pop(seqstack &top){
	if(!Is_empty(top)){
		seqstack p=top->next;
		typestack v=top->x;
		free(top);
		top=p;
		return v;
	}
	printf("栈空\n");
	return 0;
}
typestack GetTop(seqstack &top){
	if(!Is_empty(top))
		return top->x;
		printf("栈空\n");
		return 0;
}
void clear_stack(seqstack &top){
	while(Is_empty(top))
	 Pop(top);
}

int main()
{
    seqstack p=NULL;
    for(int i=0;i<7;i++)
		Push(p,i);
	while(!Is_empty(p))
		printf("%d ",Pop(p));
	return 0;
}



下面是栈的一些简单应用:
括号匹配:
bool Is_match(char ch1,char ch2)
{
	if(ch1=='(' && ch2==')' || ch1==')' && ch2=='(' )
		return true;
	else if(ch1=='[' && ch2==']' || ch1==']' && ch2=='[')
		return true;
	else if(ch1=='{' && ch2=='}' || ch1=='}' && ch2=='{')
		return true;
	return false;
}

void Kh_Match(char *str)
{
	seqstack top=NULL;
	char ch;
	for(int i=0;str[i];i++)
	{
		switch(str[i])
		{
		case '(':
		case '[':
		case '{':
			Push(top,str[i]);
			break;
		case ')':
		case ']':
		case '}':
			if(Is_empty(top))
			{
				printf("右括号多余\n");
				return ;
			}
			ch=GetTop(top);
			if(Is_match(ch,str[i]))
			{
				Pop(top);
				break;
			}
			else
			{
				printf("括号不匹配\n");
				return ;
			}
		}
	}
	if(Is_empty(top))
		printf("括号匹配\n");
	else
		printf("左括号多余\n");
}

//10进制与其他的转换(10转2、8、16)
void Trans_num(int n,int m)//2,8
{
   seqstack top=NULL;
  while(n)
  {
   Push(n%m);
   n/=m;
  }
  if(Is_empty(top))
   printf("0\n");
  while(!Is_empty(top))
  printf("%d ",Pop()); 
}
//运算符优先级
bool Is_number(char ch){
 switch(ch)
 {
  case '0': case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9': return true;
  default : return false;
 }
}
bool Comparech(char ch1,char ch2)
{
 switch(ch1)
 {
case '*':
 switch(ch2)
 {
case '*':
case '/':return false;
case '#':
case '+':
case '-':return true;
 }
case '/':
 switch(ch2)
 {
case '*':
case '/':return false;
case '#':
case '+':
case '-':return true;
 }
case '+':
 switch(ch2)
 {
case '*':
case '+':
case '-':
case '/':return false;
case '#':return true;
 }
case '-':
 switch(ch2)
 {
case '*':
case '/':
case '+':
case '-':return false;
case '#':return true;
 }
case '#':return false;
 }
}
int Calcu_complex()
{
 seqstack Num==NULL;
 seqstack Ope=NULL;
 Push(Ope,'#');
 char ch=getchar();
 char str;
 int a,b;
 if(ch=='#'){
  printf("there are no operating enements\n");
  return 0;
 }
 while(ch!='#' || GetTop(Ope)!='#')
 {
  if(Is_number(ch)){
   Push(Num,ch-'0');
   ch=getchar();
  }
  else
  {
   if(Comparech(ch,GetTop(Ope))){
    Push(Ope,ch);
    ch=getchar();
   }
   else{
    a=Pop(Num);
    b=Pop(Num);
    str=Pop(Ope);
    switch(str)
    {
  case '*':Push(Num,b*a);break;
  case '/':Push(Num,b/a);break;
     case '+':Push(Num,b+a);break;
        case '-':Push(Num,b-a);break;
    }
   }
  }
 }
 return GetTop(Num);
}
当然这些只不过是栈的简单应用,但是栈本身的思想:先进后出需要掌握。。。。





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值