doubly simple linked 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
#include <iostream>

using namespace std;

struct node{
	int data;
	node *next;
	node *prev;
}*head,*tail;

void addNodeF( int data ){
	node *newnode = new node;
	newnode->data = data;
	newnode->next = NULL;

	node *temp;
	temp = head;

	if( temp != NULL ){
		while( temp->next != NULL ){
			temp = temp->next;
		}
		temp->next = newnode;
	}
	else{
		head = newnode;
		tail = newnode;
	}
}

void addNodeR( int data ){
	node *newnode = new node;
	newnode->data = data;
	newnode->next = NULL;

	node *temp;
	temp = tail;

	if( temp != NULL ){
		temp->next = newnode;
		newnode->prev = temp;
		temp = newnode;
	}
	else{
		tail = newnode;
		head = newnode;
	}
}

void displayForward(){

	node *temp1;
	temp1 = head;

	int c = 1 ;

	if( temp1 == NULL ){
		cout << "No data inside ! " << endl;
	}
	while( temp1 != NULL ){
		cout << "Node " << c << " : " << temp1->data << endl;
		temp1 = temp1->next;
	}
}

void displayReverse(){
	node *temp1;
	temp1 = tail;

	int c = 1 ;

	if( temp1 == NULL ){
		cout << "No data inside ! " << endl;
	}
	while( temp1 != NULL ){
		cout << "Node " << c << " : " << temp1->data << endl;
		temp1 = temp1->prev;
	}

}



int main(){

	addNodeF ( 45 );
	addNodeF ( 50 );
	addNodeF ( 55 );
	addNodeF ( 60 );
	addNodeF ( 70 );

	displayForward();
	displayReverse();

	system( "pause" );
	return 0;
}


okay. here is simple doubly singly linked list.
what i done so far
but can someone teach me how to displayReverse of my linked list?
i know something wrong or miss few line code at my
 
void addNodeF ( int data )
there right? can someone teach and display?
This function is invalid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void addNodeF( int data ){
	node *newnode = new node;
	newnode->data = data;
	newnode->next = NULL;

	node *temp;
	temp = head;

	if( temp != NULL ){
		while( temp->next != NULL ){
			temp = temp->next;
		}
		temp->next = newnode;
	}
	else{
		head = newnode;
		tail = newnode;
	}
}


A new node has un undefined value in member prev.

Also it is not clear what is the difference in algorithms between functions addNodeF and asdNodeR.
Last edited on
it should be like that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void addNodeF( int data ){
	node *newnode = new node;
	newnode->data = data;
	newnode->next = NULL;

	node *temp;
	temp = head;

	if( temp != NULL ){
		while( temp->next != NULL ){
			temp = temp->next;
		}
		temp->next = newnode;
	}
	else{
		head = newnode;
	}
}


for addNode into the linked list.
but then i don't know how to keep track for previous linked list.
i just now already learn the singly linked list already ..
but i know how to do in in diagram but not relaly good in program..
What is the difference in algorithms between functions addNodeF and asdNodeR? I am asking this question because it seems they do the same.
And why do not you want to define a constructor for Node? For example

1
2
3
4
5
6
struct node{
	node( int data ) : data( data ), next( 0 ), prev( 0 ) {}
	int data;
	node *next;
	node *prev;
}*head,*tail;
Last edited on
i just trying to add node from behind.
but that doesn't matter and just ignore it.

because i don't think constructor is neccessary at here.
can u teach me for the algo reverse based on my code?
How are you going to reverse the list if it is not clear how you add elements in it?
Last edited on
yea that's why.
that's why dad node from reverse just ignore it.
will try later after i know and understand fully..

becaus ei see some reference but i get it fail.
hope to get understand from my on..

mind teach reverse?
Your function displayReverse is correct except that variable c is not being changed.
function is right.
but i cannot show it.
because i didnt not assign my tail to the previous linked list.

this the problem when i run.
error msg come out..

how>
I said you that your functiions of adding elements are incorrect but you said that it is not important.:)
err
u mean assign my linked list to reverse list?
sorry . i can't get what you mean just now.
now only get it ..
here is my full code
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
#include <iostream>

using namespace std;

struct node{
	int data;
	node *next;
	node *prev;
}*head,*tail;

void addNodeF( int data ){
	node *newnode = new node;
	newnode->data = data;
	newnode->next = NULL;

	node *temp;
	temp = head;

	if( temp != NULL ){
		while( temp->next != NULL ){
			temp = temp->next;
		}
		temp->next = newnode;
	}
	else{
		head = newnode;
	}
}


void reverse(){

	if( head == NULL ){
		cout << "No data inside ! " << endl;
	}
	else{
		node *temp = head;
		while( temp != NULL ){
			node *next = temp->next;
			temp->next = temp->prev;
			temp->prev = next;
			temp = next;
		}
		head = tail	;
	}
}

void displayForward(){

	node *temp1;
	temp1 = head;

	int c = 1 ;

	if( temp1 == NULL ){
		cout << "No data inside ! " << endl;
	}
	while( temp1 != NULL ){
		cout << "Node " << c << " : " << temp1->data << endl;
		temp1 = temp1->next;
		c++;
	}
}

void displayReverse(){
	node *temp1;
	temp1 = tail;

	int c = 1 ;

	if( temp1 == NULL ){
		cout << "No data inside ! " << endl;
	}
	while( temp1 != NULL ){
		cout << "Node " << c << " : " << temp1->data << endl;
		temp1 = temp1->prev;
		c++;
	}

}



int main(){

	addNodeF ( 45 );
	addNodeF ( 50 );
	addNodeF ( 55 );
	addNodeF ( 60 );
	addNodeF ( 70 );

	displayForward();
	reverse();
	displayReverse();

	system( "pause" );
	return 0;
}


but when i run display reverse
it show no data inside
As I said above function addNodeF is invalid.
okay.

i got it.
then what should i change?
at that function i didn't assign my tail position.
i should add what in my while loop there?
As i understand function addNodeF has to add a new element at the tail similar member function push_back of standard containers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct node{
	int data;
	node *next;
	node *prev;
	node( int data ) : data( data ), next( 0 ), prev( 0 ) {}

}*head,*tail;

void addNodeF( int data ){
	node *newnode = new node( data );

	if( tail != 0 ){
		newnode->prev = tail;
		tail->next = newnode;
	}
	else{
		head = newnode;
	}
	tail = newnode
}
Last edited on
Topic archived. No new replies allowed.