[Pi Pilei] Data structure machine two: the application of stack and queue

【Pi Pilei】Computer 2: Application of Stack and Queue

Content highlights

(1) Use the stack to convert between decimal numbers and N-ary (such as binary, octal, hexadecimal) data;
(2) Use the array q[m] to store the circular queue, which has only one queue head pointer front , No queue tail pointer, but a counter count to record the number of elements in the queue. Realize the empty, enqueue and dequeue operations of the circular queue.

Source code (1)

Use the stack to convert between decimal numbers and N-base (such as binary, octal, hexadecimal) data

#include<iostream>//栈和队列的应用:链栈进制转换十->n
using namespace std;
typedef class snode
{
    
    
public:
	int data;
	class snode *next;
};
class linkstack
{
    
    
public:
	snode *top;
    void initstack()//构造空栈
	{
    
    
		top=NULL;
    }
    void push(int x)//元素x入栈
    {
    
    
		snode *p;
		p=new snode;
		p->data=x;
		p->next=top;
		top=p;
      }
	int pop(int &x)//出栈,同时用x保存栈顶元素值
	{
    
    
		snode *p;
		if(top==NULL)
			return 0;
		x=top->data;
		p=top;
		top=top->next;
		delete p;
		return 1;
	}
	int gettop()//返回栈顶元素,不修改栈顶指针
	{
    
    	
		if(top!=NULL)
				return top->data;
		else	
			return -1;
	}
    void out()
    {
    
    
		int x;//可以暂存栈顶元素
		while(top!=NULL)
		{
    
    
			cout<<gettop();
			pop(x);//出栈
		}
		cout<<endl;
      }
	void transform()
	{
    
    
		int num,r,k;
		cout<<"输入要转换的数字,进制:";
		cin>>num>>r;
		cout<<endl;
		while(num!=0)
		{
    
    
			k=num%r;
			push(k);
			num=num/r;
		}
	}
};
int main()
{
    
    
	int n,x;
	linkstack m;
	m.initstack();//初始化链栈
	m.transform();//进制转换
	m.out();
}

Source code (2)

Use the array q[m] to store the circular queue. The queue has only one queue head pointer front, and no queue tail pointer, but a counter count to record the number of elements in the queue. Realize the empty, enqueue and dequeue operations of the circular queue.

#include<iostream>
using namespace std;
template <class T>
class hhh
{
    
    
private:
	int maxsize,front,count;
	T *a;//T类型的数组,a为数组名
public:
	hhh(int size)//构造函数,初始化
	{
    
    
		maxsize=size;
		front=count=0;
		a=new T[maxsize];//t类型
	}
	~hhh()
	{
    
    
		if(a)
		{
    
    
			delete []a;//释放申请的内存空间
			a=NULL;//释放a指针
		}
	}
	void empty()//判空
	{
    
    
		if(count==0)
		{
    
    
			cout<<"循环队列为空"<<endl;
		}
		else
		{
    
    
			cout<<"队列中存在元素"<<endl;
		}
	}
	void in(T x)
	{
    
    
		int r;
		if(maxsize==count)
		{
    
    
			cout<<"循环队列上溢"<<endl;
		}
		else
		{
    
    
			count++;
			r=(front+count)%maxsize;
			a[r]=x;
		}
	}
	void out(int x)
	{
    
    
		if(count==0)
		{
    
    
			cout<<"循环队列下溢"<<endl;
		}
		else
		{
    
    
			front=(front+1)%maxsize;
			x=a[front];
			cout<<"本次出栈元素为:"<<x<<endl;
			count--;//出栈
		}
	}
	void show()//输出队列中所有元素
	{
    
    
		if(count==0)
			cout<<"队列为空"<<endl;
		else
		{
    
    
			cout<<"全部元素:";
		    int num=count,f=front;
			while(num>0)
			{
    
    
				f=(f+1)%maxsize;
				cout<<a[f]<<' ';
				num--;
			}
			cout<<endl;
		}	
	}
};
int main()
{
    
    
	int num,x,a;
	cout<<"输入队列最大容量:";cin>>num;
	hhh<int> A(num);
	cout<<endl;
	cout<<"操作选择:1--判空,2--入队,3--出队,4--查看队中元素,0--退出"<<endl;
	while(cin>>x&&x)
	{
    
    
		switch(x)
		{
    
    
		case 1:A.empty();break;
		case 2:
			cout<<"输入入队元素";cin>>a;
			A.in(a);
			break;
		case 3:
			int m;
			A.out(m);
			break;
		case 4:
			A.show();
			break;
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_43704702/article/details/103745509