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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
#include<iostream>
using namespace std;
using std::cout; // used to prevent ambigious error cout
using std::cin;
class Doubly_linked_list // Use a class Doubly_linked_list to represent an object
{
public:
// constructor initialize the nextPtr
Doubly_linked_list()
{
prevPtr = 0; // point to null at the beginning
nextPtr = 0; // point to null at the beginning
}
// get a number
int GetNum()
{
return number;
}
// set a number
void SetNum(int num)
{
number = num;
}
// get the prev pointer
Doubly_linked_list* GetPrev()
{
return prevPtr;
}
// set the prev pointer
void SetPrev(Doubly_linked_list* ptr)
{
prevPtr = ptr;
}
// get the next pointer
Doubly_linked_list* GetNext()
{
return nextPtr;
}
// set the next pointer
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 = 0, *firstPtr = 0, *lastPtr = 0;
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;
tempPtr->SetPrev(lastPtr);
i++;
}
} while (i < n);
return firstPtr;
}
void Print_Doubly_linked_list(Doubly_linked_list* headPtr)
{
while (headPtr != 0)
{
cout << "[" << headPtr->GetNum() << "]->";
headPtr = headPtr->GetNext();
}
cout << "Null" << endl;
cout << endl;
}
void Print_Doubly_linked_list_reversely(Doubly_linked_list* headPtr)
{
int i = 0;
while (headPtr->GetNext() != 0)
{
headPtr = headPtr->GetNext();
i++;
}
while (i >= 0)
{
cout << "[" << headPtr->GetNum() << "]->";
headPtr = headPtr->GetPrev();
i--;
}
cout << "Head" << endl;
cout << endl;
}
Doubly_linked_list* Insert_node_at_front(Doubly_linked_list*ptr, Doubly_linked_list*headPtr)
{
Doubly_linked_list* tempPtr = 0;
tempPtr = headPtr;
headPtr = ptr;
ptr->SetNext(tempPtr);
return ptr;
}
Doubly_linked_list* Remove_node_at_front(Doubly_linked_list*headPtr)
{
Doubly_linked_list* tempPtr = 0;
tempPtr = headPtr;
headPtr = headPtr->GetNext();
delete tempPtr;
return headPtr;
}
Doubly_linked_list *Insert_node_at_back(Doubly_linked_list* headPtr, Doubly_linked_list* newptr)
{
Doubly_linked_list *prevPtr = 0, *currPtr = 0;
currPtr = headPtr;
while (currPtr != 0)
{
prevPtr = currPtr;
currPtr = currPtr->GetNext();
}
prevPtr->SetNext(newptr);
return headPtr;
}
Doubly_linked_list* Remove_node_at_back(Doubly_linked_list* headPtr) //remove a node from the back of a list
{
Doubly_linked_list* prevPtr = 0, * currPtr = 0;
currPtr = headPtr;
while (currPtr->GetNext() != 0)
{
prevPtr = currPtr;
currPtr = currPtr->GetNext();
}
prevPtr->SetNext(0);
delete currPtr;
return headPtr;
}
Doubly_linked_list* Insert_node(int n, Doubly_linked_list* ptr, Doubly_linked_list* headPtr) // Insert a node before the node with a given value in a list
{
Doubly_linked_list* prevPtr = 0, *currPtr = 0;
currPtr = headPtr;
while (currPtr->GetNum() != n)
{
prevPtr = currPtr;
currPtr = currPtr->GetNext();
}
prevPtr -> SetNext(ptr);
ptr -> SetNext(currPtr);
return headPtr;
}
Doubly_linked_list* Remove_node(int n, Doubly_linked_list* headPtr) // Remove a node with a give value in a list
{
Doubly_linked_list* prevPtr = 0, * currPtr = 0;
currPtr = headPtr;
while (currPtr->GetNum() != n)
{
prevPtr = currPtr;
currPtr = currPtr->GetNext();
}
prevPtr->SetNext(currPtr->GetNext());
delete currPtr;
return headPtr;
}
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);
newPtr = new Doubly_linked_list;
newPtr->SetNum(num);
cout << "Insert a node at the front of the list:" << endl;
headPtr = Insert_node_at_front(newPtr, headPtr);
Print_Doubly_linked_list(headPtr);
Print_Doubly_linked_list_reversely(headPtr);
cout << "Remove a node from the front of the list:" << endl;
headPtr = Remove_node_at_front(headPtr);
Print_Doubly_linked_list(headPtr);
Print_Doubly_linked_list_reversely(headPtr);
newPtr = new Doubly_linked_list;
newPtr->SetNum(num);
cout << "Insert a node at the end of the list:" << endl;
headPtr = Insert_node_at_back(newPtr, headPtr);
Print_Doubly_linked_list(headPtr);
Print_Doubly_linked_list_reversely(headPtr);
cout << "Remove a node from the back of the list:" << endl;
headPtr = Remove_node_at_back(headPtr);
Print_Doubly_linked_list(headPtr);
Print_Doubly_linked_list_reversely(headPtr);
cout << "Enter the number of a new node: ";
cin >> num;
newPtr = new Doubly_linked_list;
newPtr->SetNum(num);
cout << "Choose a number to add a node before the node with such value : ";
cin >> num;
cout << "Insert a node before the node with the value " << num << " in the list:" << endl;
headPtr = Insert_node(num, newPtr, headPtr);
Print_Doubly_linked_list(headPtr); Print_Doubly_linked_list_reversely(headPtr);
cout << "Choose a number to delete a node with such value : ";
cin >> num;
cout << "Remove a node with value " << num << " from the list:" << endl;
headPtr = Remove_node(num, headPtr);
Print_Doubly_linked_list(headPtr);
Print_Doubly_linked_list_reversely(headPtr);
return 0;
}
|