Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Program Syntax (behavior) Check

Program runs.

Would liked checked for accuracy.  Please advise.  Del

**************code**************************

#ifndef LIST_H
#define LIST_H

#include <iostream>
#include <fstream>

using namespace std;

typedef int ElementType;

class Node {

public:

     ElementType data;
     Node *next;

     Node* get_first() const { return first;}
       Node* first;
     int mySize;

};

typedef Node *NodePointer;
class List:public Node{

     // prototype for ouput operator
      friend ostream &operator << (ostream &out, const List &aList)  {
              aList.display(out);
              return out;

      }

public:

     // constructor
     List(int maxSize = 1024);
       // destructor
     ~List();
     // deep copy
     List(const List &origList);
     // assignment operator
     const List &operator = (const List &rightSide);
     // list traversal
     void listTraversalNode();
       // call function to print list in reverse
       void printInReverse(Node *Head);
       // boolean function ascending
     bool bubbleSort();
     // 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();
     // output
     void display(ostream &out) const;

private:

     int mySize; // current size of list store in myArray
     int myCapacity; // capacity of array
     Node *myArray; // pointer to dynamic array
     
};

#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() {

      NodePointer prev, first, ptr;
      ptr = myArray->get_first();
      while(ptr != 0) {
        ptr = prev->next;
        delete prev;
        prev = ptr;

    }

}  

// deep copy

List::List(const List& origList) {

     mySize = origList.mySize;
     
     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;

     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();
     while(ptr != 0) {
         
            out<<ptr->data<<" ";
            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() {

     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::bubbleSort() // new added function to perform above specs
{
     for(NodePointer temp = get_first(); temp != NULL; temp = temp->next)
     {
          NodePointer min = temp;

              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;
      system("pause");


}


**************************************
0
edelossantos
Asked:
edelossantos
  • 2
2 Solutions
 
rajeev_devinCommented:
>> int List::linearSearch() {
This function will not search all elements in the llist.
0
 
rajeev_devinCommented:
It should have been something like this

int List::linearSearch() {
      
      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;
}
0
 
edelossantosAuthor Commented:
Thank you very much rajeev_devin.  Del
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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