1. 程式人生 > >C++學習(21)

C++學習(21)

AC str exit 節點類 節點 lib col 元素 越界

  1 //鏈式堆棧
  2 //先設計一個帶頭結點的單鏈表類,再設計一個帶頭結點的鏈式堆棧類
  3 //要求帶頭結點的鏈式堆棧類利用帶頭結點的單鏈表類的代碼資源
  4 #include<iostream.h>
  5 #include<stdlib.h>
  6 
  7 //節點類
  8 class ListNode{
  9     friend class LinList;
 10     private:
 11         ListNode *next;
 12         float data;
 13     public:
 14         ListNode(ListNode * ptrNext=NULL){
15 next=ptrNext; 16 } 17 ListNode(const float &item,ListNode * ptrNext=NULL){ 18 data=item; 19 next=ptrNext; 20 } 21 }; 22 23 //單鏈表類 24 class LinList{ 25 private: 26 ListNode *head;//頭指針 27 int size;//節點個數 28
void ClearList();//清空鏈表 29 ListNode * Index(int pos)const;//返回指向第pos個節點的指針 30 public: 31 LinList(); 32 ~LinList(); 33 34 int ListSize()const;//獲取節點的個數 35 int ListEmpty()const;//判斷鏈表是否為空 36 void Insert(const float &item,int pos);//插入一個節點 37
float Delete(int pos);//刪除一個節點 38 float GetData(int pos)const;//獲取第pos個節點的data值 39 }; 40 41 LinList::LinList(){ 42 head=new ListNode;//頭指針指向頭結點 43 size=0;//初始化節點個數為0 44 } 45 46 LinList::~LinList(){ 47 ClearList(); 48 delete head; 49 } 50 51 //清空鏈表 52 void LinList::ClearList(){ 53 ListNode *p,*p1; 54 p=head->next; 55 while(p!=NULL){ 56 p1=p; 57 p=p->next; 58 delete p1; 59 } 60 size=0; 61 } 62 63 //返回指向第pos個節點的指針 64 ListNode * LinList::Index(int pos)const{ 65 if(pos<-1 || pos>size ){ 66 cout<<"參數pos越界出錯!"<<endl; 67 exit(0); 68 } 69 70 if(pos==-1){ 71 return head; 72 } 73 74 ListNode *p=head->next; 75 int i=0; 76 while(p!=NULL && i<pos){ 77 p=p->next; 78 i++; 79 } 80 return p; 81 } 82 83 //獲取節點的個數 84 int LinList::ListSize()const{ 85 return size; 86 } 87 88 //判斷鏈表是否為空 89 //1為空 0不為空 90 int LinList::ListEmpty()const{ 91 if(size<=0){ 92 return 1; 93 } 94 return 0; 95 } 96 97 //插入一個節點 98 void LinList::Insert(const float &item,int pos){ 99 ListNode *p=Index(pos-1); 100 ListNode *newNode=new ListNode(item,p->next); 101 p->next=newNode; 102 size++; 103 } 104 105 //刪除一個節點,並且返回這個被刪除的節點的值 106 float LinList::Delete(int pos){ 107 if(size==0){ 108 cout<<"鏈表為空,沒有元素可以刪除!"<<endl; 109 exit(0); 110 } 111 ListNode *p=Index(pos-1),*q; 112 q=p->next;//q指向第pos個節點 113 p->next=p->next->next; 114 float data=q->data;//獲取第pos個節點的data值 115 size--; 116 delete q; 117 return data; 118 } 119 120 //獲取第pos個節點的data值 121 float LinList::GetData(int pos)const{ 122 ListNode *p=Index(pos); 123 return p->data; 124 } 125 126 //帶頭結點的鏈式堆棧類 127 class LinStack:private LinList{ 128 public: 129 LinStack():LinList(){} 130 ~LinStack(){} 131 132 int StackSize()const{ 133 return ListSize(); 134 } 135 136 int StackEmpty()const{ 137 return ListEmpty(); 138 } 139 140 void Push(const float &item){ 141 Insert(item,0); 142 } 143 144 float Pop(){ 145 return Delete(0); 146 } 147 148 float GetTop()const{ 149 return GetData(0); 150 } 151 152 }; 153 int main(){ 154 LinStack myStack; 155 float i; 156 cout<<"初始化元素個數:"<<myStack.StackSize()<<endl; 157 for(i=1;i<=5;i++){ 158 myStack.Push(i); 159 } 160 161 cout<<"入棧後的元素個數:"<<myStack.StackSize()<<endl; 162 cout<<"依次出棧的元素: "; 163 for(i=1;i<=5;i++){ 164 cout<<myStack.Pop()<<" "; 165 } 166 cout<<endl; 167 cout<<"結束元素個數: "<<myStack.StackSize()<<endl; 168 169 return 0; 170 }

技術分享圖片

C++學習(21)