[HELP] C++ Doubly linked list

i don't know how to create a double linked list.

here are some 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
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;
};


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
Doubly_linked_list *Create_Doubly_linked_list(int n)
{
	Doubly_linked_list *tempPtr,*firstPtr,*lastPtr;
	int i=0;
	do
	{
		tempPtr=new Doubly_linked_list;
		tempPtr->SetNum(i);
		
		if(i==0)
		{
			firstPtr=tempPtr;
			lastPtr=tempPtr;
			i++;
		}
		else
		{
			lastPtr->SetNext(tempPtr);
			lastPtr=tempPtr;
			i++;
		}
	}
		while(i<n);
		return firstPtr;
}
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
void Print_Doubly_linked_list_reversely(Doubly_linked_list *ptr)
{
	
	i don't know how can i write in here

} 

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;
}


the output sample according to the above code is:
[0]->[1]->[2]->Null

but how can i modify the code in order to print out
"[0]->[1]->[2]->Null
[2]->[1]->[0]->head "
by using doubly linked list?


i would be very appreciate if someone can help me ,thanks X 10000
Topic archived. No new replies allowed.