Singly Linked List Delete At A Certain Location

Whenever I run this code, I try to make it delete the first node and it doesn't. if the user chooses to delete at a certain location it seems that it reads the one after it and THEN deletes that one.
If the user chooses to delete the second one, the third one gets deleted in the list.
What have I done wrong?

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
  Node *current = new Node;
			Node *previous = new Node;
			Node *deletePtr = new Node;
			current = first;

			if (location < 1 || location > Length() + 1) {
				cout << "That is an invalid location. There is nothing for me to do." << endl;
			} // else if 1
			else if (Length() == 1) {
				DeleteFirst();
			} // else if 2
			else if (location == Length()) {
				DeleteLast();
			} // else if 3
			else {
				previous = first;
				current = previous->next->next;
				int ii = 2;
				while (ii <= location) {
					previous = previous->next;
					current = current->next;
					ii++;
				} // while 
			} // else

			deletePtr = previous->next;
			previous->next = current;
			delete deletePtr;


		} // Delete() 
Last edited on
> but then now this happened.
¿what happened?


http://www.eelis.net/iso-c++/testcase.xhtml
A testcase that does not reproduce the problem is useless (...)
A testcase consisting of randomly copy&paste'd bits of code that you thought were relevant can obviously not reproduce the problem.
put a complete, compilable code with all the necessary input to reproduce your problem


Also, explain your implementation.
It seems that you are using a circular list with a header cell

by the way,
1
2
Node *current = new Node;
current = first;
memory leak.
Topic archived. No new replies allowed.