Data Structure Learning 2 --- queue and stack, data structure learning 2 ---

Source: Internet
Author: User

Data Structure Learning 2 --- queue and stack, data structure learning 2 ---

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):data(val),next(NULL){}};

Chain queue

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;}};

Cyclic queue

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];} // empty bool IsEmpty () {return rear = front;} // judge whether bool IsFull () is full () {return (rear + 1) % MAX_SIZE = front;} // enter bool EnQuene (T val) {if (IsFull () return 0; // full team data [rear] = val; rear = (rear + 1) % MAX_SIZE; return 1 ;}t DeQueue () {if (IsEmpty () return-8888889; // empty team T val = da Ta [front]; front = (front + 1) % MAX_SIZE; return val;} T & GetFront () {assert (! IsEmpty (); return data [front];} void Traverse () {if (IsEmpty () cout <"the 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
/*************************************** * ******************************* // Yang Hui implements the chain Stack 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 lines 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 ");}


Travel code Problems

/*************************************** * ********************************** // The program encoding problem * //************************************** * *********************************/# include "QueueList. h "void main () {char a; QueueList <char> myQlist; cout <" input sequence 0, 1, end with #: \ n "; while (cin> 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){}};

Chain Stack

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;}}};
Matching brackets
/*************************************** * ******************************* // Matching brackets */ /*************************************** * ********************************/# include "Stack. h "void main () {Stack <char> myStack; char a; cout <" Enter brackets (ended with 0) \ n "; while (cin> 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.