Aborted (core dumped)
Jan 3, 2013 at 4:49am UTC
Hi, I am trying to implement a linked list. Here's my code:
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
1 #include <sstream>
2 #include "ListLL.h"
3
4 using namespace std;
5
6 LList::LList():ListBase()
7 {
8 _head = NULL;
9 }
10
11 LList::LList(ListBase *listPtr){
12 _size = listPtr->getLength();
13 _head = NULL;
14 for (int i = 1; i <= _size; i++){
15 int data;
16 listPtr->retrieve(i, data);
17 insert(i, data);
18 }
19 }
20
21 LList::~LList()
22 {
23 while (!isEmpty())
24 remove(1);
25 }
26
27 bool LList::insert(int userIdx, const int & newItem)
28 {
29
30 if ( (userIdx < 1) || (userIdx > _size + 1) )
31 return false ;
32
33 ListNode *newPtr = new ListNode;
34 newPtr->item = newItem;
35 newPtr->next = NULL;
36
37 _size++;
38
39 if (userIdx == 1){
40 newPtr->next = _head;
41 _head = newPtr;
42 } else {
43 ListNode *prev = traverseTo(userIdx-1);
44 newPtr->next = prev->next;
45 prev->next = newPtr;
46 }
47
48 return true ;
49 }
50
51 bool LList::remove(int userIdx)
52 {
53
54 ListNode *cur;
55
56 if ( (userIdx < 1) || (userIdx > _size) )
57 return false ;
58
59 --_size;
60 if (userIdx == 1) {
61 cur = _head;
62 _head = _head->next;
63 } else {
64 ListNode *prev = traverseTo(userIdx - 1);
65 cur = prev->next;
66 prev->next = cur->next;
67 }
68
69 delete cur;
70
71 return true ;
72 }
73
74
75 bool LList::retrieve(int userIdx, int & dataItem)
76 {
77 if ( (userIdx < 1) || (userIdx > _size) )
78 return false ;
79
80 ListNode *cur = traverseTo( userIdx);
81 dataItem = cur->item;
82
83 return true ;
84 }
85
86 LList::ListNode* LList::traverseTo( int index )
87 {
88 if ( (index < 1) || (index > _size) )
89 return NULL;
90
91 ListNode* cur = _head;
92 for (int skip = 1; skip < index; ++skip)
93 cur = cur->next;
94 return cur;
95 }
96
97 string LList::toString()
98 {
99 ostringstream os;
100 ListNode *cur;
101
102 cur = _head;
103 os << "[ " ;
104 while ( cur != NULL) {
105 os << cur->item << " " ;
106 cur = cur->next;
107 }
108 os << "]" ;
109
110 return os.str();
111 }
I don't think there's any problem with my implementation. When I test my copy constructor with the following code,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
1 #include <iostream>
2 #include "ListArray.cpp"
3 #include "ListLL.cpp"
4 using namespace std;
5
6 int main()
7 {
8 LList list1;
9
10 list1.insert(1, 333);
11 list1.insert(1, 111);
12 list1.insert(3, 777);
13 list1.insert(3, 555);
14
15 cout << "List 1: " << list1.toString() << endl << endl;
16 cout << "Duplicate list 1 to list 2." << endl;
17 LList list2(list1);
18 cout << "List 2: " << list2.toString() << endl;
19
20 return 0;
21 }
I get the following output:
List 1: [ 111 333 555 777 ]
Duplicate list 1 to list 2.
List 2: [ 111 333 555 777 ]
Aborted (core dumped)
I didn't mean to print out the underlined sentence, and other than this, the output is correct. I am very curious why this sentence appears?
Jan 3, 2013 at 5:05am UTC
Have you tried running your program thru your debugger. The debugger should be able to tell you exactly where it detects the error. And you can view the values of your variables at the time of the crash.
Also could you fix your code posting, repost without the line numbers.
Jan 3, 2013 at 5:11am UTC
This error is in destruction you don't posted the code for function isEmpty()
.
While posting a problem post your total code other wise users are unable to help you correctly.
Jan 3, 2013 at 5:29am UTC
I don't think there's any problem with my implementation. When I test my copy constructor with the following code,
The problem may be in your lack of implementation, since you don't actually supply a copy constructor.
Jan 3, 2013 at 7:02am UTC
Hi, all, maybe i forget to post the other two files.
ListBase.h
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
1 #ifndef _LISTBASE_H_
2 #define _LISTBASE_H_
3
4 #include <string>
5 using namespace std;
6
7 class ListBase {
8 protected :
9 int _size;
10
11 public :
12 ListBase() {
13 _size = 0;
14 }
15
16 bool isEmpty(){
17 return (_size == 0);
18 }
19
20 int getLength(){
21 return _size;
22 }
23
24 virtual bool insert(int index, const int & newItem) = 0;
25
26 virtual bool remove(int index) = 0;
27
28 virtual bool retrieve(int index, int & dataItem) = 0;
29
30 virtual string toString() = 0;
31
32 };
33
34 #endif
ListLL.h
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
1 #ifndef _LISTLL_H_
2 #define _LISTLL_H_
3
4 #include "ListBase.h"
5 #include <string>
6 using namespace std;
7
8 class LList : public ListBase {
9 private :
10 struct ListNode {
11 int item;
12 ListNode *next;
13 };
14
15 ListNode*_head;
16
17 ListNode* traverseTo( int index );
18
19 public :
20
21 LList();
22 LList(ListBase *listPtr);
23 ~LList();
24
25 bool insert(int index, const int & newItem);
26
27 bool remove(int index);
28
29 bool retrieve(int index, int & dataItem);
30
31 string toString();
32
33 };
34
35 #endif
The isEmpty() method is implemented in ListBase.h, and LList is actually inherited form ListBase class.
Jan 3, 2013 at 7:17am UTC
Your copy constructor is not correct and it is never called.
LList(ListBase *listPtr);
Use LList(LList &listPtr);
like this.
Jan 3, 2013 at 7:25am UTC
Copy constructor logic is also wrong try this updated code.
1 2 3 4 5 6 7 8 9
LList::LList(LList &listPtr){
// _size = listPtr.getLength();
_head = NULL;
for (int i = 1; i <= listPtr.getLength(); i++){
int data;
listPtr.retrieve(i, data);
insert(i, data);
}
}
Topic archived. No new replies allowed.