Linked list editing using remove and insert

I need to take the first linked list, and use the insert and remove functions to turn it into the second list that is commented. I cannot for the life of me figure out how to do this, because I dont know how to use the remove function or insert function more than once.

I had success removing one node, but I dont know how to add or remove anymore. If anyone could help I would appreciate it. I am just lost.

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  #include <iostream>
#include <string>
using namespace std;

struct Cafe
	{
	string item;
	int count;
	Cafe *link;
	};

typedef Cafe* CafePtr; 

void head_insert(CafePtr& head, string i, int c);
CafePtr searchnum(CafePtr& head, int target);
CafePtr searchstr(CafePtr& head, string target); 
void insert (CafePtr after_me, string i, int c);//insert a node in the list
void print (CafePtr head);						//print the list
void remove(CafePtr before, CafePtr discard);	//remove a node from the list

int main(){

	CafePtr head;
	CafePtr iter, h;

	head = new Cafe;
	head->link = NULL;

	head_insert(head, "QL", 3808);
	head_insert(head, "CHGHS", 1312);
	head_insert(head, "REI", 10466);
	head_insert(head, "CPT", 1678);
	head_insert(head, "NetApp", 6887);
	head_insert(head, "EJ", 36937);
	head_insert(head, "WFM", 41717);
	head_insert(head, "SAS", 6046);
	head_insert(head, "BCG", 1958);
	head_insert(head, "Google", 18500);

	print (head);

	//head ->item = "Google"; 
	//head ->count = 34311; //If i try to do this with other items, the program just crashes

	CafePtr after_me = searchstr(head, "Google");
	
	insert(after_me, "SAS", 6373);

	CafePtr before = searchstr(head, "Google"); 
	CafePtr discard = searchstr(head, "BCG");
	remove(before, discard);
	
	
	//Needs to turn into a linked list that looks like: (by using insert and remove)
	//head_insert(head, "CPT", 1896);
	//head_insert(head, "Ultimate", 1440);
	//head_insert(head, "EJ", 35114);
	//head_insert(head, "Hilcorp", 1012);
	//head_insert(head, "NetApp", 7426);
	//head_insert(head, "WFM", 43927);
	//head_insert(head, "BCG", 2314);
	//head_insert(head, "CHGHS", 1378);
	//head_insert(head, "SAS", 6373);
	//head_insert(head, "Google", 34311);



	cout << endl;
	print(head);
	return 0;

}

void head_insert(CafePtr& head, string i, int c)
{
	CafePtr temp_ptr;
	temp_ptr = new Cafe;

	temp_ptr -> item = i;
	temp_ptr -> count = c;

	temp_ptr -> link = head;
	head = temp_ptr;

}

CafePtr searchnum(CafePtr& head, int target)
{
	CafePtr here = head;

	if(here == NULL)
	{
		return NULL;
	}
	else
	{
		while(here->count !=target && here->link != NULL)
			here = here->link;
		if(here->count == target)
			return here;
		else
			return NULL;
	}
}

CafePtr searchstr(CafePtr& head, string target)
{
	CafePtr here = head;

	if(here == NULL)
	{
		return NULL;
	}
	else
	{
		while(here->item !=target && here->link != NULL)
			here = here->link;
		if(here->item == target)
			return here;
		else
			return NULL;
	}
}

void insert (CafePtr after_me, string i, int c)
{
	CafePtr temp_ptr;
	temp_ptr = new Cafe;
	temp_ptr->item = i;
	temp_ptr->count = c;

	temp_ptr->link = after_me->link;
	after_me->link = temp_ptr;			//this lets the previous link point to temp_ptr
}


void print (CafePtr head)
{

	for (CafePtr iter = head; iter !=NULL; iter=iter->link)
	{
		cout << (iter->item) << "\t" << (iter->count) << endl;
	}
}

void remove(CafePtr before, CafePtr discard)
{
	before->link = discard->link;
	delete discard;					//after adding this, you can remove any node but the head node
}
Whose idea is this remove()?
1
2
3
4
// List has A->B->C->D->E
remove( D, B );
// List has now A->deleted
// Somewhere, lost in memory are the D->C->D and E 



The search* methods can return NULL. Check for it.

1
2
3
4
5
6
7
8
CafePtr node = searchstr(head, "CPT");
if ( NULL != node ) insert( node, "Ultimate", 1440);

node = searchstr(head, "Ultimate");
if ( NULL != node ) insert( node, "SEP", 42);

node = searchstr(head, "Google");
if ( NULL != node ) node->count = 7;
Last edited on
Topic archived. No new replies allowed.