List Reverse C++

So I'm given ListNode and list to be modified by adding a member unction named reverse that rearranges the nodes in the list so that their order is reversed. I'm having problems implementing this function. The code gives me the list but it doesn't reverse the 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once
#include<iostream>
using namespace :: std;
#include "ListNode.h"
template<class T>
class List
{
private:
	ListNode<T> *head;
	int size;
	ListNode<T> *find(int index) const;
public:
	List();
	List(const List<T> &aList);
	~List();
	int getLength() const;
	void insert (int index, T tempData);
	void Reverse (int index, T tempData);
	void remove(int index);
	void retrieve(int index, T &tempData);
	bool isEmpty() const;
	void print() const;
};//end List class

template<class T>
bool List<T>::isEmpty() const
{
	if(size == 0){
		return true;
	} //end if
	else{
		return false;
	}//end else
} //end function isEmpty
template<class T>
int List<T>::getLength() const
{
	return size;
}//end function getLength
template<class T>
List<T>::List():head(NULL), size(0)
{
}//end default constructor

template<class T>
ListNode<T> *List<T>::find(int index) const
{
	if((index<1)||index>getLength())
		return NULL;
	else{
		ListNode<T> *current=head;
		for(int i =1; i<index; i++){
			current = current->next;
		} //end for
		return current;
	}//end else
}//end function find

template<class T>
void List<T>::insert(int index, T tempData){
	int newLength=getLength()+1;
	if((index<1)||(index>newLength)){
		cout<<"insert index out of rande\n";
	}//end if
	else{
		ListNode<T> *newPtr = new ListNode<T>(tempData);
		if(newPtr == NULL){
			cout<<"insert cannot allocate memory \n";
		}//end if
		else{
			size= newLength;
			if(index==1){
				newPtr->next = head;
				head = newPtr;
			}//end else
			else{
				ListNode<T> *previousPtr = find (index -1);
				newPtr->next = previousPtr->next;
				previousPtr->next = newPtr;


			}//end else
		} //end else
	}//end if
}//end function insert

template<class T>
void List<T>::Reverse(int index, T tempData){
	int newLength=getLength()+1;
	if((index<1)||(index>newLength)){
		cout<<"insert index out of rande\n";
	}//end if
	else{
		ListNode<T> *newPtr = new ListNode<T>(tempData);
		if(newPtr == NULL){
			cout<<"insert cannot allocate memory \n";
		}//end if
		else{
			size= newLength;
			if(index==1){
				newPtr->next = head;
				head = newPtr;
			}//end else
			else{
				ListNode<T> *previousPtr = find (index +1);
				newPtr->next = previousPtr->next;
				previousPtr->next = newPtr;


			}//end else
		} //end else
	}//end if
}//end function reverse
template<class T>
void List<T>::remove(int index)
{
	ListNode<T> *currentPtr;
	if((index<1)||(index>getLength())){
		cout<<"Remove index out of rande \n";
	} //end if
	else {
		size--;
		if (index ==1){
			currentPtr=head;
			head=head->next;
		}//end if
		else{
			ListNode<T> *previousPtr = find(index - 1);
			currentPtr=previousPtr->next;
			previousPtr->next = currentPtr->next;
		}//end else
		currentPtr->next = NULL;
	}//end else
}//end function remove

template<class T>
List<T>::~List()
{
	while(!isEmpty()){
		remove(1);
	}//end while
}//end function remove
template<class T>
void List<T>::retrieve(int index, T &tempData)
{
	if ((index<1)||(index>getLength())){
		cout<<"Retrieve index our of range \n";
	}//end if
	else {
		ListNode<T> *currentPtr = find(index);
		tempData=currentPtr->data;
	}//end else
}//end function retrieve

template<class T>
void List<T>::print() const
{
	if(isEmpty()){
		cout<<"List is Empty\n";
	}//end if
	else{
		ListNode<T> * currentPtr = head;
		while(currentPtr != NULL){
			cout<<currentPtr->data<<",";
			currentPtr = currentPtr->next;
		} //end while
	}//end else
	cout<<"\n\n";
}//end function print

template<class T>
List<T>::List(const List<T> & aList):size(aList.size)
{
	if (aList.head == NULL){
		head = NULL;
	}//end if
	else{
		head = new ListNode<T>(aList.head->data);
		ListNode<T> *origPtr, *currentPtr;
		currentPtr = head;
		origPtr = head;
		origPtr = aList.head->next;
		while(origPtr != NULL){
			currentPtr->next=new ListNode<T>(origPtr->data);
			currentPtr = currentPtr->next;
			origPtr = origPtr->next;
		} //end while
		currentPtr->next;
	}//end else
}//end copy constructor 


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
#pragma once
template <class T> class List;
template <class T>

class ListNode
{
	friend class List <T>;
public:
	ListNode(const T&);
	T getData() const;
private:
	T data;
	ListNode<T> *next;
};//end ListNode class

template <class T>
ListNode<T>::ListNode(const T &info):data(info),next(NULL)
{
}//end default constructor

template <class T>
T ListNode<T>::getData() const
{
	return data;
}//end function getData 


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
#include <iostream>
#include <string>
using namespace std;
#include "list.h"

string str;
int n, s;


int main () 
{
	cout<<"*****************************************"<<endl;
	List <string> L1, L2;

	cout<<"How many character do you want to enter"<<endl;
	cin>>n;
	while (s!= n)
	{
		cout<<" Enter character"<<endl;
		cin>>str; 
		L1.Reverse(1,str);
		L2.insert(1,str);   
		s++; 
	}
	cout<<"<---Original List--->"<<endl;
	L2.print();
	cout<<">>>>List Reverse<<<<"<<endl;
	L1.print();
	
	cout<<"*****************************************"<<endl;

	return (1);

}
Topic archived. No new replies allowed.