[HELP] doubly linked list in C++

In this code,
if I input 3 as the item created
the result is
"
[0]->[1]->[2]->Null
[0]->Head
"
but i expect the result should be

"
[0]->[1]->[2]->Null
[2]->[1]->[0]->Head
"

how can i modify the code in the member function
"void Print_Doubly_linked_list_reversely(Doubly_linked_list *ptr)"
in order to have the result
"[2]->[1]->[0]->Head"

thank you for all your help><

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
#include <iostream>
using namespace std;

class Doubly_linked_list
{
   public:
	Doubly_linked_list()
	{  prevPtr=0; 
		nextPtr=0;	}

	int GetNum()
	{  return number;	}

	void SetNum (int num)
	{  number=num;}

	Doubly_linked_list *GetPrev()
	{ return prevPtr;}

	void SetPrev(Doubly_linked_list *ptr)
	{ prevPtr =ptr;}

	Doubly_linked_list *GetNext()
	{ return nextPtr;}

	void SetNext(Doubly_linked_list *ptr)
	{ nextPtr=ptr;}

private:
	int number;
	Doubly_linked_list *prevPtr;
	Doubly_linked_list *nextPtr;
};

 Doubly_linked_list *Create_Doubly_linked_list(int n)
{
	
	Doubly_linked_list *tempPtr,*firstPtr,*lastPtr,*currPtr;
	int i=0;
	do
	{
		tempPtr=new Doubly_linked_list;
		tempPtr->SetNum(i);
		if(i==0)
		{
			firstPtr=tempPtr;
			currPtr=tempPtr;
			lastPtr=tempPtr;
			i++;
		}
		else
		{
			lastPtr->SetNext(tempPtr);
			lastPtr=tempPtr;
			lastPtr->SetPrev(currPtr);
			currPtr=tempPtr;
			i++;
		}
	}
		while(i<n);
		return firstPtr;
}

1
2
3
4
5
6
7
8
9
10
void Print_Doubly_linked_list(Doubly_linked_list *ptr)
{
	while(ptr!=0)
	{
		cout<<"["<<ptr->GetNum()<<"]->";
		ptr=ptr->GetNext();
	}
	cout<<"Null"<<endl;
	cout<<endl;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void Print_Doubly_linked_list_reversely(Doubly_linked_list *ptr)
{
	//what can i do here?


	while(ptr!=0)
	{
		cout<<"["<<ptr->GetNum()<<"]->";
		ptr=ptr->GetPrev();
	}
	cout<<"Head"<<endl;
	cout<<endl;
	
}

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	int num;
	Doubly_linked_list *headPtr;
	Doubly_linked_list *newPtr;
	cout<<"How many items you want to create? ";
	cin>>num;
	headPtr= Create_Doubly_linked_list(num);
	Print_Doubly_linked_list(headPtr);
	Print_Doubly_linked_list_reversely(headPtr);
	return 0;
}

Last edited on
closed account (D80DSL3A)
To print the list in reverse you will need to start at the last pointer (call it tailPtr), not at headPtr.

The tailPtr is the last value taken by tempPtr in your Create_Doubly_linked_list() function.

Maybe you could add a 2nd argument to the function, something like:
Create_Doubly_linked_list(int n, Doubly_linked_list *& tailPtr) then assign tailPtr = tempPtr; when the do loop ends in the function. Note: A reference to a pointer is needed for the 2nd argument (or a pointer to the pointer-take your pick).
Assuming you go with the Doubly_linked_list*& as an argument, in main():
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	int num;
	Doubly_linked_list *headPtr;
	Doubly_linked_list *tailPtr;// you didn't need newPtr anyways
	cout<<"How many items you want to create? ";
	cin>>num;
	headPtr= Create_Doubly_linked_list(num, tailPtr);// value for tailPtr is recorded
	Print_Doubly_linked_list(headPtr);
	Print_Doubly_linked_list_reversely(tailPtr);// start at the tail, not the head.
	return 0;
}
thank you very much
i have solved the problem ^^
Topic archived. No new replies allowed.