• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

Code Check

// new functions added, need checked for syntax correctiveness.  Del

#ifndef LIST_H
#define LIST_H

#include <iostream>
#include <fstream>

using namespace std;

//const int CAPACITY = 1024;
typedef int ElementType;

class Node {

public:

     ElementType data;
     Node *next;

     Node* get_first() const { return first;} // private!
private:

     Node* first;
     //Node* info;
     int mySize;

};

typedef Node *NodePointer;


class List:public Node{

     // prototype for ouput operator
      friend ostream &operator << (ostream &out, const List &aList)  // needs to be a friend
       {

     aList.display(out);
     return out;

       }
public:

     List(int maxSize = 1024);
     ~List();
     // copy constuctor
     List(const List &origList); // no ',' here!
       // deep copy
       List(const List origList); // ADDED
     // assignment operator
     const List &operator = (const List &rightSide); // MODIFIED
     // list traversal
     void listTraversalNode();
     void printInReverse(Node *Head);   // call function to print list in reverse
     bool sort(); // boolean function ascending
      // perform linear search
      int List::linearSearch();
     // empty operation
     bool empty() const;
     // insert and erase
     void insert(ElementType item, int pos);
     void erase(int pos);
       // deep erase
       void List delete();
     // output
     void display(ostream &out) const;

private:

     int mySize; // current size of list store in myArray
     //ElementType myArray[CAPACITY]; // array to store list of elements
     int myCapacity; // capacity of array
     Node *myArray; // pointer to dynamic array
    //  Node *date; <----------------- what is this
     
};


#endif

***************************************************

#include <cassert>
#include <iostream>
#include <fstream>
#include <list>
#include <new>
#include <string>
#include "list.h"

using namespace std;

// prototype for ouput operator
//ostream &operator << (ostream &out, const List &alist);<-- you don't need this one

// definition of class constructor
List::List(int maxSize) : mySize(0), myCapacity(maxSize) {

     myArray = new Node;
     assert(myArray != 0);

}

// definition of class destructor
List::~List() { // CHANGED

    // delete [] myArray;
      
      NodePointer prev = first, ptr;

      while(ptr != 0) {
            ptr = prev -> next;
            delete prev;
            prev = ptr;

      }

   
      
}  

// definition of copy constructor
List::List(const List &origList) : mySize(origList.mySize), myCapacity(origList.myCapacity) {

         myArray = new Node;

}

// deep copy
List::List(const List origList) { // ADDED

      mySize = origList.mySize;
      get_first = 0;

      if(mySize == 0) return;
      NodePointer origPtr, lastPtr;
      first = new Node(origList.first->data);
      lastPtr = first;
      origPtr = origList.first->next;

      while(origPtr != 0) {

            lastPtr -> next = new Node(origPtr->data);
            origPtr = origPtr->next;
            lastPtr = lastPtr->next;

      }

}

// definition of assignment operator
const List &List::operator = (const List &rightSide) { // CHANGED

      /*

     // check that not self-assignment
     if(this != &rightHandSide) {
          // allocate new array if necessary
          if(myCapacity != rightHandSide.myCapacity) {
               delete [] myArray;
               myCapacity = rightHandSide.myCapacity;
               myArray = new Node;
               // check if memory available
               if(myArray == 0) {
                    cerr << " Inadequate memory to allocate stack. \n";
                    exit(1);

               }

          }
          // copy rightHandSide's list elements into this new array
          mySize = rightHandSide.mySize;

     }
     return *this;

       */

      mySize = rightSide.mySize;
      get_first = 0; // first is private

      if(mySize == 0) return *this;
      if(this != &rightSide) {

            this -> ~List();
            NodePointer origPtr, lastPtr;
            first = new Node(rightSide.first -> data); // copy first node
            lastPtr = first;
            origPtr = rightSide.first -> next;

            while(origPtr != 0) {
                  lastPtr -> next = new Node(origPtr -> data);
                  origPtr = origPtr -> next;
                  lastPtr = lastPtr -> next;

            }

      }
      return *this;

}

