王道数据结构第三章


//链栈操作实现——创增删查
typedef struct Lnode
{
	Elemtype data;
	struct LNode *next;
}stack;
void add(stack *l,Elemtype x)//入栈
{
	LNode *h=l,*p;
	p->data=x;
	p->next=l->next;
	l->next=p;
}
bool StackPop(Stack l) //出栈
{
	if (l->next == NULL)
	{
		return 0;
	}
	else
	{
		lNode *temp=l->next;
		l->next=l->next->next;
		print(temp->data);
		free(temp);
	}
	return 1;
}
Elemtype StackPop(Stack l)//查询
{
	if (l->next == NULL)
	{
		return 0;
	}
	else
	{
		return l->next->data;
	}
}

//3
int judge(char a[])
{
	int i=0;
	int j=0,k=0;
	while(a[i]!='\0')
	{	
		switch(a[i])
		{
			case'I':j++;
			case'O':k++;
			if(k>j)
			{
				print("非法");
				break;
			}
		}
		i++;
	}
	if(j!=k)
	{
		print("非法");
		return false;
	}
	else
		print("合法");
	return true;
}

//4
bool compareyxxy(Linklist &l,int n)
{
	int i;
	int a[n/2];
	lNode *p=l->next;
	for(i=0;i<=n/2;i++)
	{
		a[i]=p->data;
		p=p->next;
	}
	i--;
	if(n%2=1)
		p=p->next;
	while(p!=NULL&&s[i]==p->data)
	{
		i--;
		p=p->next;
	}
	if(i=-1)
		return 1;
	else 
		return 0;
}

//5
#define maxsize 100
#define Elemtype int
typedef struct stack
{
	Elemtype s[maxsize];
	int top[2];
}stack;
int push(int i,Elemtype x)
{
	if(i!=0&&i!=1)
	{
		print("输入error");
		exit(0);
	}
	if(stack.top[0]+1=stack.top[1])
	{
		print("满了");
		return 0;
	}
	switch(i){
		case 0:stack.s[++stack.top];return 1;break;
		case 1:stack.s[--stack.top];return 1;
	}
}
Elemtype pop(int i)
{
	if(i!=0&&i!=1)
	{
		print("输入error");
		exit(0);
	}
	switch(i){
		case 0:
			if(stack.top[0]==-1)
			{
				print("空");
				return -1;
			}
			else
				return stack.s[stack.top[0]--];
			case 1:
			if(stack.top[1]==maxsize)
			{
				print("满");
				return -1;
			}
			else
				return stack.s[stack.top[1]++];
	}//switch
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值