【数据结构】队列和栈
队列节点
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<<"队列为空!\n"; 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):\n"; 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<<'\n'; Assign(Org,Iter); } system("pause"); }
游程编码问题
/************************************************************************/ /* 游程编码问题 */ /************************************************************************/ #include "QueueList.h" void main() { char a; QueueList<char> myQlist; cout<<"输入0,1序列,以#结束:\n"; 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结束)\n"; 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; }