// definition of empty
bool List::empty() const {

     return mySize == 0;

}

// definition of display
void List::display(ostream &out) const {

     out << "\n\n The elements in the list are: ";

     NodePointer ptr = get_first(); // MODIFIED
     while(ptr != 0) {
          //out << ptr->date << " "; <----- do you mean *date* or *data* ??
            out<<ptr->data<<" "; // MODIFIED
                 ptr = ptr->next;

     }
     out << " ";

}



// definition of insert
void List::insert(ElementType item, int pos) {

     if(mySize == myCapacity) {
          cerr << " No space for linked list element, terminating execution. \n";
          exit(1);

     }
     if(pos < 0 || pos > mySize) {
          cerr << " Illegal location to insert -- " << pos << ". Linked list unchanged. \n";
          return;

     }

     // first shift array elements right to make room for item
     for(int i = mySize; i > pos; i--)
          myArray[i] = myArray[i - 1];
            mySize++;

}
// perform linear search
int List::linearSearch() { // added function

     ElementType dataVal;
     cout << " Type in value to search-> ";
     cin >> dataVal;

     int loc;
     NodePointer temp = get_first(); // MODIFED <-- instead of *first* cauze it returns first pointer :)
     for(loc = 0; loc < mySize; loc++) {
          if(temp->data == dataVal)
            {       cout << " Element located at location. ";
     return loc;
        }
          else {
               temp = temp -> next;
               cout << "\n The element is not in the list. ";
               return -1;
            }
     }

}
// traverse the list
void List::listTraversalNode() {

     NodePointer ptr = myArray->get_first(); // need to declare it with a type!
     NodePointer first = ptr;

     while(ptr != 0) {
          ptr = ptr -> next; // appropriate statements to process ptr -> data

     }

     NodePointer newptr = new Node; // newptr of type NodePointer

     NodePointer predptr = NULL;
     if(predptr != 0) { // not inserting at front
          newptr -> next = predptr -> next;
          predptr -> next = newptr;

     }
     else { // inserting at front
          newptr -> next = first;
          first = newptr; // reset first

     }

     if(predptr != 0) { // not deleting first node
          ptr = predptr -> next;
          predptr -> next = ptr -> next; // bypass

     }
     else { // deleting first node
          ptr = first;
          first = ptr -> next; // reset first

     }
     delete ptr; // return node to heap

}

// reverse print of list
void List::printInReverse(Node *Head) {

     if (Head == 0){
     cout << "List is empty." << endl;
     }
     //int count = 0;
     Node *printPtr = new Node;
     printPtr = Head;

     while(printPtr != 0) {
     printPtr = printPtr->next; //advance pointer to next location in list...
       
      }
 
}

// boolean value function ascending for node element
// the purpose of this function is to generate a boolean-valued
// function that determines whether the data items in the linked
// list are arranged in ascending order.
// sorts list in ascending order
bool List::sort() // new added function to perform above specs
{
     for(NodePointer temp = get_first(); temp != NULL; temp = temp->next)
     {
          NodePointer min = temp;

        /*  for(NodePointer current = min; current != NULL; current = current -> next)
          {
               if(current->NodePointer < min->data)min=current; //<-------- from where did you get *info* variable?
          }*/

          ElementType temp_var = temp -> data;
          temp -> data = min -> data;
          min -> data = temp_var;
     }
      return true;
           return false;

}
// definition of erase
void List::erase(int pos) {

     if(mySize == 0) {
          cerr << " List is empty \n";
          return;

     }
     if(pos < 0 || pos >= mySize) {
          cerr << " Illegal location to delete -- " << pos << ". List unchanged. \n";
          return;

     }

      // decrease list size
     mySize--;

}

