zoukankan      html  css  js  c++  java
  • 【数据结构】队列和栈

    队列节点

    QueueNode.h
    #pragma once
    #include <iostream>
    using namespace std;
    template <class T>class QueueNode
    {
    public:
    	T data;
    	QueueNode<T>* next;
    	QueueNode():next(NULL){};
    	QueueNode(T val):data(val),next(NULL){}
    };
    

    链式队列

    QueueList.h
    #include "QueueNode.h"
    #include <assert.h>
    //no head node
    //when insert the first node ,you need let the front node point it.
    template <class T>class QueueList
    {
    public:
    	QueueNode<T> *front;
    	QueueNode<T> *rear;
    
    	QueueList():front(NULL),rear(NULL){};
    	
    	bool IsEmpty()
    	{
    		return front==NULL;
    	}
    	void EnQueue(T val)
    	{
    		QueueNode<T>* add=new QueueNode<T>(val);
    		if (IsEmpty())//when insert the first node ,you need let the front node point it.
    		{
    			front=rear=add;
    		}
    		else
    		{
    			rear->next=add;//when the head node exits,you just need change rear point.
     			rear=add;
    		}
    	}
    	T DeQueue()
    	{
    		assert(!IsEmpty());
    		QueueNode<T> *del=front;
    		T val=del->data;
    		front=front->next;
    		if (del==rear)
    		{
    			rear=NULL;
    		}
    		delete del;
    		return val;
    	}
    	void SetNull()
    	{
    		while (!IsEmpty())
    		{
    			DeQueue();
    		}
    	}
    	void Traverse()
    	{
    		QueueNode<T>* p=front;
    		while (p)
    		{
    			cout<<p->data<<" ";
    			p=p->next;
    		}
    	}
    	int GetFront()
    	{
    		if (IsEmpty())
    			return 0;
    		return front->data;
    	}
    };
    

    循环队列

    Queue.h
    #include <assert.h>
    #define MAX_SIZE 10
    template<class T>class Queue
    {
    	int rear;
    	int front;
    	T * data;
    public:
    	Queue():front(0),rear(0)
    	{
    		data= new T[MAX_SIZE]; 
    	}
    	
    	//判空
    	bool IsEmpty()
    	{
    		return rear==front;
    	}
    	//判断满
    	bool IsFull()
    	{
    		return (rear+1)%MAX_SIZE==front;
    	}
    	//入队
    	bool EnQuene(T val)
    	{
    		if (IsFull())
    			return 0;//队满
    		data[rear]=val;
    		rear=(rear+1)%MAX_SIZE;
    		return 1;
    	}
    	T DeQueue()
    	{
    		if (IsEmpty())
    			return -8888889;//队空
    		T val=data[front];
    		front=(front+1)%MAX_SIZE;
    		return val;
    	}
    	T& GetFront()
    	{
    		assert(!IsEmpty());
    		return data[front];
    	}
    	void Traverse()
    	{
    		if (IsEmpty())
    			cout<<"队列为空!
    ";
    		else
    		{
    			int pos=front;
    			while (pos!=rear)
    			{
    				cout<<data[pos]<<endl;
    				pos=(pos+1)%MAX_SIZE;
    			}
    		}
    	}
    };
    链式栈实现杨辉三角
    /************************************************************************/
    /* 链式栈实现杨辉三角 */
    /************************************************************************/
     #include "QueueList.h"
     #include <iostream>
    using namespace std;
    template <class T>
     void Assign(QueueList<T>&Org,QueueList<T>&iter)
     {
     	Org.SetNull();
     	while (!iter.IsEmpty())
     	{
     		T temp=iter.DeQueue();
     		Org.EnQueue(temp);
     	}
     }
     void main()
     {
     
     	int n;
     	cout<<"显示的行数(>=3):
    ";
     	cin>>n;
     	QueueList<int> Org;
     	Org.EnQueue(1);
     	Org.Traverse();
     	cout<<endl;
     	for (int i=1;i<n;i++)
     	{
     		QueueList<int> Iter;
     		Iter.EnQueue(1);
     		while (!Org.IsEmpty())
     		{
     			int OutQueue=Org.DeQueue();
     			Iter.EnQueue(OutQueue+Org.GetFront());
     		}
     		Iter.Traverse();
     		cout<<'
    ';
     		Assign(Org,Iter);
     	}
     system("pause");
    
     }


    游程编码问题

    /************************************************************************/
    /* 游程编码问题 */
    /************************************************************************/
    #include "QueueList.h"
    
    void main()
    {
    	char a;
    	QueueList<char> myQlist;
    	cout<<"输入0,1序列,以#结束:
    ";
    	while (cin>>a && a!='#')
    	{
    		myQlist.EnQueue(a);
    	}
    	int count=0;
    	char temp; 
    	char first=myQlist.GetFront();
    	while (!myQlist.IsEmpty())
    	{
    		temp=myQlist.DeQueue();
    		if (temp==first)
    		{
    			count++;
    		}
    		else
    		{
    			cout<<count;
    			count=1;
    		}
    		first=temp;
    	}
    	cout<<count;
    }
    


    栈节点

    StackNode.h
    #pragma once
    #include <stdlib.h>
    template<class T>class StackNode
    {
    public:
    	T data;
    	StackNode<T>* next;
    	StackNode(T value):next(NULL),data(value){}
    };
    

    链式栈

    Stack.h
    #include "StackNode.h"
    #include <iostream.h>
    template<class T> class Stack
    {
    private:
    	StackNode<T>* top;
    public:
    	Stack():top(NULL){}
    	void Push(T val)
    	{
    		StackNode<T>* add=new StackNode<T>(val);
    		add->next=top;
    		top=add;
    	}
    	T Pop()
    	{
    		if (IsEmpty())
    			return -1;
    		StackNode<T>* del=top;
    		top=top->next;
    		T data=del->data;
    		delete del;
    		return data;
    	}
    	bool IsEmpty()
    	{
    		return top==NULL;
    	}
    	T GetTop()
    	{
    		return top->data;
    	}
    	void MakeEmpty()
    	{
    		StackNode<T>* del=top;
    		while (top)
    		{
    			top=top->next;
    			delete top;
    		}
    	}
    	void TraverseStack()
    	{
    		StackNode<T>* p=top;
    		while (p)
    		{
    			cout<<p->data<<endl;
    			p=p->next;
    		}
    	}
    };
    
    括号匹配
    /************************************************************************/
    /* 括号匹配 */
    /************************************************************************/
    #include "Stack.h"
    void main()
    {
    	Stack<char> myStack;
    	char a;
    	cout<<"请输入括号形式(以0结束)
    ";
    	while (cin>>a&&a!='0')
    	{
    		switch (a)
    		{
    		case '(' :
    			myStack.Push(a);
    			break;
    		case ')' :
    			if (!myStack.IsEmpty())
    			{	
    				myStack.Pop();
    				break;
    			}
    		}
    	}
    	if (myStack.IsEmpty())
    	{
    		cout<<"Ok!"<<endl;
    	}
    	else
    			cout<<"Wrong!"<<endl;
    }
    
  • 相关阅读:
    LINUX用户管理——/etc/passwd文件详解
    接口类型的问题:信息丢失
    swift是强类型语言
    swift的多态
    面向对象的本质:基于对象+面向接口+继承
    多态的本质:同一函数接口在接受不同的类型参量时表现出不同的行为--多态是面向接口编程
    类型约束的本质是:类型构造器在约束类型的基础上构造新的函数类型
    类型与函数:函数是一种复合类型,它的构建依赖于数据类型
    类型约束的语义学研究:基于类型约束的编程
    复合类型、类型约束、添加功能、高阶函数
  • 原文地址:https://www.cnblogs.com/qhyuan1992/p/5385327.html
Copyright © 2011-2022 走看看