数据结构三——栈与队列

在这里插入图片描述

顺序栈

在这里插入图片描述

//入栈
int push(int s[],int x)
{
    if (top==M)
    {
        printf("overflow");
        return(-1);
    }
    s[top++]=x;
    return top;
}

//出栈
int pop(int s[], int *q){
   if (top==0)
   {
        printf(“underflow");
        return(-1);
    }
    *q=s[--top];
    return top;
}

在这里插入图片描述

链栈

typedef struct node
{
     int data;
     struct  node  *link;
}JD;

void push(int x)
{
   JD *p = (JD *)malloc(sizeof(JD));
   p->data=x;  
   p->link=top;
   top=p;
}

在这里插入图片描述
在这里插入图片描述

int Match_Brackets()
{
	SqStack  S ;   
	S=Init_Stack() ;  /*堆栈初始化*/
    char ch = 0;    
    scanf(%c” , &ch) ;
	while (asc(ch)!=13)
	{
   		if  ((ch==() || (ch==[))  
   			push(S , ch) ; 
		else if  (ch==]) 
		{
	  		char x=pop(S) ;  
	  		if (x !=[) 
	  			return -1; 
			else if  (ch==)) 
			{
	  			char x=pop(S) ; 
	  			if (x !=()  
	  				return -1;
			} 
			scanf(%c” , &ch) ;
		}
		if  (S.top!=0) 
			return -1; 
		else 
			return 0; 
	}

栈在递归调用中的应用

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

例子

//例1:最大公约数的递归实现
//程序1(非递归实现):
int gcd(int a, int b)
{
    while (a != 0)
    {
         int c = b % a;
         b = a; 
         a = c;
    }
    return b;
}
//程序2(递归实现)
int gcd(int a, int b)
{
    if (a==0) return b;
    else return gcd(b%a, a);
}

//n阶Hanoi塔问题
void hanoi (int n, char A, char B, char C) 
{
   if (n==1)
        move(A, 1, C);   // 将编号为1的圆盘从x移到z
    else {
        hanoi(n-1, A, C, B);  // 将A上编号为1至n-1的圆盘移到B,  C作辅助塔
        move(A, n, C);     // 将编号为n的圆盘从A移到C
        hanoi(n-1, B, A, C);  // 将B上编号为1至n-1的圆盘移到C, A作辅助塔
    }
}

//Gray码的构造


队列

在这里插入图片描述
##链队列
在这里插入图片描述
在这里插入图片描述

队列的顺序存储

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

//入队
int en_cycque(int sq[], int front, int rear, int x)
{
  if (((rear+1)%M)==front)
      return -1;
   else
   {
       sq[rear]=x; 
       rear=(rear+1)%M;
       return 0;
   }
}

//出队
int dl_cycque(int sq[], int front, int rear, int *q)
{
    if(front==rear)
       return -1;
    else
    {
         *q=sq[front]; 
         front=(front+1)%M;
         return 0;
     }
}

双端队列

在这里插入图片描述

练习

1:递归输出1. .n的所有排列

int a[MAXN+1];
bool mark[MAXN+1];  // all equal to false initially
Void searchPermutation(int i)
{
     if (i == 0) 
     	PrintList(a);
     else 
        for (int j = 1; j <= n; j++)
            if (!mark[j])
            {
            	mark[j] = true; a[i] = j;
               	searchPermutation(i - 1);
               	mark[j] = false;
       		}
}
searchPermutation(n);

2:关于FILO

在这里插入图片描述

// 1 
#include<stdio.h>

int main() 
{
    char s, Q[8], x = 'A';  
    int count = 0, top = 0;
    scanf("%c", &s);
    while (count < 8) 
    {
		if (top > 0 && Q[top-1] == s)
		{
			top--; 
			scanf("%c", &s); 
			count++; 
        }
		else if (x <= 'H')	
		{
			Q[top] = x;
			top++;
			x++;
		}
		else 
		{
			printf("0"); 
			return 0;
		}
    }
	printf("1"); return 0;
}

// another 1
#include<stdio.h>
using namespace std;

int main() 
{
    char s[8];
    scanf("%s", s);
    int ok = 1;
    int i = 0; 
    int top = -1;
    char line[8];
    char c = 'A';
    while (i < 8) 
    {
        if (top < 0) 
        	line[++top] = c++;
        while (line[top] != s[i]) 
        {
            if (c > 'H') 
            {
                ok = 0;
                break;
            }
            line[++top] = c++;
        }
        if (!ok) break;
        while (top >= 0 && i < 8 && line[top] == s[i])   
        {
            --top; 
            ++i;
        }
    }
    if (ok) printf("%d\n", 1);
    else printf("%d\n", 0);
	
	return 0;
}

3:Gray码的构造

 void graycode(int n, int a[])
 {
 	if(n==1)
 	{
 		a[0]=0; 
 		a[1]=1;
 		return;
	 }
	 graycode(n-1,a);
	 int L = 1<<(n-1);   // 得到2^(n-1)
	 for (int i = 0; i < L; i++)  a[L + L - i - 1] = a[i] + L;
 }

4:循环队列入队出队

#include <stdio.h>

int main()
{
	    int m,n;
	    scanf("%d%d", &m, &n);
	    getchar();
	    char que[m];
		int front = 0;
		int rear = 0;
	    for(int i = 0; i < n; i++)
	    {
	    	char a[5];
	    	gets(a);
	        if(a[0] == '1')
	        {
			    que[rear]=a[2]; 
			    rear=(rear+1)%m;
		    }
		    else 
		    	front=(front+1)%m;
	        printf("%d %d\n", front, rear);
	    }
	    while (front != rear)
	    {
	    	printf("%c",que[front]);
			front = (front + 1)%m;
		}
    return 0;
}

思考:FILO的forbidden pattern

在这里插入图片描述
挖坑挖坑

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值