Mar 11, 2011 at 8:33pm UTC
I tried that code and I get seg fault
Mar 11, 2011 at 9:07pm UTC
On your original post, line 15 -- m_temp is dereferenced but it is uninitialized.
Mar 11, 2011 at 9:10pm UTC
I was writing the proper function when I noticed this:
new Address(fname,lname,phnum,email,m_head );
I take it Address is your class for nodes. Why are you passing the list's head to every node you create? In a singly linked list, a node only needs to know which node comes after it. I'll just write the function for a type Address that doesn't take any nodes as constructor arguments:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void Addr_list::insert(string fname, string lname, string phnum, string email)
{
Address *new_address = new Address(fname, lname, phnum, email);
if (m_head == NULL) {
m_head = new_address;
m_head->next = NULL;
return ;
}
Address *curr = m_head;
Address *prev;
while (curr != NULL && lname > curr->m_lname) {
prev = curr;
curr = curr->next;
}
prev->next = new_address;
new_address->next = curr;
}
Be warned that I didn't test this. Also notice that prev isn't really necessary, but it makes things more readable.
Last edited on Mar 11, 2011 at 9:11pm UTC
Mar 11, 2011 at 10:02pm UTC
Alright, Filipe I tried your code in mine, IT prints out the first node for every node i enter, except if i enter b b b b in before a a a a i will get a seg fault
sample input :
insert a a a a
insert b b b b
insert c c c c
print
sample output :
a a: a, a
a a: a, a
a a; a, a
sample input:
insert b b b b
insert a a a a
print
sample output:
segmentation fault
Here is my complete 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
//addr_list.h
//Kauffman,Tyler
//tkauffman
#ifndef ADDR_LIST_H
#define ADDR_LIST_H
class Addr_list
{
public :
Addr_list();
~Addr_list();
void insert(string fname,string lname,string phnum, string email);
void remove(string fname,string lname);
void print();
private :
struct Address
{
public :
Address (string fname, string lname, string phnum, string email)
{m_fname=fname, m_lname=lname, m_phnum=phnum, m_email=email;}
string m_fname,m_lname,m_phnum,m_email;
Address *m_next;
};
Address *m_head;
};
#endif
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
//abook.cpp
//Kauffman,Tyler
//tkauffman
#include <iostream>
#include <string>
#include<algorithm>
using namespace std;
#include "addr_list.h"
int main()
{
string insert = "insert" ;
int i;
string command;
string fname,lname,phnum,email;
Addr_list *list = new Addr_list;
while (cin>>command)
{
if (command == "insert" )
{
cin >> fname>>lname>>phnum>>email;
list -> insert (fname,lname,phnum,email);
}
if (command == "remove" )
{
//cout << "remove was called " <<endl;
list ->remove(fname,lname);
}javascript:editbox1.editSend()
if (command =="print" )
{
list ->print();
}
else if (command != "insert" && command != "remove" )
{
cerr<< "Wrong" <<endl;
}
}
}
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
//addr_list.cpp
//Kauffman,Tyler
//tkauffman
#include <iostream>
using namespace std;
#include "addr_list.h"
Addr_list::Addr_list()
{
m_head = NULL;
}
Addr_list::~Addr_list()
{
Address *ptr = m_head;
while (ptr != NULL)
{
Address *temp;
temp = ptr;
ptr = ptr->m_next;
delete temp;
}
}
void Addr_list::insert(string fname,string lname, string phnum, string email)
{
Address *new_address = new Address(fname,lname,phnum,email);
if (m_head == NULL)
{
m_head = new_address;
m_head->m_next = NULL;
return ;
}
Address *curr = m_head;
Address *prev;
while (curr != NULL && lname >curr->m_lname)
{
prev = curr;
curr = curr->m_next;
}
prev->m_next = new_address;
new_address->m_next = curr;
}
void Addr_list::remove(string fname, string lname)
{
//cout<<"remove function was called " << endl;
}
// iterate through all the Nodes in the list and print each Node
void Addr_list::print()
{
Address *ptr = m_head;
cout << ptr->m_fname<<ptr -> m_lname<<": " <<ptr->m_phnum<<"," <<ptr->m_email<<endl;
ptr = ptr->m_next;
// cout << "size is: " << size << endl;
}
Last edited on Mar 11, 2011 at 10:11pm UTC
Mar 11, 2011 at 10:42pm UTC
I narrowed it down further still;
The seg fault only happens when m_head needs be be replaced. I will keep trying
Edit once more; I figure that it is because I dont have the condition what if m_head is bigger than m_head->m_next->m_lname..
duh!! ill keep u guys posted!!
Last edited on Mar 11, 2011 at 10:43pm UTC
Mar 12, 2011 at 12:25am UTC
True. I forgot to cater for the case when the head is the value to be replaced (which means you can't dereference prev because it's dangling). I think you can take it from there.
Mar 12, 2011 at 12:52am UTC
NVM! it was in the first if statement duh again, brains are definitely fried!!
You helped me so much Thank you.
1 2
if (m_head == NULL || lname < m_head->m_lname)
Last edited on Mar 12, 2011 at 1:05am UTC