Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #ifndef STACK_LIST_H
  2. #define STACK_LIST_H
  3. #include <iostream>
  4. using namespace std;
  5. struct Stack_struct
  6. {
  7. Stack_struct *next;
  8. float number;
  9.  
  10. };
  11.  
  12. template <typename T> class Stack_list
  13. {
  14. Stack_struct *head;// the peak of our list
  15. int length;
  16. public:
  17. Stack_list(){ head=0; length=0;}
  18. Stack_list(int num);
  19. Stack_list(const Stack_list<T> &stL); // copy constructer
  20. ~Stack_list()
  21. {
  22. while (head!=NULL)
  23. {
  24. Stack_struct *temphead;
  25. temphead=head;
  26. head=head->next;
  27. delete temphead;
  28. }
  29. }
  30. void add_list(Stack_struct *&head, float number);
  31. float get_last(Stack_struct *&head);
  32. Stack_struct* get_head()const{return head;}
  33. void add_toliststruct(float addnum);
  34. float getelement();
  35. bool Empty_list(){return head==NULL ;}
  36. int get_length(){return length;}
  37. template<typename P> friend void operator<<(ostream &out, const Stack_list<T> &L);
  38. void Print_list();
  39. Stack_list<T> &operator=(const Stack_list<T> &stList);
  40.  
  41. };
  42.  
  43. template<typename T>
  44. Stack_list<T>:: Stack_list(const Stack_list<T> &stL)
  45. {
  46. head=new Stack_struct;
  47. Stack_struct *head1=stL.get_head();
  48. length=stL.length;
  49. head->number=head1->number;
  50. head->next=0;
  51. Stack_struct *a=head, *p;
  52. for (int i=1; i<length; ++i)
  53. {
  54. p=new Stack_struct;
  55. head1=head1->next;
  56. p->number=head1->number;
  57. a->next=p;
  58. p->next=0;
  59. a=p;
  60. }
  61.  
  62.  
  63. }
  64. template<typename T>
  65. Stack_list<T>:: Stack_list(int num)
  66. {
  67. length=num;
  68. head=new Stack_struct;
  69. cin>>head->number;
  70. head->next=0;
  71. Stack_struct *a=head,*p;
  72. for (int i=1; i<num; ++i)
  73. {
  74. p=new Stack_struct;
  75. cin>>p->number;
  76. a->next=p;
  77. p->next=0;
  78. a=p;
  79.  
  80. }
  81.  
  82.  
  83. }
  84. template<typename T>
  85. void Stack_list<T>:: Print_list()
  86. { if (head!=NULL)
  87. {
  88. Stack_struct *p=head;
  89. while(p)
  90. {
  91. cout<<p->number<<" ";
  92. p=p->next;
  93. }
  94. }
  95. }
  96. template<typename T>
  97. void Stack_list<T>:: add_toliststruct(float addnum)
  98. {
  99. length=length+1;
  100. add_list(head,addnum);
  101. }
  102.  
  103. template<typename T>
  104. float Stack_list<T>:: getelement()
  105. {
  106. length=length-1;
  107. return get_last(head);
  108. }
  109.  
  110. template<typename T>
  111. void operator<<(ostream &out, const Stack_list<T> &L)
  112. {
  113. Stack_struct *temphead;
  114. temphead=L.get_head();
  115. while (temphead!=NULL)
  116. {
  117. out<<temphead->number;
  118. out<<' ';
  119. temphead=temphead->next;
  120. }
  121. }
  122. template<typename T>
  123. Stack_list<T> &Stack_list<T>:: operator=(const Stack_list<T> &stList)
  124. {
  125. while (head && head->next)
  126. {
  127. Stack_struct *temp=head;
  128. head=head->next;
  129. delete temp;
  130. }
  131. delete head;
  132. head=new Stack_struct;
  133. Stack_struct *head1=stList.get_head();
  134. length=stList.length;
  135. head->number=head1->number;
  136. head->next=0;
  137. Stack_struct *a=head, *p;
  138. for (int i=1; i<length; ++i)
  139. {
  140. p=new Stack_struct;
  141. head1=head1->next;
  142. p->number=head1->number;
  143. a->next=p;
  144. p->next=0;
  145. a=p;
  146. }
  147.  
  148. }
  149. template<typename T>
  150. void Stack_list<T>::add_list(Stack_struct *&head, float number)
  151. {
  152. if (head==NULL)
  153. {
  154. head=new Stack_struct;
  155. head->next=0;
  156. head->number=number;
  157. }
  158. else
  159. {
  160. Stack_struct *addition=new Stack_struct;
  161. addition->next=head;
  162. addition->number=number;
  163. head=addition;
  164. }
  165. }
  166. template<typename T>
  167. float Stack_list<T>::get_last(Stack_struct *&head)
  168. {
  169. if (head==NULL) return 0;
  170. else
  171. {
  172. if (head->next!=NULL)
  173. {
  174. float number=head->number;
  175. Stack_struct *temphead=head;
  176. head=head->next;
  177. delete temphead;
  178. return number;
  179.  
  180. }
  181. else
  182. {
  183. float number=head->number;
  184. delete head;
  185. head=NULL;
  186. return number;
  187. }
  188. }
  189. }
  190.  
  191.  
  192.  
  193.  
  194. #endif // STACK_LIST_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement