Guest User

Untitled

a guest
Feb 16th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. #ifndef LINKLIST_LINK_H
  2. #define LINKLIST_LINK_H
  3.  
  4. #include <iosfwd>
  5.  
  6. class Link{
  7. private:
  8. int value;
  9. Link* next = nullptr;
  10. public:
  11. Link();
  12.  
  13. explicit Link(int key);
  14.  
  15. int getValue() const;
  16.  
  17. void setValue(int value);
  18.  
  19. Link *getNext() const;
  20.  
  21. void setNext(Link *next);
  22.  
  23. friend void operator<<(std::ostream& os, const Link &link);
  24. };
  25.  
  26. #endif //LINKLIST_LINK_H
  27.  
  28. #include "Link.h"
  29.  
  30. #include <iostream>
  31.  
  32. Link::Link() {
  33. }
  34.  
  35. Link::Link(int key) {
  36. this->setValue(key);
  37. }
  38.  
  39. int Link::getValue() const {
  40. return value;
  41. }
  42.  
  43. void Link::setValue(int value) {
  44. Link::value = value;
  45. }
  46.  
  47.  
  48. Link *Link::getNext() const {
  49. return next;
  50. }
  51.  
  52. void Link::setNext(Link *next) {
  53. Link::next = next;
  54. }
  55.  
  56. void operator<<(std::ostream& os, const Link &link){
  57. os<<link.getValue()<<" ";
  58. }
  59.  
  60. #ifndef LINKLIST_LINKLIST_H
  61. #define LINKLIST_LINKLIST_H
  62.  
  63. #include <vector>
  64. #include "Link.h"
  65.  
  66. class LinkList{
  67.  
  68. protected:
  69. Link* head = nullptr;
  70.  
  71. Link* tail = nullptr;
  72.  
  73. std::vector<void* >linksAddresses;
  74. public:
  75.  
  76. virtual ~LinkList();
  77.  
  78. void insert(int key);
  79.  
  80. void push_back(int key);
  81.  
  82. void push_front(int key);
  83.  
  84. bool deleteKey(int key);
  85.  
  86. bool findKey(int key);
  87.  
  88. void insert_sorted(int key);
  89.  
  90. friend void operator<<(std::ostream& os, const LinkList &linkList);
  91.  
  92. void getLinksAddresses();
  93.  
  94. void printReverse() const;
  95.  
  96. void do_reverse();
  97.  
  98. Link* delete_at_pos(int n);
  99. };
  100.  
  101. #endif //LINKLIST_LINKLIST_H
  102.  
  103. #include <iostream>
  104. #include "LinkList.h"
  105.  
  106. void LinkList::insert(int key) {
  107. push_back(key);
  108. }
  109.  
  110. void LinkList::push_front(int key) {
  111.  
  112. Link *newNode = new Link(key);
  113. if (head == nullptr) {
  114. head = newNode;
  115. tail = newNode;
  116. } else {
  117. newNode->setNext(head);
  118. head = newNode;
  119. }
  120. //std::cout << "Inserted " << key <<" address : "<<&newNode << " at frontn";
  121.  
  122. }
  123.  
  124. void LinkList::push_back(int key) {
  125. Link *newNode = new Link(key);
  126. if (tail == nullptr) {
  127. head = newNode;
  128. tail = newNode;
  129. } else {
  130. tail->setNext(newNode);
  131. tail = newNode;
  132. }
  133. //std::cout << "Inserted " << key << " at backn";
  134.  
  135. }
  136.  
  137. bool LinkList::deleteKey(int key) {
  138. Link *curr = head;
  139. Link *prev = head;
  140.  
  141. if (head != nullptr && head->getValue() == key) {
  142. Link *temp = head;
  143. head = head->getNext();
  144. if(head == nullptr)
  145. tail = nullptr;
  146. delete temp;
  147. std::cout << "Deleted " << key << "n";
  148. return true;
  149. }
  150.  
  151. while (curr != nullptr && curr->getValue() != key) {
  152. prev = curr;
  153. curr = curr->getNext();
  154. }
  155.  
  156. if (curr == nullptr) {
  157. std::cout << "To delete Key " << key << " doesn't existn";
  158. return false;
  159. } else {
  160. prev->setNext(curr->getNext());
  161. delete curr;
  162. std::cout << "Deleted " << key << "n";
  163. return true;
  164. }
  165. }
  166.  
  167. bool LinkList::findKey(int key) {
  168. Link *current = head;
  169. while (current != nullptr && current->getValue() != key)
  170. current = current->getNext();
  171. return current != nullptr;
  172. }
  173.  
  174. LinkList::~LinkList() {
  175. Link *next;
  176. Link *curr = head;
  177. while (curr != nullptr) {
  178. next = curr->getNext();
  179. delete curr;
  180. curr = next;
  181. }
  182. //std::cout<<"Memory freedn";
  183. }
  184.  
  185. void operator<<(std::ostream &os, const LinkList &linkList) {
  186. Link *curr = linkList.head;
  187. os << "List is : ";
  188. while (curr != nullptr) {
  189. os << *curr;
  190. curr = curr->getNext();
  191. }
  192. os << 'n';
  193. }
  194.  
  195. void LinkList::insert_sorted(int key) {
  196. //TODO
  197. }
  198.  
  199. void LinkList::getLinksAddresses() {
  200. while (head != nullptr){
  201. linksAddresses.push_back(&head);
  202. std::cout<<&head<<" "<<head->getValue()<<"n";
  203. head = head->getNext();
  204. }
  205. }
  206.  
  207. void reverse(Link* head){
  208. if(head!= nullptr){
  209. reverse(head->getNext());
  210. std::cout<<head->getValue()<<" ";
  211. }
  212. }
  213.  
  214. void LinkList::printReverse() const {
  215. reverse(head);
  216. std::cout<<"n";
  217. }
  218.  
  219. void LinkList::do_reverse() {
  220.  
  221. Link* prev = nullptr;
  222. Link* curr = head;
  223. Link* next = curr->getNext();
  224. while (curr){
  225.  
  226. }
  227.  
  228. }
  229.  
  230. Link* LinkList::delete_at_pos(int n)
  231. {
  232. if(n <=0)
  233. return head;
  234. int count = 1;
  235. Link* curr = head, *prev = nullptr;;
  236. while(curr!=nullptr){
  237. if(count == n)
  238. break;
  239. prev = curr;
  240. curr = curr->getNext();
  241. count++;
  242. }
  243. if(curr!=nullptr){
  244. Link* temp = curr;
  245. if(curr == head){
  246. head = head->getNext();
  247. }else{
  248. prev->setNext(curr->getNext());
  249. }
  250. delete temp;
  251. }
  252. return head;
  253. }
  254.  
  255. #ifndef LINKLIST_LRU_HPP
  256. #define LINKLIST_LRU_HPP
  257.  
  258. #include <iostream>
  259. #include "LinkList.h"
  260. class LRU : public LinkList{
  261. private:
  262. const int MAX = 4;
  263. int max_len = 0;
  264. Link* findPage(int key){
  265. Link *current = LinkList::head;
  266. while (current != nullptr && current->getValue() != key)
  267. current = current->getNext();
  268. return current;
  269. }
  270. public:
  271. void insert(int key) override{
  272. if(MAX > 0) {
  273. Link *page = findPage(key);
  274. if (page != nullptr) {
  275. access(page->getValue());
  276. return;
  277. }
  278.  
  279. if (max_len >= MAX) {
  280. deleteKey(LinkList::head->getValue());
  281. max_len--;
  282. }
  283. push_back(key);
  284. max_len++;
  285. }
  286. }
  287.  
  288. bool access(int key){
  289. Link *current = findPage(key);
  290. if(current == nullptr)
  291. return false;
  292. else{
  293. int val = current->getValue();
  294. deleteKey(val);
  295. max_len--;
  296. insert(val);
  297. return true;
  298. }
  299. }
  300. };
  301.  
  302. #endif //LINKLIST_LRU_HPP
  303.  
  304. #include "LinkList.h"
  305. #include "LRU.hpp"
  306. #include <iostream>
  307. void testingLinkedLRU();
  308. int main() {
  309. testingLinkedLRU();
  310. return 0;
  311. }
  312. void testingLinkedLRU(){
  313. LRU lru;
  314. std::cout<<lru;
  315.  
  316. lru.insert(1);
  317. std::cout<<lru;
  318.  
  319. lru.insert(2);
  320. std::cout<<lru;
  321.  
  322. lru.insert(3);
  323. std::cout<<lru;
  324.  
  325. lru.insert(4);
  326. std::cout<<lru;
  327.  
  328. lru.insert(5);
  329. std::cout<<lru;
  330.  
  331. lru.insert(6);
  332. std::cout<<lru;
  333.  
  334. lru.access(3);
  335. std::cout<<lru;
  336.  
  337. lru.insert(-5);
  338. std::cout<<lru;
  339.  
  340. lru.insert(5);
  341. std::cout<<lru;
  342.  
  343. lru.insert(50);
  344. std::cout<<lru;
  345.  
  346. lru.access(5);
  347. std::cout<<lru;
  348.  
  349. lru.access(1);
  350. std::cout<<lru;
  351.  
  352. lru.access(-5);
  353. std::cout<<lru;
  354.  
  355. }
Add Comment
Please, Sign In to add comment