C++ doubly linked list problem

Here is one of my functions, i'm practicing c++ without using the oo concept.
i've been trying it for a whole night but i still can't figure it out. The code works fine on example 1. When i call the function InsertintoSortedList, it allow me to insert only once. After that, if i call it again, it just jumped out from the function. The link below is an example. "ID: 1" is the one i first inserted.

http://i.imgur.com/jK71dOB.png <-----Sample output Link

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
void InsertIntoSortedList(){

	newnode = new carlist;
	newnode->next = NULL;
	newnode->back = NULL;
		if (head == NULL) {
			cout << "List is empty, Please insert Data: " << endl;
			cout << "ID: ";
			cin >> newnode->id;
			cout << "Description: ";
			cin >> newnode->description;
			cout << "Price: ";
			cin >> newnode->price;
			cout << "Stock: ";
			cin >> newnode->stock;
			head = tail = newnode;
		}
		else if (newnode->price < head->price) {
			
			InsertAtBeginning();
		}
		else {
			
			temp = head;
			while (temp != NULL) {
				if (newnode->price > temp->price) {
					previous = temp;
				}
				else {
					break;
				}
				temp = temp->next;
			}
			if (temp == NULL) {
				InsertAtEnd();
			}
			else {
				newnode->next = previous->next;
				previous->next->back = newnode;
				previous->next = newnode;
				newnode->back = previous;
			}
		}
		
	
}


It works totally fine in this example.
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
void insertIntoSortedList(int eid) {
	newnode = new employee;
	newnode->employeeID = eid;
	newnode->next = NULL;
	newnode->back = NULL;
	if (head == NULL) {
		head = tail = newnode;
	}
	else if (eid < head->employeeID) {
		
		insertatBeginning(eid);
	}
	else {
		temp = head;
		while (temp != NULL) {
			if (eid > temp->employeeID) {
				previous = temp;
			}
			else {
				break;
			}
			temp = temp->next;
		}
		if (temp == NULL) {
			insertAtEnd(eid);
		}
		else {
			newnode->next = previous->next;
			previous->next->back = newnode;
			previous->next = newnode;
			newnode->back = previous;
		}
	}

}
Last edited on
I don't know if thats the reason the error is thrown, but that statement does not make sense in c++!
head = tail = newnode;
Last edited on
but that statement does not make sense in c++!

See tutorial on operators:
http://www.cplusplus.com/doc/tutorial/operators/

cplusplus_com wrote:
The following expression is also valid in C++:
 
x = y = z = 5;


It assigns 5 to the all three variables: x, y and z; always from right-to-left.
Please post the complete error message, all of them, exactly as they appear in your development environment.

By the way if you're using one of the current C++ standards you should be using nullptr instead of NULL.
Please post the complete error message, all of them, exactly as they appear in your development environment.

By the way if you're using one of the current C++ standards you should be using nullptr instead of NULL.

Thankyou for replying. I've already solved the nullptr problem. However, another new problem pop out again. So i've edited the topic and a link on the sample output. I've been figuring this thing for a whole night. Still no progress on what could possibly cause it.
Last edited on
Topic archived. No new replies allowed.