// deep delete
void List::delete() {

      int index;

      cout << "\n Type in index between 0 and mySize - 1 of the Node to be deleted-> ";
      cin >> index;

      if(index < 0; index >= mySize) {

            cout << "\n Illegal location to delete. ";
            return;

      }
      mySize = -1;
      NodePointer ptr;
      prevPtr = get_first();

      if(index == 0) {

            ptr = first;
            first = ptr.next;
            delete ptr;
      }
      else {
            for(int i = 0; i < index; i++)
                  prevPtr = prevPtr->next;
            ptr = prevPtr->next;
            prevPtr->next = ptr->next;
            delete ptr;

      }
}

int main() {

     int myCapacity = 42; // It is always a good idea to *declare* the variables you are using

     // test the class constructor
     List intList;
     cout << " Constructing intList\n";

     // test empty and output of empty list
     if(intList.empty())
          cout << " Empty: \n" << intList << endl; // test output of empty list

     // test insert
     for(int i = 0; i < 9; i++) {
          cout << " Inserting " << i << " at position " << i/2 << endl;
          intList.insert(i, i/2); // insert i at position i/2

          // test output
          cout << intList << endl;

     }

     cout << " List empty? " << (intList.empty() ? "Yes" : "No") << endl;

     cout << "\nTry to insert at position -1" << endl;
     intList.insert(0, -1);

     cout << "\nTry to insert at position 10" << endl;
     intList.insert(0, 10);

     // test erase
     int index;
     cout << endl;

     while(!intList.empty()) {

          cout << " Give an index of a list element to remove: ";
          cin >> index;
          intList.erase(index);
          cout << intList << endl;

     }
     cout << " List is empty. " << endl;

     cout << "\nInserting " << myCapacity << " integers\n";

     for(int j = 0; j < myCapacity; j++)
          intList.insert(j, j);

     cout << " Attempting to insert one more integer: \n";
     intList.insert(-1, 0);
     return 0; //<-------- add this
       system("pause");


}

*******************************************
0
edelossantos
Asked:
edelossantos
1 Solution
 
jkrCommented:
The following will compile:

// new functions added, need checked for syntax correctiveness.  Del

#ifndef LIST_H
#define LIST_H

#include <iostream>
#include <fstream>

using namespace std;

//const int CAPACITY = 1024;
typedef int ElementType;

class Node {

public:

     ElementType data;
     Node *next;

     Node* get_first() const { return first;} // private!
//private: removing 'private' - you're accessing the members all over in your code

     Node* first;
     //Node* info;
     int mySize;

};

typedef Node *NodePointer;


class List:public Node{

     // prototype for ouput operator
      friend ostream &operator << (ostream &out, const List &aList)  // needs to be a friend
       {

     aList.display(out);
     return out;

       }
public:

     List(int maxSize = 1024);
     ~List();
     // copy constuctor
     List(const List &origList); // no ',' here!
      // deep copy
      //List(const List origList); // ADDED -- but wrong, since 'List' so far is incomplete
     // assignment operator
     const List &operator = (const List &rightSide); // MODIFIED
     // list traversal
     void listTraversalNode();
     void printInReverse(Node *Head);   // call function to print list in reverse
     bool sort(); // boolean function ascending
      // perform linear search
      int List::linearSearch();
     // empty operation
     bool empty() const;
     // insert and erase
     void insert(ElementType item, int pos);
     void erase(int pos);
      // deep erase
      void delete_all(); // 1. 'List' was incorrect here 2. 'delete' is a C++ keyword
     // output
     void display(ostream &out) const;

private:

     int mySize; // current size of list store in myArray
     //ElementType myArray[CAPACITY]; // array to store list of elements
     int myCapacity; // capacity of array
     Node *myArray; // pointer to dynamic array
    //  Node *date; <----------------- what is this
     
};


#endif

***************************************************

#include <cassert>
#include <iostream>
#include <fstream>
#include <list>
#include <new>
#include <string>
#include "list.h"

using namespace std;

// prototype for ouput operator
//ostream &operator << (ostream &out, const List &alist);<-- you don't need this one

// definition of class constructor
List::List(int maxSize) : mySize(0), myCapacity(maxSize) {

     myArray = new Node;
     assert(myArray != 0);

}

