link list

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
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <string>

using namespace std;

class LinkedList{
private:
	struct node{
		int data;
		node* next;
	};
	node* head;
	node* iterator_current;
public:
	LinkedList();

	void add(const int &);
	int remove();
	bool isEmpty() const;

	bool more();
	int next();
	void setiterator();
	void insertbeforecurrent( const int& );
	bool delete_current( int& );
};

LinkedList::LinkedList(){
	head = NULL;
	iterator_current = NULL;
}

void LinkedList:: add( const int& data ){
	node* tmp;
	node* curr;

	tmp = new node;
	tmp->data = data;
	tmp->next = NULL;
	if( head != NULL ){
		curr = head;
		while( curr->next ){
			curr = curr->next;
		}
		curr->next = tmp;
	}
	else
		head = tmp;
}

bool LinkedList::isEmpty()const{
	if( head == NULL )
		return true;
	else
		return false;
}

int LinkedList::remove(){

	node* tmp;
	int data;
	data = head->data;
	tmp = head;
	head = head->next;
	delete tmp;
	return data;
}

bool LinkedList::more(){
	if( iterator_current != NULL )
		return true;
	else
		return false;
}

void LinkedList::setiterator(){
	iterator_current = head;
}

int LinkedList::next(){
	int tmp = iterator_current->data;
	iterator_current = iterator_current->next;
	return tmp;
}

bool LinkedList::delete_current( int& retdata ){

	node* curr;
	node* prev;
	curr = head;
	prev = NULL;
	if( iterator_current == NULL )
		return false;
	while( curr != iterator_current ){
		prev = curr;
		curr = curr->next;
	}
	if( curr == NULL )
		return false;
	if( prev ){
		prev->next = curr->next;
		retdata = curr->data;
		delete curr;
		return true;
	}
	else{
		head = curr->next;
		retdata = curr->data;
		delete curr;
		return true;
	}
}

void LinkedList::insertbeforecurrent( const int& data ){

	node* tmp;
	tmp = new node;
	tmp->data = data;
	tmp->next = NULL;
	node* curr;
	node* prev;
	curr = head;
	prev = NULL;
	while( curr != iterator_current ){
		prev = curr;
		curr = curr->next;
	}
	if( prev == NULL ){
		head = tmp;
	}
	else
		prev->next = tmp;
	tmp->next = curr;
}

int main(){

	LinkedList d1;

	cout << "Enter number : ";
	int number;
	while( cin >> number ){
		d1.add( number );
		cout << "Enter number : ";
	}

	d1.setiterator();
	int value = d1.next();
	int foot = 1000;
	int foot1 = 50;
	d1.insertbeforecurrent( foot );
	d1.setiterator();
	while( d1.delete_current( foot1 ) )
	while( d1.more() ){
		int x = d1.next();
		cout << "Node contain : " << x << endl;
	}

	

	system( "pause" );
	return 0;
}


why my main delete current cannot work?
the problem is that you don't reset iterator_current to a something valid. After the call to delete_current() it points to an invalid node
so i have to add

d1.setiterator() back?
or?
i wan to delete certain value only
In delete_current() you may call next() after the while loop.

Note that calling next() is somewhat dangerous since it would crash if iterator_current doesn't point to a valid node
Topic archived. No new replies allowed.