Advertisement
Guest User

Untitled

a guest
May 5th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. #include<iostream>
  2. #include<iterator>
  3. #include<math.h>
  4.  
  5. using namespace std;
  6.  
  7. template<typename key, typename info>
  8. class Ring{
  9. private:
  10. struct Node{
  11. key nkey;
  12. info ninfo;
  13. Node* next;
  14. Node* prev;
  15. };
  16.  
  17. Node* head;
  18. Node headNode;
  19. void copyAll(const Ring<key,info>& x);
  20. void clearAll();
  21. unsigned int size=0;
  22.  
  23. public:
  24. class iterator{
  25. Node* itr;
  26. public:
  27. iterator(Node* temp):itr(temp){}
  28. iterator(const iterator& myitr):itr(myitr.itr){}
  29.  
  30. const iterator& operator++(){
  31. itr = itr->next;
  32. return *this;
  33. }
  34.  
  35. const iterator& operator--(){
  36. itr=itr->prev;
  37. return *this;
  38. }
  39.  
  40. bool operator==(const iterator& right){
  41. return itr == right.itr;
  42. }
  43.  
  44. bool operator!=(const iterator& right){
  45. return itr!=right.itr;
  46. }
  47.  
  48. const Node& operator*()const{
  49. return *itr;
  50. }
  51.  
  52. const iterator& operator+=(int x){
  53. for(int i=0; i<abs(x) ;i++){
  54. if(x>0){
  55. itr = itr->next;
  56. }
  57. if(x<0){
  58. itr = itr->prev;
  59. }
  60. }
  61. return *this;
  62. }
  63.  
  64. const iterator& operator-=(int x){
  65. // for(int i=0; i<abs(x) ;i++){
  66. // if(x<0){
  67. // itr = itr->next;
  68. // }
  69. // if(x>0){
  70. // itr = itr->prev;
  71. // }
  72. // }
  73. return *this += -x;
  74. }
  75. };
  76.  
  77. public:
  78. Ring();
  79. Ring(const Ring<key,info>& right);
  80. Ring(Ring<key,info>&& right);
  81. ~Ring();
  82. Ring& operator=(const Ring<key,info>& right);
  83. Ring& operator=(Ring<key,info>&& right);
  84. void push_front(const key& nkey, const info& ninfo); //AFTER SENTINEL NODE
  85. void push_back(const key& nkey, const info& ninfo); //BEFORE SENTINEL NODE
  86. void remove(const key& nkey); //deletes the first occurance of the key in the ring
  87. iterator begin()const{return head->next;}
  88. iterator end()const{return head;}
  89. void display();
  90. int getsize()const{return this->size;}
  91. };
  92.  
  93.  
  94. template<typename key, typename info>
  95. Ring<key,info>::Ring(){
  96. headNode.nkey=0;//Key();
  97. headNode.ninfo=0;//Info();
  98. head=&headNode;
  99. headNode.next=&headNode;
  100. headNode.prev=&headNode;
  101. }
  102.  
  103. template<typename key, typename info>
  104. Ring<key,info>::Ring(const Ring<key,info>& right){
  105. head=&headNode;
  106. headNode.next=&headNode;
  107. headNode.prev=&headNode;
  108. copyAll(right);
  109. size=right.size;
  110. }
  111.  
  112. template<typename key, typename info>
  113. Ring<key,info>::Ring(Ring<key,info>&& right){
  114. head=right.head;
  115. size=right.size;
  116. right.size=0;
  117. right.head=nullptr;
  118. }
  119.  
  120. template<typename key, typename info>
  121. Ring<key,info>::~Ring(){
  122. clearAll();
  123. }
  124.  
  125. template<typename key, typename info>
  126. void Ring<key,info>::push_back(const key& nkey,const info& ninfo){
  127. if(nkey==0 || ninfo==0){
  128. cerr << "Key or Info can not be 0" << endl; //zero values belong to sentinel only
  129. return;
  130. }
  131. Node* nptr = new Node;
  132. nptr->nkey=nkey;
  133. nptr->ninfo=ninfo;
  134. if(head->next==head){
  135. head->next=nptr;
  136. nptr->next=head;
  137. nptr->prev=head;
  138. head->prev=nptr;
  139. size++;
  140. return;
  141. }
  142. else{
  143. head->prev->next=nptr;
  144. nptr->prev=head->prev;
  145. nptr->next=head;
  146. head->prev=nptr;
  147. size++;
  148. return;
  149. }
  150. }
  151.  
  152. template<typename key, typename info>
  153. void Ring<key,info>::push_front(const key& nkey,const info& ninfo){
  154. if(nkey==0 || ninfo==0){
  155. cerr << "Key or Info can not be 0" << endl; //zero values belong to sentinel only
  156. return;
  157. }
  158. Node* nptr = new Node;
  159. nptr->nkey=nkey;
  160. nptr->ninfo=ninfo;
  161. if(head->next==head){ // head->next==head
  162. head->next=nptr;
  163. nptr->next=head;
  164. nptr->prev=head;
  165. head->prev=nptr;
  166. size++;
  167. return;
  168. }
  169. else{
  170. head->next->prev=nptr;
  171. nptr->prev=head;
  172. nptr->next=head->next;
  173. head->next=nptr;
  174. size++;
  175. return;
  176. }
  177. }
  178.  
  179. template<typename key, typename info>
  180. void Ring<key,info>::remove(const key& nkey){
  181. if(head->next->nkey==nkey && head->next==head){
  182. delete head->next;
  183. head->next=head;
  184. head->prev=head;
  185. size--;
  186. return;
  187. }
  188. else{
  189. Node* ptr=head->next;
  190. while(ptr!=head){
  191. if(ptr->nkey==nkey){
  192. ptr->prev->next=ptr->next;
  193. ptr->next->prev=ptr->prev;
  194. delete ptr;
  195. size--;
  196. return;
  197. }
  198. ptr=ptr->next;
  199. }
  200. }
  201. cerr << "Key not found" << endl;
  202. }
  203.  
  204. template<typename key, typename info>
  205. void Ring<key,info>::clearAll(){
  206. Node* ptr=head->next;
  207. while(ptr!=head){
  208. ptr=ptr->next;
  209. delete (head->next);
  210. head->next=ptr;
  211. }
  212. head->next=head; //just to make sure head is not lost
  213. head->prev=head;
  214. size=0; //really a need for these 3 lines??
  215. }
  216.  
  217. template<typename key, typename info>
  218. void Ring<key,info>::copyAll(const Ring<key,info>& x){
  219.  
  220. Node* xtr = x.head->next;
  221. Node* etr = this->head->next;
  222. if(etr->next!=etr){
  223. this->clearAll();
  224. }
  225.  
  226. while(xtr!=x.head){
  227. Node* ntr = new Node;
  228. ntr->nkey=xtr->nkey;
  229. ntr->ninfo=xtr->ninfo;
  230. ntr->next=nullptr; //can also use head in place of this->head
  231. if(head->next==head){
  232. head->next=ntr;
  233. ntr->prev=head;
  234. } // original in xtr
  235. else{
  236. etr->next=ntr;
  237. ntr->prev=etr;
  238. } //copy in ntr
  239. etr=ntr; //etr points to this copy so ntr points to new element in the next loop
  240. xtr=xtr->next;
  241. }
  242. head->prev=etr;
  243. etr->next=head;
  244. }
  245.  
  246. template<typename key, typename info>
  247. Ring<key,info>& Ring<key,info>::operator=(const Ring<key,info>& right){
  248. if(right.getsize()==0){return *this;}
  249. if(this==&right){return *this;}
  250. this->copyAll(right);
  251. size=right.size;
  252. return *this;
  253. }
  254.  
  255. template<typename key, typename info>
  256. Ring<key,info>& Ring<key,info>::operator=(Ring<key,info>&& right){
  257. this->clearAll();
  258. head=right.head;
  259. size=right.size;
  260. right.head=nullptr;
  261. return *this;
  262. }
  263.  
  264. template<typename key, typename info>
  265. void Ring<key,info>::display(){
  266. for(auto it=this->begin();it!=this->end();++it){
  267. cout << (*it).nkey << "-" << (*it).ninfo<< " ";
  268. }
  269. cout << endl;
  270. }
  271.  
  272. template<typename key, typename info>
  273. Ring<key,info> produce(const Ring<key,info>& r1, const Ring<key,info>& r2, int s1, int len1, int s2, int len2, int repeat, bool dir){
  274. Ring<key,info> nRing;
  275. if(len1==0 || len2==0 || repeat==0 || r1.getsize()==0 || r2.getsize()==0){
  276. cerr << "Invalid parameters" << endl;
  277. return nRing;
  278. }
  279. auto itr1=r1.begin(); auto itr2=r2.begin();
  280. itr1+=s1; itr2+=s2;
  281. if(s1<0){itr1-=((abs(s1)/r1.getsize())+1);} // to deal with the difference of 1 after each cycle due to sentinel node
  282. if(s2<0){itr2-=((abs(s2)/r2.getsize())+1);}
  283.  
  284. for(int x=0;x<repeat;x++){
  285. for(int i=0;i<abs(len1);i++){
  286. if(itr1==r1.end()){--i;} //bypassing the sentinel node
  287. else{
  288. if(!dir){nRing.push_front((*itr1).nkey,(*itr1).ninfo);}
  289. else{nRing.push_back((*itr1).nkey,(*itr1).ninfo);}
  290. }
  291. len1>0?++itr1:--itr1;
  292. }
  293. for(int j=0;j<abs(len2);j++){
  294. if(itr2==r2.end()){--j;}
  295. else{
  296. if(!dir){nRing.push_front((*itr2).nkey,(*itr2).ninfo);}
  297. else{nRing.push_back((*itr2).nkey,(*itr2).ninfo);}
  298. }
  299. len2>0?++itr2:--itr2;
  300. }
  301. }
  302. return nRing;
  303. }
  304.  
  305. int main(){
  306. /*Ring<int,int> a;
  307. a.push_front(2,4);
  308. a.push_front(4,6);
  309. a.push_front(3,7);
  310. a.display();
  311. Ring<int,int> b;
  312. b.push_front(9,7);
  313. b.push_front(5,2);
  314. b.push_front(6,8);
  315. b.push_front(1,1);
  316. b.display();*/
  317.  
  318. Ring<int,int> a;
  319. a.push_back(1,1);
  320. a.push_back(2,1);
  321. a.push_back(3,1);
  322. a.push_back(4,1);
  323. //a.push_back(3,2);
  324. a.display();
  325. Ring<int,int> b;
  326. b.push_back(10,2);
  327. b.push_back(20,2);
  328. b.push_back(30,2);
  329. b.push_back(40,2);
  330. b.push_back(50,2);
  331. b.display();
  332. Ring<int,int> r;
  333. Ring<int,int> d=produce(a,b,-2,3,3,-2,2,1);
  334. d.display();
  335. //cout << d.getsize()<<endl;
  336. //r=b;
  337. //r.display();
  338. //Ring<int,int> c;
  339. //c.reverse(b);
  340. //c.display();
  341. return 0;
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement