Advertisement
Vermiculus

LinkedList<T>

Feb 5th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.46 KB | None | 0 0
  1. /*
  2.  *  LinkedList.h
  3.  *  Project 1
  4.  *
  5.  *  Created by Sean Allred on 2/1/12.
  6.  *  Copyright 2012 St. Mary's College of Maryland. All rights reserved.
  7.  *
  8.  */
  9.  
  10. #include <cstdlib>
  11. #include <iostream>
  12.  
  13. using namespace std;
  14.  
  15. const bool VERBOSE = true;
  16. const bool SUPERVERBOSE = VERBOSE && false;
  17.  
  18. template <typename T> class LinkedList;
  19. template <typename T> ostream& operator << (ostream&, LinkedList<T>&);
  20. template <typename T> istream& operator >> (istream&, LinkedList<T>&);
  21.  
  22. template <typename T>
  23. struct Node {
  24.     Node<T>(T v) : value(v) {}
  25.     Node<T> *prev;
  26.     Node<T> *next;
  27.     T value;
  28. };
  29.  
  30. template <typename T>
  31. class LinkedList {
  32.    
  33.     friend ostream& operator << <T>(ostream&, LinkedList<T>&);
  34.     friend istream& operator >> <T>(istream&, LinkedList<T>&);
  35.    
  36. public:
  37.     /* (CON)(DE)STRUCTORS */
  38.     /* LinkedList()
  39.       Creates an empty LinkedList and
  40.       initializes 'head' and 'tail'.
  41.      */
  42.     LinkedList();
  43.    
  44.     /* LinkedList(int)
  45.       Creates an 'empty' LinkedList with
  46.       however many nodes are specified.
  47.      
  48.       Note that each node is *supposed* to be
  49.       rubbish, but Xcode seems to automagically
  50.       initialize primitives for you.
  51.      */
  52.     LinkedList(int);
  53.    
  54.     /*
  55.       Copy constructor
  56.      */
  57.     LinkedList(const LinkedList&);
  58.    
  59.     /* ~LinkedList()
  60.       Frees all memory used by the object
  61.       by removing literally every last node.
  62.      
  63.       The method trim() is called until the
  64.       size is zero, then 'head' and 'tail'
  65.       are deleted.
  66.      */
  67.     ~LinkedList();
  68.     /* END (CON)(DE)STRUCTORS */
  69.    
  70.     /*
  71.       public interface to 's', which denotes
  72.       how many nodes are in the LinkedList
  73.      */
  74.     int size();
  75.    
  76.     /*
  77.        Deletes the cell at the
  78.        ith index
  79.      */
  80.     T delCell(int);
  81.    
  82.     /*
  83.        Inserts a value (T) such that
  84.        its index is (int)
  85.      */
  86.     int insert(T, int);
  87.    
  88.     /*
  89.        Shorthand append
  90.      */
  91.     void operator += (T);
  92.    
  93.     /*
  94.        Successfully gets a value
  95.        from the list (shorthand get(int))
  96.      */
  97.     T& operator [](const int);
  98.    
  99.     /*
  100.        Reference assignment
  101.      */
  102.     void operator =(LinkedList&);
  103.    
  104.     /*
  105.        Takes off the last element
  106.      */
  107.     T trim();
  108.    
  109. //protected:
  110.     Node<T> *head;
  111.     Node<T> *tail;
  112.    
  113. //private:
  114.     Node<T>* get(int);
  115.    
  116.     Node<T>* set(T, int);
  117.    
  118.     int append(T);
  119.    
  120.     void print();
  121.    
  122.     int insertAfter(T, Node<T>*);
  123.    
  124.     int insertBefore(T, Node<T>*);
  125.    
  126.     T destroy(Node<T>*);
  127.    
  128.     void expand(int);
  129.    
  130.     void prime();
  131.    
  132.     int s;
  133. };
  134.  
  135. /*******************************/
  136. /* Constructors and Destructor */
  137. /*******************************/
  138.  
  139. template <typename T>
  140. LinkedList<T>::LinkedList() {
  141.     this->prime();
  142. }
  143.  
  144. template <typename T>
  145. LinkedList<T>::LinkedList(int i) {
  146.     this->prime();
  147.    
  148.     this->expand(i);
  149. }
  150.  
  151. template <typename T>
  152. LinkedList<T>::LinkedList(const LinkedList<T>& l) {
  153.     this->prime();
  154.    
  155.     Node<T> *n = l.head;
  156.     while (n != NULL) {
  157.         this->append(n->value);
  158.         n = n->next;
  159.     }
  160. }
  161.  
  162. template <typename T>
  163. LinkedList<T>::~LinkedList<T>() {
  164.     if(VERBOSE) {
  165.         printf("Destroying List at %p\n", this);
  166.     }
  167.     while (s != 0) {
  168.         this->trim();
  169.     }
  170.     if (VERBOSE) {
  171.         printf("List at %p destroyed\n", this);
  172.     }
  173. }
  174.  
  175. /**********************/
  176. /* Operator Overloads */
  177. /**********************/
  178.  
  179. template <typename T>
  180. void    LinkedList<T>::operator = (LinkedList<T>& l) {
  181.     this->head = l.head;
  182.     this->tail = l.tail;
  183. }
  184.  
  185. template <typename T>
  186. void    LinkedList<T>::operator += (T val) {
  187.     this->append(val);
  188. }
  189.  
  190. template <typename T>
  191. T&      LinkedList<T>::operator [] (const int i) {
  192.     return this->get(i)->value;
  193. }
  194.  
  195. /***** Insertion and Extraction *****/
  196.  
  197. template <typename T>
  198. ostream& operator << (ostream &os, LinkedList<T> &list) {
  199.     Node<T> *p = list.head;
  200.     os << "{";
  201.     while (p->next != NULL) {
  202.         os << p->value << ", ";
  203.         p = p->next;
  204.     }
  205.     os << p->value << "}";
  206.     return os;
  207. }
  208.  
  209. template <typename T>
  210. istream& operator >> (istream &is, LinkedList<T> &list) {
  211.     T val;
  212.    
  213.     is >> val;
  214.    
  215.     list.append(val);
  216.    
  217.     return is;
  218. }
  219.  
  220. /********************/
  221. /* Member Functions */
  222. /********************/
  223.  
  224. template <typename T>
  225. void    LinkedList<T>::prime() {
  226.     if(VERBOSE) {
  227.         printf("Created LinkedList at %p.\n", this);
  228.     }
  229.     this->s = 0;
  230.    
  231.     this->head = NULL;
  232.     this->tail = NULL;
  233. }
  234.  
  235. /***** Accessors *****/
  236.  
  237. template <typename T>
  238. int     LinkedList<T>::size() {
  239.     return this->s;
  240. }
  241.  
  242. template <typename T>
  243. Node<T>* LinkedList<T>::get(int i) {
  244.     if (i >= this->size()) {
  245.         this->expand(i - this->size());
  246.     }
  247.    
  248.     Node<T> *r = this->head;
  249.     while (i > 0 && r != this->tail) {
  250.         r = r->next;
  251.         i--;
  252.     }
  253.    
  254.     return r;
  255. }
  256.  
  257. /***** Mutators *****/
  258.  
  259. /* Neutral */
  260.  
  261. template <typename T>
  262. Node<T>*    LinkedList<T>::set(T val, int i) {
  263.     this->get(i)->value = val;
  264. }
  265.  
  266. /* Increasing */
  267.  
  268. template <typename T>
  269. int     LinkedList<T>::append(T val) {
  270.    
  271.     Node<T> *newnode = new Node<T>(val);
  272.    
  273.     if(SUPERVERBOSE) {
  274.         printf("Appending value '%d' at %p\n", val, newnode);
  275.     }
  276.    
  277.     switch (s) {
  278.         case 0:
  279.             this->head = newnode;
  280.             newnode->prev = NULL;
  281.             break;
  282.            
  283.         case 1:
  284.             this->head->next = newnode;
  285.             newnode->prev = this->head;
  286.             break;
  287.            
  288.         default:
  289.             this->tail->next = newnode;
  290.             //newnode->prev = this->tail->prev;
  291.             newnode->prev = this->get(s - 1);
  292.     }
  293.    
  294.     newnode->next = NULL;
  295.     this->tail = newnode;
  296.    
  297.     s++;
  298.     return 0;
  299. }
  300.  
  301. template <typename T>
  302. int     LinkedList<T>::insert(T val, int i) {
  303.     if (s == 0 || s == 1) {
  304.         s++;
  305.         return this->append(val);
  306.     }
  307.     return this->insertAfter(val, this->get(i));
  308. }
  309.  
  310. template <typename T>
  311. int     LinkedList<T>::insertAfter(T val, Node<T> *oldnode) {
  312.     if (oldnode == this->tail) {
  313.         return this->append(val);
  314.     }
  315.     Node<T> *newnode = new Node<T>(val);
  316.    
  317.     newnode->prev = oldnode;
  318.     newnode->next = oldnode->next;
  319.    
  320.     newnode->next->prev = newnode;
  321.     oldnode->next = newnode;
  322.    
  323.     s++;
  324.    
  325.     return 0;
  326. }
  327.  
  328. template <typename T>
  329. void    LinkedList<T>::expand(int i) {
  330.     while (i > 0) {
  331.         T *n = new T;
  332.         this->append(*n);
  333.         i--;
  334.     }
  335. }
  336.  
  337. /* Decreasing */
  338.  
  339. template <typename T>
  340. T       LinkedList<T>::trim() {
  341.     return this->delCell(this->size()-1);
  342. }
  343.  
  344. template <typename T>
  345. T       LinkedList<T>::delCell(int i) {
  346.     Node<T> *del = this->get(i);
  347.    
  348.     if (del == this->head) {
  349.         this->head = del->next;
  350.     } else {
  351.         del->prev->next = del->next;
  352.     }
  353.    
  354.     if (del == this->tail) {
  355.         this->tail = del->prev;
  356.     } else {
  357.         del->next->prev = del->prev;
  358.     }
  359.    
  360.    
  361.     return this->destroy(del);
  362. }
  363.  
  364. template <typename T>
  365. T       LinkedList<T>::destroy(Node<T> *n) {
  366.     T r = n->value;
  367.     delete n;
  368.     s--;
  369.    
  370.     if(SUPERVERBOSE) {
  371.         printf("Value '%d' at %p destroyed\n", r, n);
  372.     }
  373.     return r;
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement