資料結構學習2---隊列和棧,資料結構學習2---

來源:互聯網
上載者:User

資料結構學習2---隊列和棧,資料結構學習2---

隊列節點

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 10template<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;}elsecout<<"Wrong!"<<endl;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.