Monday, 15 September 2014

linked list - C++ delete error -



linked list - C++ delete error -

i'm new c++ , i'm trying code linked-list based queue. test programme worked fine until added delete dequeue function. error: malloc: *** error object 0x7f8a61403a00: pointer beingness freed not allocated

any help appreciated.

#include <string> #include "queue.h" #include <iostream> using namespace std; queue::queue() { // constructs new empty queue. current_size = 0; // number of elements in queue front_p = null; // first element in queue back_p = null; // lastly element in queue } queue::queue( const queue& q ) {// re-create constructor. // nil re-create if queue empty if (q.current_size == 0) { return; } // queue not empty else { node * p = q.front_p; node * n; // assign front end pointer n = new node(p -> data, null); front_p = n; p = p -> next; // middle elements while (p -> next != null) { n -> next = p; n = new node(p -> data, null); p = p -> next; } // assign pointer n = new node(p -> data, null); back_p = n; current_size = q.current_size; } } void queue::enqueue( int item ) { // enqueues <item> node * n = new node(item, null); // first item in queue, front end & ptrs point same element if (back_p == null) { front_p = n; back_p = n; } else { back_p -> next = n; back_p = n; } current_size++; } int queue::dequeue() { // removes , returns front end item. // empty queue if (current_size == 0) { homecoming -1; } int item = front(); node * p = front_p; if (current_size == 1) { front_p = null; back_p = null; delete p; current_size--; homecoming item; } else { front_p = front_p -> next; delete p; current_size--; homecoming item; } }

test file

//test file #include <iostream> #include "queue.h" using namespace std; int main(void) { queue q1; cout << "create new queue q1" << endl; cout << "size of q1 \t" << q1.size() << endl; cout << "is q1 empty? \t" << q1.empty() << endl; cout << endl << endl; cout << "enqueue \t1,2,3,4,5" << endl; q1.enqueue(1); q1.enqueue(2); q1.enqueue(3); q1.enqueue(4); q1.enqueue(5); cout << "front of q1 \t" << q1.front() << endl; cout << "size of q1 \t" << q1.size() << endl << endl; cout << "q2 deep re-create of q1" << endl; queue q2(q1); cout << "front of q2 \t" << q2.front() << endl; cout << "size of q2 \t" << q2.size() << endl << endl; cout << "removed 4 q1" << endl; q1.remove(4); cout << "removed 2 q2" << endl; q2.remove(2); cout << endl; cout << "print out remaining elements of q1" << endl; int n = q1.size(); for(int i=0; < n; i++) { cout << q1.dequeue() << " "; } cout << endl << endl; cout << "print out remaining elements of q2" << endl; n = q2.size(); for(int i=0; < n; i++) { cout << q2.dequeue() << " "; } cout << endl; }

header file

class queue { public: queue(); // constructs new empty queue. queue( const queue& q );// re-create constructor. ~queue();// destructor. void enqueue( int item ); // enqueues <item>. int dequeue(); // dequeues front end item. int front(); // returns front end item without dequeuing it. bool empty(); // returns true iff queue contains no items. int size(); // returns current number of items in queue. bool remove(int item); // if <item> occurs in queue, removes // first occurrence of <item> , returns true; otherwise returns false. private: class node // node type linked list { public: node(int new_data, node * next_node){ info = new_data ; next = next_node ; } int info ; node * next ; }; node * front_p ; // pointer (node containing the) next item // dequeud, or null if queue empty. node * back_p ; // pointer (node containing the) lastly item // enqueued, or null if queue empty. int current_size ; // current number of elements in queue. };

there other issues code, immediate 1 copy-constructor broken.

in cycle

while (p -> next != null) { n -> next = p; n = new node(p -> data, null); p = p -> next; }

p pointer element of source queue q. meanwhile, n element of new queue. doing n -> next = p; making new queue nodes link source queue node chain. creates nonsensical node linkage structure, falls apart later.

c++ linked-list queue delete-operator

No comments:

Post a Comment