栈(包含顺序栈,链栈,共享栈)--2020/7/23

  • P70题22:一个栈的入栈序列是1,2,3,…n,出栈序列为 P 1 , P 2 , . . . , P n P_1,P_2,...,P_n P1,P2,...,Pn,若 P 2 = 3 P_2=3 P2=3,则 P 3 P_3 P3可能的取值为: n − 1 n-1 n1

  • 思路:因为 P 2 = 3 P_2=3 P2=3,所以 P 3 可 为 4 , 5 , 6 , . . . , n P_3可为4,5,6,...,n P34,5,6...n

    • (1)若 P 1 = 1 则 P 3 = 2 , 4 , 5 , . . . , n P_1=1则P_3=2,4,5,...,n P1=1P3=2,45...n
    • (2) P 1 = 2 则 P 3 = 1 , 4 , 5 , . . . , n P_1=2则P_3=1,4,5,...,n P1=2P3=1,45...n
    • 综上 P 3 ! = 3 P_3 !=3 P3!=3 P 3 P_3 P3可能的取值为: n − 1 n-1 n1
  • 注:C语言标识符要求标识符的第一个字符必须是大小写英文字母或下划线,不能是数字

  • 顺序栈

    • 基本操作:
      顺序栈栈满指 s . t o p + 1 = = m a x s i z e s.top+1==maxsize s.top+1==maxsize
#include<stdio.h>
#include<stdlib.h>
#define Maxsize 50
typedef struct
{
	int data[Maxsize];
	int top;
 } Sqstack;
 void init(Sqstack &s)  //初始化 
 {
 	s.top=-1;
 }
 bool empty(Sqstack s) //判断是否为空 
 {
 	if(s.top==-1)
 		return true;
 	else
 		returen false;
  } 
  bool enter(Sqstack &s,int x) //进栈 
  {
  	if(s.top==Maxsize-1)
  		return false;
  	else
  	{
  		s.top++;
  		s.data[s.top]=x;
	  }
  }
  bool delete_(Sqstack &s)  //出栈 
  {
  	if (s.top==-1)
  		return false;
  	else
  	{
  		s.top--;
  		int x=s.data[s.top];
  		printf("%d\n",x);
  		return true;
	  }
  }
  void read (Sqstack s) //读栈 
  {
  	if (s.top==-1)
  	{
  		printf("栈为空\n");
  		return 0;
	  }
	else
	{
		int x=s.data[s.top];
		printf("栈顶元素:%d\n",x);
	} 	
  }

共享栈(上课时未讲过)

栈满指 t o p 0 + 1 = = t o p 1 top0+1==top1 top0+1==top1

#define maxsize 100
typedef struct
{
	int data[maxsize];
	int top0;//0号栈栈顶指针 
	int top1;//1号栈栈顶指针 
}shstack;

void init(shstack &s)  //初始化 
{
	s.top0=-1;
	s.top1=maxsize;
 } 
 void full(shstack s)   //是否满栈 
 {
 	if(s.top0+1==s.top1)
 		return true;
 	else
 		return false;
 }
 void read(shstack s)    //读取栈 
 {
 	if (s.top0==-1)
 		printf("0号栈为空");
	else
		{
			int x1=s.data[s.top0];
			printf("x1:%d\n",x1);
		 } 
 	if(s.top1==maxsize)
 		printf("1号栈为空");
 	else
 	{
 		int x2=s.data[s.top1];
 		printf("x2:%d\n",x2);
	 }
 }
 bool enter(shstack &s)   //进栈 
 {
 	if(s.top0+1==s.top1)
 	{
 		return false;
	 }
	 else
	 {
	 	printf("请输入元素:\n");
 		int m1;
		 m1=scanf("%d",&m1); 
	 	printf("请输入要进入的栈:0/1?\n");
	 	int x=-1;
	 	scanf("%d",&x);
	 	if(x==0)
	 	{
	 		s.top0++;
	 		s.data[s.top0]=m1;
		 }
		 else
		 {
		 	s.top1--;
		 	s.data[s.top1]=m1;
		 }
	 }
 }
  • 链栈
  • 操作与单链表类似。链栈中的进栈相当于利用头插法插入单链表,我这里的链栈无头结点。
#include<stdio.h>
#include<stdlib.h>
typedef struct Liknode
{
	int data;
	struct Liknode *next;
 }Liknode,*Listack;
void init(Listack &L)  //创建无头结点的链栈 
 {
 	int x;
 	printf("请输入x:\n");
 	scanf("%d",&x);
 	/*
 	L=(Listack)malloc(sizeof(Liknode));
 	L->next=NULL;
 	L->data=x;*/
	Listack p=NULL,s;
	  
	 while(x!=999)
 	{
 	s=(Listack)malloc(sizeof(Liknode));
 	s->next=p;
 	s->data=x;
 	p=s;
 	printf("请输入x:\n");
 	scanf("%d",&x);
	 }
	 L=s;
  }
  void delete_(Listack &L)  //出栈 
  {
  	Listack p=L;
  	L=L->next;
  	free(p);
   } 
  void print(Listack L)  //遍历 
  {
  	Listack p=L;
  	while(p!=NULL)
  	{
  		printf("%d    ",p->data);
  		p=p->next;
	  }
  }
  void destory(Listack &L)  //销毁栈 
  {
  	Listack p=L,r=p;
  	while(p!=NULL)
  	{
  		r=p->next;
  		free(p);
  		p=r;
	  }
  }
  int main()
  {
  	Listack L;
  	init(L);
  	print(L);
  	delete_(L);
  	printf("----=-----\n"); 
  	print(L);
  	destory(L);
  	return 0;
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值