C++学习(21)

  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 }

猜你喜欢

转载自www.cnblogs.com/Tobi/p/9250561.html
今日推荐