c++ - Ordered Linked List errors -


i know of code isn't working when first run program , first string read in text field program errors out.the main function passing string "insert list function" in implementation. program suppose insert node every time string read in text file. program call call delete function know isn't working yet(that why commented out). trying find error created when insert function called. main function has while loop creates node every text entry , passes node 1 one sorted in abc order.

header file:

 #include <string> using namespace std;  struct node { string data; node * next; };  class list { public: list(); ~list(); bool insert(string); bool delete(string); void print(); bool edit(string, string); private:  node * head;  node * cur;  node * trailer;  }; 

implementation:

#include <iostream> #include <string> #include <fstream> #include "list.h" using namespace std;  list::list():head(null) {}  list::~list() {} bool list::insert(string data) { node* newnode = new node;  if (newnode == null) {     cout << "error: memory allocation failed" << endl;     return false; }  newnode->data = data; cur = head; trailer = null; if (head == null) {     //cout << "head null" << endl;     head = newnode;     cout << head -> data << endl;     newnode->next = null;     //return true; } while (newnode->data > cur->data && cur -> next != null) {     trailer = cur;     cur = cur->next;  }  if (cur->next == null) {     cur->next = newnode;     newnode->next = null;     return true; }  else {     trailer->next = newnode;     newnode->next = cur;     return true;    }     }   bool list::delete(string data) { /*node *temp = head->next; while (head != null) {     delete head;     head = temp;     temp = head->next; } return true;*/  } bool list::edit(string datadelete, string datainsert) { delete(datadelete); insert(datainsert); return true; } void list::print() { (node * count = head; count != null; count = count->next) {     cout << count->data << endl; } } 

@deepak right, problem when you're inserting first element head variable null , cur set value of head.

to fix can place

cur = head; trailer = null; 

after condition

if (head == null) {     //cout << "head null" << endl;     head = newnode;     cout << head -> data << endl;     newnode->next = null;     //return true; } 

also there error when try insert element should placed in beggining (value smaller other value in list). happen when condition of loop

trailer = null; while (newnode->data > cur->data && cur -> next != null) { ... } 

is false in first call, trailer null. fix need check trailer variable, this

if (trailer == null) {     newnode->next = head;     head = newnode;     return true; } 

as result code of insert

bool list::insert(string data) {     node* newnode = new node;      if (newnode == null)     {         cout << "error: memory allocation failed" << endl;         return false;     }      newnode->data = data;     if (head == null)     {         head = newnode;         newnode->next = null;         return true;     }      cur = head;     trailer = null;     while (newnode->data > cur->data && cur -> next != null)     {         trailer = cur;         cur = cur->next;     }      if (trailer == null) {         newnode->next = head;         head = newnode;         return true;     }      if (cur->next == null)     {         cur->next = newnode;         newnode->next = null;         return true;     }     else     {         trailer->next = newnode;         newnode->next = cur;         return true;     } } 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -