zoukankan      html  css  js  c++  java
  • 数据结构——栈与队列操作(用栈模拟队列)

    【栈与队列操作】
    问题描述:假设有两个长度相同的栈 S1,S2,已知以下入栈、出栈、判栈满和判栈空操作:
    void Push(S,x);
    Elemtype Pop(S);
    bool StackFull(S);
    bool StackEmpty(S);
    现用这两个栈构成一个队列,实现入队列、出队列操作的算法:
    bool EnQueue(x);
    Elemtype DeQueue(S);

    要求:
    (1)设栈空间大小MaxSize=10,队列的数据通过调用算法initRandomize(int *arr, int n, int min, int max)随机产生。
    (2)测试环节要包含出队列"空"和入队列"满"的情况出现。

    知识点:
    1.静态栈的基本算法(因为简单,自定义函数短小,总觉得遗漏了些什么)
    2.用栈来模拟队列(这里就没定义队列了,栈S1就当作时队列了,出队列的时候借用栈S2来倒序输出)
    3.关于initRandomize(int *arr, int n, int min, int max)的使用
    4.还有就是Sleep()、memset()的使用

    自己基于【数据结构】【严蔚敏】版敲的
    (网上应该会有许多更优的思路)
    直接来完整代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <windows.h>
    #define OK 1
    #define ERROR 0
    #define OVERFLOW -1
    #define TRUE 1
    #define FALSE 0
    #define  MAXSIZE 10           //栈的大小和队列的大小都是 10 
    
    typedef int Elemtype; 
    typedef  struct Stack {
    	int    data[MAXSIZE];	        
    	int         top;        	// 栈顶指针
    } SqStack;
    
    #include <time.h>  //time(0)需要的头文件 (代表当前时间自标准时间戳(1970年1月1日0点0分0秒,GMT)经过了多少秒) 
    void initRandomize(int *arr, int n, int min, int max)
    {
        int i = 0;
        srand(time(0));  			/*设置种子,并生成伪随机序列*/
        for (i = 0; i < n; ++i) {
            arr[i] = rand()% (max - min + 1) + min;  /*得到从[min, max]之间的随机数*/
            printf("%d ", arr[i]);
        }
        printf("
    ");
    
    }
    
    int InitStack(SqStack &S)  //初始化静态栈 
    {
    	S.top=0;
    }
    
    int Push(SqStack &S,int e)
    {
    	if(S.top>=MAXSIZE) return ERROR;
    	S.data[S.top++]=e;
    	return OK;
    }
    
    
    int Pop(SqStack &S,int &e)
    {
    	if(S.top == 0)  return ERROR;
    	e=S.data[--S.top];//e=*(S.data+S.top-1)
    	return OK;
    }
    
    int Empty(SqStack &S)
    {
    	if(S.top == 0)  return 1;
    	else return 0;
     } 
     
    int Full(SqStack S)
    {
    	if(S.top == MAXSIZE)  return 1;
    	else return 0;
    }
    
    SqStack S1;	
    SqStack S2;
    int EnQueue(int x)//进入队列 
    {
    	if(Full(S1))
    	{
    		printf("队列已满
    ");
    	}
        else
    	Push(S1,x);
    	
    }
    Elemtype DeQueue()//出队列 
    {
    	if(Empty(S1))
    	{
    		printf("队列为空
    ");
    	}
    	InitStack(S2);
    	int e;
    	while(!Empty(S1))
    	{
    		Pop(S1,e);
    		Push(S2,e);	
    	}
    	while(!Empty(S2))
    	{
    		Pop(S2,e);
    		printf("%d ",e);
    	}
    	printf("
    
    
    ");
    
    }
    
    int EmptyStack(SqStack &S) //判断栈是否为空 
    {
    	if(Empty(S1))  return 1;
    	else 0;
    }
    
    void test1() 
    {
    	int a[10],i;	
    	initRandomize(&a[0], 10, 1, 1002);//生成15个随机数         
    	InitStack(S1); 
    	for(i=0;i<10;i++)   //溢出与不溢出关键看这里读入了几个数,栈的大小等于队列的大小 
    	{
    		EnQueue(a[i]);
    	}
    	printf("输出队列为:
    "); 
    	DeQueue();
    	//memset(a,0,sizeof(int)*10);
    }
    
    void test2() 
    {
    	int a[10]={1,2,3},i;	
        initRandomize(&a[3], 10, 1, 1002);        
    	InitStack(S1); 
    	for(i=0;i<11;i++)   //溢出与不溢出关键看这里读入了几个数,栈的大小=队列的大小=MAXSIZE=10 
    	{
    		EnQueue(a[i]);
    	}
    	if(i<=10)
    	{
    		printf("输出队列为:
    "); 
    		DeQueue();
    	}
    	//memset(a,0,sizeof(int)*10);
    	//printf("%d %d",a[0],*(a+3));
    }
    
    void test3() 
    {
    	int a[10],i;	
    	initRandomize(&a[0], 10, 1, 1002);       
    	InitStack(S1); 
    	for(i=0;i<0;i++)   //溢出与不溢出关键看这里读入了几个数,栈的大小等于队列的大小 
    	{
    		EnQueue(a[i]);		
    	}
    	if(!Empty(S1))
    	{
    		printf("输出队列为:
    "); 
    		DeQueue();
    	}
    	else
    	printf("队列为空"); 
    }
    
    int main()
    {
    	test1();	//正常情况 
    	Sleep(1000);//暂停1秒,变化种子点 
    	test2();	// 溢出  
    	test3();    // 为空 
    }
    
    
    
    
  • 相关阅读:
    Python使用SMTP模块、email模块发送邮件
    harbor搭建及使用
    ELK搭建-windows
    ELK技术栈之-Logstash详解
    【leetcode】1078. Occurrences After Bigram
    【leetcode】1073. Adding Two Negabinary Numbers
    【leetcode】1071. Greatest Common Divisor of Strings
    【leetcode】449. Serialize and Deserialize BST
    【leetcode】1039. Minimum Score Triangulation of Polygon
    【leetcode】486. Predict the Winner
  • 原文地址:https://www.cnblogs.com/vivid-victory/p/10090473.html
Copyright © 2011-2022 走看看