pointer-based implementation of ADT list with nodes

Hey guys, I was just trying to get this implementation from my textbook to work by creating a list and testing the methods, but I'm confused on how to actually go about creating the list now that I've implemented it in a pointer-based way and was wondering what I would need to change from when I was just using a standard list.

I can compile my program but when I try to insert values into my list it crashes. This is what I'm using in my main function, which I haven't modified since I've implemented the pointer-based ADT:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include "list.h" // ADT list operations

using namespace std;

int main()
{
   List         aList;
   ListItemType dataItem;
   bool         success;


   try {
       aList.insert(1, 1);
       //aList.insert(2, 20);
       //aList.insert(3, 2);
       //aList.insert(4, 6);
       //aList.insert(5, 3);
       //aList.insert(6, 15);
   }
   catch( ListException & e1)
   {
          cerr << e1.what();
   }
   catch ( ListIndexOutOfRangeException & e2)
   {
       //handling the exception here
   }
   catch (...)
   {
         //handling all the other exceptions here
   }
   aList.retrieve(1, dataItem);

   cout <<"The item at position 1 is: "<< dataItem <<endl;

   return 0;

}


here is the implementation cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include "list.h"  // header file
#include <cstddef>
#include <new>

List::List(): size(0), head(NULL)
{

}  // end default constructor

List::List(const List& aList)
    : size(aList.size)
{
    if(aList.head == NULL)
        head = NULL;
    else{
        head = new ListNode;
        head->item = aList.head->item;

        ListNode *newPtr = head;

        for(ListNode *origPtr = aList.head->next;
            origPtr != NULL; origPtr = origPtr->next){
                newPtr->next = new ListNode;
                newPtr = newPtr->next;
                newPtr->item = origPtr->item;
            }

        newPtr->next = NULL;
    }

}

List::~List(){
    while (!isEmpty())
        remove(1);
}

bool List::isEmpty() const
{
   return size == 0;
}  // end isEmpty

int List::getLength() const
{
   return size;
}  // end getLength


List::ListNode *List::find(int index) const
{
    if((index < 1) || (index > getLength())){
        return NULL;
}   else{
        ListNode *cur = head;
        for (int skipper = 1; skipper < index; ++skipper){
            cur = cur->next;
            return cur;
        }
}
}

void List::retrieve(int index, ListItemType& dataItem) const
    throw (ListIndexOutOfRangeException)
{
    if ( (index <1) || (index > getLength())){
        throw ListIndexOutOfRangeException(
        "ListIndexOutOfRangeException:retrieve index out of range");
    }else{
        ListNode *cur = find(index);
        dataItem = cur->item;
    }

}

void List::insert(int index, const ListItemType& newItem)
    throw(ListIndexOutOfRangeException, ListException)
{
    int newLength = getLength() + 1;

    if((index < 1) || (index > newLength)){
        throw ListIndexOutOfRangeException(
        "ListIndexOutOfRangeException:retrieve index out of range");
    }else{
        try
        {
               ListNode *newPtr = new ListNode;
               size = newLength;
               newPtr->item = newItem;

               if(index == 1){
                    newPtr->next = head;
                    head = newPtr;
               }else{
                    ListNode *prev = find(index - 1);
                    newPtr->next = prev->next;
                    prev->next = newPtr;
               }
        }
        catch (bad_alloc e)
            {
                throw ListException("ListException: insert cannot allocate enough memory.");
            }
    }
}

void List::remove(int index) throw (ListIndexOutOfRangeException)
{
    ListNode *cur;

    if( (index<1) || (index > getLength())){
        throw ListIndexOutOfRangeException(
              "ListIndexOutOfRangeException: remove index out of range");
    }else{
        --size;
        if(index == 1){
            cur = head;
            head = head->next;
        }else{
            ListNode *prev = find(index-1);
            cur = prev->next;
            prev->next = cur->next;
        }

        cur->next = NULL;
        delete cur;
        cur = NULL;

    }
}
Looks like the list takes a ListItemType as data but you are passing an int.
Topic archived. No new replies allowed.