3)链栈和链队列
鏈棧:
1 #include<iostream> 2 using namespace std; 3 struct node{ 4 int data; 5 node *next; 6 }; 7 enum error{underflow,overflow,success,fail}; 8 class stack{ 9 public: 10 stack();//初始化 11 ~stack();//定義析構函數,以在需要時自動釋放鏈棧空間 12 bool empty() const;//判斷為空 13 bool full() const;//判斷為滿 14 int get_top(int &x)const;//取棧頂元素 15 int push(const int x);//入棧 16 int pop();//出棧 17 private: 18 int count;//棧中數據的個數 19 int data;//棧中數據 20 node *top; 21 }; 22 /*初始化:棧頂指針置為空,計數變量設置為0*/ 23 stack::stack(){ 24 count=0; 25 top=NULL; 26 } 27 28 /* 29 *判斷棧是否為空:count=0||top=NULL 30 */ 31 bool stack::empty()const{ 32 return count==0;//return top==NULL; 33 } 34 35 /* 36 *取棧頂元素的實現,若棧不為空,返回棧頂元素的值,否則返回出錯信息 37 */ 38 int stack::get_top(int &x)const{ 39 if(empty())return underflow; 40 x=top->data; 41 return success; 42 } 43 44 /* 45 *入棧 46 */ 47 int stack::push(const int x){ 48 node *s=new node; 49 s->data=x; 50 s->next=top; 51 top=s; 52 count++; 53 return success; 54 } 55 56 /* 57 出棧 58 */ 59 int stack::pop(){ 60 if(empty())return underflow; 61 node *u=new node; 62 u=top; 63 top=u->next; 64 delete u; 65 count--; 66 return success; 67 } 68 69 stack::~stack(){ 70 while(!empty())pop(); 71 } 72 73 int xchg(int n,stack s){ 74 cout<<"十進制:["<<n<<"]->8進制:"; 75 int mod,x; 76 while(n!=0){ 77 mod = n % 8; 78 s.push(mod); 79 n/=8; 80 } 81 while(s.empty()!=true){ 82 s.get_top(x); 83 cout<<x; 84 s.pop(); 85 } 86 cout<<endl; 87 return 0; 88 } 89 int main() 90 { 91 stack s; 92 xchg(100,s); 93 return 0; 94 } 95鏈隊列:
1 #include<iostream> 2 #include<iomanip> 3 using namespace std; 4 5 enum error{underflow,overflow,success}; 6 7 struct node{ 8 int data; 9 node *next; 10 }; 11 12 class queue{ 13 public: 14 queue(); 15 ~queue();//定義析構函數,以在需要時自動釋放鏈隊列空間 16 bool empty() const;//判斷為空 17 bool full() const;//判斷為滿 18 int get_front(int &x )const;//取隊頭元素 19 int append(const int x);//入隊 20 int serve();//出隊 21 private: 22 int count; 23 node *front,*rear; 24 }; 25 26 queue::queue(){//初始化隊列 27 front =new node; 28 rear =new node; 29 //node *rear = new node; 30 count=0; 31 rear = front; 32 front->next=NULL; 33 rear->next=NULL; 34 } 35 36 bool queue::empty()const{//判斷為空 37 if(count==0)return true; 38 else return false; 39 // return count==0; 40 } 41 42 int queue::get_front(int &x)const{//取隊頭元素 43 if(empty())return underflow; 44 else{ 45 x=front->next->data; 46 return success; 47 } 48 } 49 50 int queue::append(const int x){//入隊 51 node *s= new node; 52 s->data=x; 53 s->next=NULL; 54 rear->next=s; 55 rear=s; 56 count++; 57 return success; 58 } 59 60 int queue::serve(){//出隊 61 node *u =new node; 62 if(empty())return underflow; 63 else{ 64 u=front->next; 65 front->next=u->next; 66 delete u; 67 count--; 68 } 69 if(front->next==NULL)rear=front;//如果刪除的是最后一個結點,尾指針rear指向了一個已經刪除的節點 70 return success; 71 } 72 73 queue::~queue(){ 74 while(!empty()) serve(); 75 delete front;//最后釋放頭結點 76 } 77 78 int main(){ 79 queue q; 80 int n; 81 cout<<"please input 楊輝三角要打印的行數:"; 82 cin>>n; 83 int s1,s2; 84 for(int i=1;i<n;i++)cout<<" "; 85 cout<<1<<endl;//輸出第一行上的1 86 q.append(1);//所輸出1入隊 87 for(int i=2;i<=n;i++){//逐行計算并輸出2~N行上的數據 88 s1=0;//存放前一個入隊數 89 for(int k=1;k<=n-i;k++ )cout<<" "; 90 for(int j=1;j<=i-1;j++){//先計算并輸出n-1個數 91 q.get_front(s2);//取隊頭元素并出隊 92 q.serve(); 93 cout<<s1+s2<<setw(4); 94 q.append(s1+s2);//所輸出的當行中的元素入隊 95 s1=s2; 96 } 97 cout<<1<<endl;//輸出當行中的子最后一個元素1并換行 98 q.append(1); 99 } 100 return 0; 101 }?
轉載于:https://www.cnblogs.com/minmsy/p/5022031.html
總結
- 上一篇: 升级到win10,安装visualstu
- 下一篇: Attachments to close