// definition of class destructor
List::~List() { // CHANGED

    // delete [] myArray;
     
     NodePointer prev, first, ptr; //'=' ?

     ptr = myArray->get_first();

     while(ptr != 0) {
          ptr = prev -> next;
          delete prev;
          prev = ptr;

     }

   
     
}  

// definition of copy constructor
/*
List::List(const List &origList) : mySize(origList.mySize), myCapacity(origList.myCapacity) {

         myArray = new Node;

}
*/
// you can only have *one*
// deep copy

List::List(const List& origList) { // ADDED

     mySize = origList.mySize;
     //get_first = 0; What's that?

     if(mySize == 0) return;
     NodePointer origPtr, lastPtr;
     first = new Node;
     first->data = origList.first->data;
     lastPtr = first;
     origPtr = origList.first->next;

     while(origPtr != 0) {

          lastPtr -> next = new Node;
          lastPtr->data = origPtr->data;
          origPtr = origPtr->next;
          lastPtr = lastPtr->next;

     }

}

// definition of assignment operator
const List &List::operator = (const List &rightSide) { // CHANGED

     /*

     // check that not self-assignment
     if(this != &rightHandSide) {
          // allocate new array if necessary
          if(myCapacity != rightHandSide.myCapacity) {
               delete [] myArray;
               myCapacity = rightHandSide.myCapacity;
               myArray = new Node;
               // check if memory available
               if(myArray == 0) {
                    cerr << " Inadequate memory to allocate stack. \n";
                    exit(1);

               }

          }
          // copy rightHandSide's list elements into this new array
          mySize = rightHandSide.mySize;

     }
     return *this;

      */

     mySize = rightSide.mySize;
     //get_first = 0; // first is a member function, so what's that?

     if(mySize == 0) return *this;
     if(this != &rightSide) {

          this -> ~List();
          NodePointer origPtr, lastPtr;
          first = new Node; // copy first node -> 'Node' has *no* constructor...
          first->data = rightSide.first -> data;
          lastPtr = first;
          origPtr = rightSide.first -> next;

          while(origPtr != 0) {
               lastPtr -> next = new Node;
               lastPtr->data = origPtr -> data;
               origPtr = origPtr -> next;
               lastPtr = lastPtr -> next;

          }

     }
     return *this;

}

// definition of empty
bool List::empty() const {

     return mySize == 0;

}

// definition of display
void List::display(ostream &out) const {

     out << "\n\n The elements in the list are: ";

     NodePointer ptr = get_first(); // MODIFIED
     while(ptr != 0) {
          //out << ptr->date << " "; <----- do you mean *date* or *data* ??
            out<<ptr->data<<" "; // MODIFIED
                 ptr = ptr->next;

     }
     out << " ";

}



// definition of insert
void List::insert(ElementType item, int pos) {

     if(mySize == myCapacity) {
          cerr << " No space for linked list element, terminating execution. \n";
          exit(1);

     }
     if(pos < 0 || pos > mySize) {
          cerr << " Illegal location to insert -- " << pos << ". Linked list unchanged. \n";
          return;

     }

     // first shift array elements right to make room for item
     for(int i = mySize; i > pos; i--)
          myArray[i] = myArray[i - 1];
            mySize++;

}
// perform linear search
int List::linearSearch() { // added function

     ElementType dataVal;
     cout << " Type in value to search-> ";
     cin >> dataVal;

     int loc;
     NodePointer temp = get_first(); // MODIFED <-- instead of *first* cauze it returns first pointer :)
     for(loc = 0; loc < mySize; loc++) {
          if(temp->data == dataVal)
            {       cout << " Element located at location. ";
     return loc;
        }
          else {
               temp = temp -> next;
               cout << "\n The element is not in the list. ";
               return -1;
            }
     }

}
// traverse the list
void List::listTraversalNode() {

     NodePointer ptr = myArray->get_first(); // need to declare it with a type!
     NodePointer first = ptr;

     while(ptr != 0) {
          ptr = ptr -> next; // appropriate statements to process ptr -> data

     }

     NodePointer newptr = new Node; // newptr of type NodePointer

     NodePointer predptr = NULL;
     if(predptr != 0) { // not inserting at front
          newptr -> next = predptr -> next;
          predptr -> next = newptr;

     }
     else { // inserting at front
          newptr -> next = first;
          first = newptr; // reset first

     }

     if(predptr != 0) { // not deleting first node
          ptr = predptr -> next;
          predptr -> next = ptr -> next; // bypass

     }
     else { // deleting first node
          ptr = first;
          first = ptr -> next; // reset first

     }
     delete ptr; // return node to heap

}

