Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************************************
- * COMP2012 - 2014/15 Fall
- * Programming Assignment 2
- * File: List.cpp
- *******************************************************/
- #include <iostream>
- #include "List.h"
- using namespace std;
- /******************* member functions *******************/
- PolyList::PolyList()
- {
- head=NULL;
- tail=NULL;
- }
- bool PolyList::empty() const
- {
- if (head!=NULL) return false;
- else return true;
- }
- //////////////////////////////////////////////////////////
- // add your implementation for the member functions below
- // ...
- // copy constructor
- PolyList::PolyList(const PolyList& a){
- PolyNode * a_head = new PolyNode;
- a_head = a.head;
- PolyNode * curr = new PolyNode;
- curr = head;
- curr->coef = a_head->coef;
- curr->expx = a_head->expx;
- curr->expy = a_head->expy;
- a_head = a_head->next;
- while (a_head != NULL)
- {
- curr->next = new PolyNode;
- curr->next->prev = curr;
- curr = curr->next;
- curr->coef = head->coef;
- curr->expx = head->expx;
- curr->expy = head->expy;
- a_head = a_head ->next;
- }
- tail = curr;
- }
- // destructor
- PolyList::~PolyList(){
- while ( head!= NULL){
- PolyNode * new_head = head;
- head = head->next;
- head->prev = NULL;
- delete new_head;
- }
- }
- // returns the number of node in the linked list
- int PolyList::length() const{
- PolyNode * count = head;
- int node_num = 0;
- while(count != NULL)
- {
- node_num++;
- count = count->next;
- }
- return node_num;
- }
- // add a node to the end of the linked list
- void PolyList::attachNode(PolyNode* new_node){
- if((*this).empty()){
- (*this).setHead(new_node);
- (*this).setTail(new_node);
- }else{
- (*this).getTail()->next = new_node;
- new_node->prev = (*this).getTail();
- (*this).setTail(new_node);
- }
- }
- // delete the first node in the linked list
- void PolyList::deleteHead(){
- PolyNode * new_head = head;
- head = head->next;
- head->prev = NULL;
- delete new_head;
- }
- // Assignment operator
- PolyList& PolyList::operator=(const PolyList& a){
- PolyNode* head = this->head;
- while(head != NULL) {
- PolyNode* temp = head->next;
- delete head;
- head = temp;
- }
- PolyNode * a_head = new PolyNode;
- a_head = a.head;
- PolyNode * curr = new PolyNode;
- curr = head;
- curr->coef = a_head->coef;
- curr->expx = a_head->expx;
- curr->expy = a_head->expy;
- a_head = a_head->next;
- while (a_head != NULL)
- {
- curr->next = new PolyNode;
- curr->next->prev = curr;
- curr = curr->next;
- curr->coef = head->coef;
- curr->expx = head->expx;
- curr->expy = head->expy;
- a_head = a_head ->next;
- }
- tail = curr;
- return (*this);
- }
- // Equal-to operator
- bool PolyList::operator==(const PolyList& a) const{
- PolyNode * a_head = a.head;
- PolyNode * curr = head;
- while(a_head!= NULL && curr!= NULL)
- {
- if((a_head->coef == curr->coef)&&(a_head->expx == curr->expx)&&(a_head->expy == curr->expy))
- {
- curr = curr->next;
- a_head = a_head->next;
- }
- else
- return false;
- }
- return true;
- }
- // find and return the n-th node in the linked list
- PolyNode* PolyList::operator[](int n) const{
- /*
- PolyNode * find = new PolyNode;
- find = head;
- int i = 1;
- while (i != n){
- find = find->next;
- i++;
- }
- return find;
- */
- if( n > (*this).length())
- {
- return NULL;
- }
- PolyNode * find = new PolyNode;
- for (int i = 0;i < (*this).length();i++)
- {
- if( i == n )
- break;
- else
- find = find ->next;
- }
- return find;
- }
- //Get Head
- PolyNode* PolyList::getHead()const{
- return head;
- }
- void PolyList::setHead(PolyNode* NewHead){
- head = NewHead;
- }
- void PolyList::setTail(PolyNode* NewTail){
- tail = NewTail;
- }
- PolyNode* PolyList::getTail()const{
- return tail;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement