Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. /*******************************************************
  2.  * COMP2012 - 2014/15 Fall
  3.  * Programming Assignment 2
  4.  * File: List.cpp
  5.  *******************************************************/
  6.  
  7. #include <iostream>
  8. #include "List.h"
  9.  
  10. using namespace std;
  11.  
  12. /******************* member functions *******************/
  13. PolyList::PolyList()
  14. {
  15.     head=NULL;
  16.     tail=NULL;
  17. }
  18.  
  19. bool PolyList::empty() const
  20. {
  21.     if (head!=NULL) return false;
  22.     else return true;
  23. }
  24.  
  25. //////////////////////////////////////////////////////////
  26. // add your implementation for the member functions below
  27. // ...
  28. // copy constructor
  29. PolyList::PolyList(const PolyList& a){
  30.     PolyNode * a_head = new PolyNode;
  31.     a_head = a.head;
  32.     PolyNode * curr = new PolyNode;
  33.     curr = head;
  34.  
  35.     curr->coef = a_head->coef;
  36.     curr->expx = a_head->expx;
  37.     curr->expy = a_head->expy;
  38.     a_head = a_head->next;
  39.  
  40.     while (a_head != NULL)
  41.     {
  42.         curr->next = new PolyNode;
  43.         curr->next->prev = curr;
  44.         curr = curr->next;
  45.         curr->coef = head->coef;
  46.         curr->expx = head->expx;
  47.         curr->expy = head->expy;
  48.         a_head = a_head ->next;
  49.     }
  50.  
  51.     tail = curr;
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. }
  59. // destructor
  60. PolyList::~PolyList(){
  61.     while ( head!= NULL){
  62.         PolyNode * new_head = head;
  63.         head = head->next;
  64.         head->prev = NULL;
  65.         delete new_head;
  66.     }
  67. }
  68.  
  69. // returns the number of node in the linked list
  70. int PolyList::length() const{
  71.     PolyNode * count = head;
  72.     int node_num = 0;
  73.     while(count != NULL)
  74.     {
  75.         node_num++;
  76.         count = count->next;
  77.     }
  78.     return node_num;
  79. }
  80.  
  81. // add a node to the end of the linked list
  82. void PolyList::attachNode(PolyNode* new_node){
  83.  
  84.     if((*this).empty()){
  85.         (*this).setHead(new_node);
  86.         (*this).setTail(new_node);
  87.     }else{
  88.         (*this).getTail()->next = new_node;
  89.         new_node->prev = (*this).getTail();
  90.         (*this).setTail(new_node);
  91.     }
  92.  
  93. }
  94.  
  95. // delete the first node in the linked list
  96. void PolyList::deleteHead(){
  97.     PolyNode * new_head = head;
  98.     head = head->next;
  99.     head->prev = NULL;
  100.     delete new_head;
  101. }
  102.  
  103. // Assignment operator
  104. PolyList& PolyList::operator=(const PolyList& a){
  105.  
  106.     PolyNode* head = this->head;
  107.     while(head != NULL) {
  108.         PolyNode* temp = head->next;
  109.         delete head;
  110.         head = temp;
  111.     }
  112.  
  113.     PolyNode * a_head = new PolyNode;
  114.     a_head = a.head;
  115.     PolyNode * curr = new PolyNode;
  116.     curr = head;
  117.  
  118.     curr->coef = a_head->coef;
  119.     curr->expx = a_head->expx;
  120.     curr->expy = a_head->expy;
  121.     a_head = a_head->next;
  122.  
  123.     while (a_head != NULL)
  124.     {
  125.         curr->next = new PolyNode;
  126.         curr->next->prev = curr;
  127.         curr = curr->next;
  128.         curr->coef = head->coef;
  129.         curr->expx = head->expx;
  130.         curr->expy = head->expy;
  131.         a_head = a_head ->next;
  132.     }
  133.  
  134.     tail = curr;
  135.  
  136.     return (*this);
  137.  
  138.  
  139. }
  140.  
  141. // Equal-to operator
  142. bool PolyList::operator==(const PolyList& a) const{
  143.     PolyNode * a_head = a.head;
  144.     PolyNode * curr = head;
  145.  
  146.     while(a_head!= NULL && curr!= NULL)
  147.     {
  148.  
  149.         if((a_head->coef == curr->coef)&&(a_head->expx == curr->expx)&&(a_head->expy == curr->expy))
  150.         {
  151.             curr = curr->next;
  152.             a_head = a_head->next;
  153.         }
  154.         else
  155.             return false;
  156.     }
  157.     return true;
  158. }
  159.  
  160. // find and return the n-th node in the linked list
  161. PolyNode* PolyList::operator[](int n) const{
  162.     /*
  163.         PolyNode * find = new PolyNode;
  164.         find = head;
  165.         int i = 1;
  166.         while (i != n){
  167.             find = find->next;
  168.             i++;
  169.         }
  170.         return find;
  171.      */
  172.  
  173.     if( n > (*this).length())
  174.     {
  175.         return NULL;
  176.     }
  177.  
  178.     PolyNode * find = new PolyNode;
  179.     for (int i = 0;i < (*this).length();i++)
  180.     {
  181.         if( i == n )
  182.             break;
  183.         else
  184.             find = find ->next;
  185.  
  186.     }
  187.     return find;
  188.  
  189. }
  190.  
  191. //Get Head
  192. PolyNode* PolyList::getHead()const{
  193.     return head;
  194. }
  195.  
  196. void PolyList::setHead(PolyNode* NewHead){
  197.     head = NewHead;
  198. }
  199.  
  200. void PolyList::setTail(PolyNode* NewTail){
  201.     tail = NewTail;
  202. }
  203.  
  204. PolyNode* PolyList::getTail()const{
  205.     return tail;
  206.  
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement