Queue node
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):d Ata (Val), Next (NULL) {}};
Chained queues
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> *FR Ont 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 n Eed Let's 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;}};
Loop queue
Queue.h#include <assert.h> #define Max_size 10template<class t>class queue{int rear;int Front; T * Data;public:queue (): Front (0), rear (0) {data= new t[max_size];} empty bool IsEmpty () {return rear==front;} Judge full bool Isfull () {return (rear+1)%max_size==front;} queue bool Enquene (T val) {if (Isfull ()) return 0;//team full data[rear]=val;rear= (rear+1)%max_size;return 1;} T DeQueue () {if (IsEmpty ()) return-8888889;//team empty T val=data[front];front= (front+1)%max_size;return Val;} t& Getfront () {ASSERT (! IsEmpty ()); return Data[front];} void Traverse () {if (IsEmpty ()) cout<< "Queue is empty!\n"; Else{int pos=front;while (pos!=rear) {cout<<data[pos]< <endl;pos= (pos+1)%max_size;}}};
chain-Stack implementation Yang Hui triangle
/************************************************************************//* chain-Stack implementation Yang Hui triangle *//*********************** /#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<< "number of rows displayed (>=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"); }
Run-Length coding issues
/************************************************************************//* run-length coding problem *//************************** /#include "QueueList.h" void Main () {char A; queuelist<char> myqlist;cout<< "input 0,1 sequence, end with #: \ 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;}
Stack node
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) {}};
Chained stacks
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;}}};
Bracket Matching
/************************************************************************//* brackets Match *//**************************** /#include "Stack.h" void Main () {stack<char> Mystack;char a;cout << "Please enter the form of parentheses (end with 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;} elsecout<< "wrong!" <<endl;}
Data Structure Learning 2---queues and stacks