栈的知识

1.计算进栈顺序为e1,e2,e3,e4,e5,存在多少种可能的出栈序列:

卡特兰数:C(2n,n)/(n+1)

 

2.图的广度优先用队列,深度优先用栈。

3.(*p).a,和p->a完全等效

4.顺序栈:

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

//结构体
struct SqStack
{
	 int Data[100];
     int Top;
};

//初始化栈,让栈顶为-1,而0就是开始第一个
void InitSqStack(SqStack *t)
{
    t->Top = -1;
}

//检测栈是否为空
int SqStackEmpty(SqStack *t)
{
    if(t->Top == -1)
	{
	    return 1;
	}
	else
	{
	    return 0;
	}
}

//入栈的操作
int Push(SqStack *t , int x)
{
    if(t->Top == (100-1))
	{
	   return  0;
	}
    t->Top++;
    t->Data[t->Top] = x;
	return 1;
}

//出栈的操作,由于需要输出x,所以这里用*x作为形参,而且用的时候用&x带入
int Pop(SqStack *t , int *x)
{
    if(t->Top == -1)
	{
	   return  0;
	}

	*x = t->Data[t->Top];
    t->Top--;

}

//打印栈
void PrintSqStack(SqStack *t)
{
    while(t->Top != -1)
	{
	    printf("打印栈中的东西%d\n",t->Data[t->Top--]);
	}
}


 void main()
{
    int x;
    SqStack t;
    InitSqStack(&t);
    Push(&t , 1);
	Push(&t , 2);
	Push(&t , 3);
	Push(&t , 4);
	Pop(&t , &x);
	printf("打印x%d\n",x);
	PrintSqStack(&t);
	printf("打印Top %d\n",t.Top);


}

链栈:

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

struct Lnode{
       int data;
	   struct Lnode *next;
};

void InitStack(Lnode *ln)
{
   ln = (Lnode *)malloc(sizeof(Lnode));
   ln->next = NULL;
}


void Push(Lnode *ln,int x)
{
    Lnode *p;
	p = (Lnode *)malloc(sizeof(Lnode));
	if(p ==NULL){
		printf("ERROR");
		exit(0);
	}
	p->next = NULL;
    p->data = x;
	p->next = ln->next;
	ln->next = p;
}

int Pop(Lnode *ln,int *x)
{
    Lnode *p = ln->next;
	if(p ==NULL){
		return 0;
	}
	*x = p->data;
	ln->next = p->next;
	free(p);
	return 1;
}

void PrintStack(Lnode *ln)
{
     Lnode *p = ln->next;
	 while(p)
	 {
	     printf("printstack:  %d\n",p->data);
		 p = p->next;
	 }


}

void main()
{

	Lnode p;
	int x;
	InitStack(&p);
	Push(&p,2);
	Push(&p,3);
	Push(&p,4);
	Push(&p,5);
	Pop(&p,&x);
	printf("出战元素为");
	PrintStack(&p);


}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值