Advertisement
Guest User

labka1

a guest
Dec 1st, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int counter = 0;
  8. int countele = 0;
  9. template<typename Key, typename Info>
  10. class sequence{
  11. private:
  12. struct element{
  13. Key k;
  14. Info i;
  15. element *next;
  16. };
  17.  
  18. element *head;
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. public:
  27. void push_front(const Key &k, const Info &i);
  28. void push_back(const Key &k, const Info &i);
  29. void pop_front();
  30. void pop_back();
  31. void clear();
  32.  
  33. void deleteKey(const Key &k);
  34.  
  35.  
  36. sequence();
  37. ~sequence();
  38. sequence(const sequence<Key, Info> & copy);
  39.  
  40. void print();
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49. };
  50.  
  51.  
  52. template<typename Key, typename Info>
  53. sequence<Key,Info>::sequence(){
  54. head=NULL;
  55. counter++;
  56. }
  57.  
  58. template<typename Key, typename Info>
  59. sequence<Key,Info>::sequence(const sequence<Key, Info> & copy){
  60. element *temp = copy.head;
  61. this->head = NULL;
  62. while(temp){
  63. this->push_back(temp->k, temp->i);
  64. temp = temp->next;
  65. }
  66. }
  67.  
  68.  
  69. template<typename Key, typename Info>
  70. void sequence<Key, Info>::deleteKey(const Key &k){
  71. element *temp;
  72.  
  73. }
  74.  
  75.  
  76. template<typename Key, typename Info>
  77. sequence<Key,Info>::~sequence(){
  78. clear();
  79. counter--;
  80. }
  81.  
  82.  
  83.  
  84. template<typename Key, typename Info>
  85. void sequence<Key,Info>::print(){
  86. element *temp = head;
  87. while(temp!=NULL){
  88. cout<<"key: "<<temp->k<<" info: "<<temp->i<<endl;
  89. temp = temp->next;
  90. }
  91. cout<<"---------------------"<<endl;
  92. }
  93.  
  94.  
  95. template<typename Key, typename Info>
  96. void sequence<Key,Info>::clear(){
  97. element *temp = head;
  98. while(head!=NULL){
  99. head = head->next;
  100. delete temp;
  101. temp = head;
  102. countele--;
  103.  
  104. }
  105.  
  106. }
  107.  
  108.  
  109.  
  110. template<typename Key, typename Info>
  111. void sequence<Key, Info>::push_front(const Key &k, const Info &i){
  112. element *temp;
  113. temp = new element;
  114. temp->k = k;
  115. temp->i = i;
  116. temp->next = head;
  117. head = temp;
  118. countele++;
  119. }
  120.  
  121. template<typename Key, typename Info>
  122. void sequence<Key, Info>::push_back(const Key &k, const Info &i){
  123. element *temp;
  124. temp = new element;
  125. temp->k = k;
  126. temp->i = i;
  127. temp->next = NULL;
  128. element *tmp = head;
  129. if(head == NULL){
  130. head = temp;
  131. countele++;
  132. cout<<"pusta glowa"<<endl;
  133. }
  134. else{
  135. while(tmp->next){
  136. tmp = tmp->next;
  137. }
  138. tmp->next = temp;
  139. cout<<"TEST"<<endl;
  140. countele++;
  141. }
  142. }
  143.  
  144. template<typename Key, typename Info>
  145. void sequence<Key, Info>::pop_front(){
  146. if(head==NULL){
  147. return;
  148. }
  149. element *temp = head;
  150. head = head->next;
  151. delete temp;
  152. countele--;
  153. }
  154.  
  155.  
  156. template<typename Key, typename Info>
  157. void sequence<Key, Info>::pop_back(){
  158. if(head==NULL){
  159. return;
  160. }
  161. element *temp = head;
  162. if(head->next == NULL){
  163. delete temp;
  164. head = NULL;
  165. countele--;
  166. return;
  167. }
  168. else{
  169. element *prev;
  170. while(temp->next){
  171. prev = temp;
  172. temp = temp->next;
  173. }
  174. delete temp;
  175. prev->next = NULL;
  176. countele--;
  177. }
  178.  
  179. }
  180.  
  181.  
  182.  
  183. int main(){
  184. sequence<unsigned,string> s;
  185. s.push_front(1,"jeden");
  186. s.push_front(2,"dewa");
  187. s.push_front(3,"terzy");
  188. s.push_back(4, "tsztery");
  189. cout<<countele<<endl;
  190. cout<<counter<<endl;
  191. s.print();
  192. s.pop_front();
  193. cout<<countele<<endl;
  194. cout<<counter<<endl;
  195. s.print();
  196. s.clear();
  197. cout<<countele<<endl;
  198. cout<<counter<<endl;
  199. s.print();
  200. s.pop_front();
  201. cout<<countele<<endl;
  202. cout<<counter<<endl;
  203. s.print();
  204. s.push_back(5, "pienc");
  205. cout<<countele<<endl;
  206. cout<<counter<<endl;
  207. s.print();
  208. s.clear();
  209.  
  210. s.push_front(7,"jed7en");
  211. s.push_front(58,"dew785a");
  212. s.push_front(38,"ter875zy");
  213. s.push_front(14,"je785en");
  214. s.push_front(12,"de8wa");
  215. s.push_front(32,"ter785");
  216. s.print();
  217. s.pop_back();
  218. s.pop_front();
  219. s.print();
  220. s.clear();
  221. s.pop_back();
  222. s.print();
  223. s.pop_front();
  224. s.print();
  225. s.push_back(6,"ref");
  226. s.print();
  227. s.pop_back();
  228. s.print();
  229. s.push_front(99,"sf");
  230. s.print();
  231. s.pop_front();
  232. s.print();
  233. cout<<countele<<endl;
  234. cout<<counter<<endl;
  235.  
  236. s.clear();
  237. s.push_back(14,"je785en");
  238. s.push_back(12,"de8wa");
  239. s.push_back(32,"ter785");
  240.  
  241. s.print();
  242. cout<<countele<<endl;
  243. cout<<counter<<endl;
  244.  
  245. sequence<unsigned,string> c(s);
  246. c.print();
  247. cout<<countele<<endl;
  248. cout<<counter<<endl;
  249.  
  250. //a=+b;
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement