Delete Nodes from a linked list

For my program I have to enter commands followed by expressions
the commands are:
insert (insert names/phone numbers/emails into the list alphabetically)
lookuplast (look up person by last name)
lookupfirst (" ")
remove (remove node by first and last name )
print

I Have everything going the way I want except the remove function, is it the same algorithm as the insert-sorted function??

Code below:
any help would be greatly appreciated i have 3 hours to wrap this up!

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
//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 || lname < m_head->m_lname)
    {
	new_address->m_next = m_head;
	m_head = new_address;
	return;
    }
    Address *curr = m_head;
    Address *prev;
    while (curr != NULL && lname >curr->m_lname)
    {
	prev = curr;
	curr = curr->m_next;
    }
        new_address->m_next = curr;
	prev->m_next = new_address;
	
 } 
 
 void Addr_list::lookupfirst(string fname)
 {
         
	 Address *ptr = m_head; 
         for (Address *cur = m_head; cur != NULL; cur = cur->m_next)
	 {
	        if (fname == cur->m_fname)
		{
	        cout <<cur->m_fname<<" "<<cur->m_lname<<": " << cur->m_phnum
		<< " "<<cur->m_email<<endl;
		}
	 }

 }
 
 void Addr_list::lookuplast(string lname)
 {
	  Address *ptr = m_head; 
         for (Address *cur = m_head; cur != NULL; cur = cur->m_next)
	 {
	        if (lname == cur->m_lname)
		{
	        cout <<cur->m_fname<<" "<<cur->m_lname<<": " << cur->m_phnum
		<< " "<<cur->m_email<<endl;
		}
	 }
  }

void Addr_list::remove(string fname, string lname)
{
	  Address *temp;
    if (m_head == NULL || lname < m_head->m_lname)
    {
	temp->m_next = m_head;
	m_head = temp;
	return;
    }
    Address *curr = m_head;
    Address *prev;
    while (curr != NULL && lname >curr->m_lname)
    {
	prev = curr;
	curr = curr->m_next;
    }
        temp->m_next = curr;
	prev->m_next = temp;
	
 } 
 
// iterate through all the Nodes in the list and print each Node
void Addr_list::print()
{
     for (Address *cur = m_head; cur != NULL; cur = cur->m_next)
	{
	cout <<cur->m_fname<<" "<<cur->m_lname<<": " << cur->m_phnum
		<< " "<<cur->m_email<<endl;	
	}
	
	
        // cout << "size is: " << size << 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
//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 lookupfirst(string fname);
	void lookuplast(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
44
45
46
47
48
49
50
51
52
53
//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")
		{
		cin>>fname>>lname;
		list ->remove(fname,lname);	
		}
		if(command == "print")
		{
		list ->print();	
		}
		if(command == "lookupfirst")
		{
		cin >> fname;
		list ->lookupfirst(fname);
		}
		if (command == "lookuplast")
		{
		cin >>lname;
		list ->lookuplast(lname);
		}	
		else if (command != "insert" && command != "remove"&& command != "lookupfirst" && command != "print")
		{
		cerr<< "Wrong"<<endl;
		}
	}

}



The delete algorithm works like this:

- Iterate through the list keeping a pointer to the current node you are looking at and the previous node
- For each of the nodes, check whether the name matches (i.e. you want to delete it)
- Set the previous node's next pointer to equal the current node's next pointer (unlinking the current node)
- Free the memory for the current node
Heres what i have
It is not working
any idea why?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Addr_list::remove(string fname, string lname)
{
	Address *cur, *prev;
	prev =NULL;
	for (Address *cur = m_head; cur != NULL;prev = cur, cur = cur->m_next)
	{
	    if(fname == cur->m_fname && lname == cur->m_lname)
	    {		    
		if (prev == NULL)
		{
		m_next = cur->m_next;	
		}
		else
		{
		cur->m_next =NULL;
		delete cur;
		cur = NULL;
		
		}
	    }	
	}
}		
Last edited on
Oh brother, please tell us what happens when you try.
right sorry,
segmentation error after input:

insert tyler kauffman 530-420-3333 t@m.com
insert jammie over 530-444-4444 j@m.com
insert bugsy bug 999-999-2222 b@m.com

remove tyler kauffman

output:
segmentation error.
Okay I have it now where I can remove any node BUT the first one, When i try to remove the first node I get a seg error,

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
void Addr_list::remove(string fname, string lname)
{
	Address *cur, *prev;
	cur = m_head;
	prev = cur ->m_next;
	while(prev->m_lname != lname && prev->m_fname != fname)
	{
	cur = cur ->m_next;
	prev=prev ->m_next;
	}
	if(m_head ->m_lname == lname &&m_head->m_fname ==fname)
	{
	prev = cur;
	m_head = m_head ->m_next;
	delete prev;
	}
	else
	{
	cur ->m_next = cur->m_next->m_next;
	delete prev;
	}
	
	prev = NULL;
	cur = NULL;
}		
Topic archived. No new replies allowed.