// reverse print of list
void List::printInReverse(Node *Head) {

     if (Head == 0){
     cout << "List is empty." << endl;
     }
     //int count = 0;
     Node *printPtr = new Node;
     printPtr = Head;

     while(printPtr != 0) {
     printPtr = printPtr->next; //advance pointer to next location in list...
       
      }
 
}

// boolean value function ascending for node element
// the purpose of this function is to generate a boolean-valued
// function that determines whether the data items in the linked
// list are arranged in ascending order.
// sorts list in ascending order
bool List::sort() // new added function to perform above specs
{
     for(NodePointer temp = get_first(); temp != NULL; temp = temp->next)
     {
          NodePointer min = temp;

        /*  for(NodePointer current = min; current != NULL; current = current -> next)
          {
               if(current->NodePointer < min->data)min=current; //<-------- from where did you get *info* variable?
          }*/

          ElementType temp_var = temp -> data;
          temp -> data = min -> data;
          min -> data = temp_var;
     }
      return true;
           return false;

}
// definition of erase
void List::erase(int pos) {

     if(mySize == 0) {
          cerr << " List is empty \n";
          return;

     }
     if(pos < 0 || pos >= mySize) {
          cerr << " Illegal location to delete -- " << pos << ". List unchanged. \n";
          return;

     }

      // decrease list size
     mySize--;

}

// deep delete
// 'delete' is a keyword!
void List::delete_all() {

     int index;

     cout << "\n Type in index between 0 and mySize - 1 of the Node to be deleted-> ";
     cin >> index;

     if(index < 0 || index >= mySize) {

          cout << "\n Illegal location to delete. ";
          return;

     }
     mySize = -1;
     NodePointer ptr, prevPtr;
     prevPtr = myArray->first;

     if(index == 0) {

          ptr = first;
          first = ptr->next;
          delete ptr;
     }
     else {
          for(int i = 0; i < index; i++)
               prevPtr = prevPtr->next;
          ptr = prevPtr->next;
          prevPtr->next = ptr->next;
          delete ptr;

     }
}

int main() {

     int myCapacity = 42; // It is always a good idea to *declare* the variables you are using

     // test the class constructor
     List intList;
     cout << " Constructing intList\n";

     // test empty and output of empty list
     if(intList.empty())
          cout << " Empty: \n" << intList << endl; // test output of empty list

     // test insert
     for(int i = 0; i < 9; i++) {
          cout << " Inserting " << i << " at position " << i/2 << endl;
          intList.insert(i, i/2); // insert i at position i/2

          // test output
          cout << intList << endl;

     }

     cout << " List empty? " << (intList.empty() ? "Yes" : "No") << endl;

     cout << "\nTry to insert at position -1" << endl;
     intList.insert(0, -1);

     cout << "\nTry to insert at position 10" << endl;
     intList.insert(0, 10);

     // test erase
     int index;
     cout << endl;

     while(!intList.empty()) {

          cout << " Give an index of a list element to remove: ";
          cin >> index;
          intList.erase(index);
          cout << intList << endl;

     }
     cout << " List is empty. " << endl;

     cout << "\nInserting " << myCapacity << " integers\n";

     for(int j = 0; j < myCapacity; j++)
          intList.insert(j, j);

     cout << " Attempting to insert one more integer: \n";
     intList.insert(-1, 0);
     return 0; //<-------- add this
      system("pause");


}


But, from what I saw, I wasn't optimistic enough to run that code...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now