I have created the main to test the nine functions and the class for the doubly linked nodes
Class for doubly linked nodes
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
|
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;
};
| |
Int main for testing the nine functions
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
|
int main()
{
int num;
Doubly_linked_list *headPtr;
Doubly_linked_list *newPtr;
cout << "How many items you want to create? ";
cin >> num; // input node number
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; // input of new node number
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;
}
| |
Right now I have to create the nine functions for the Int main to test the doubly linked nodes, here is what the nodes should be in order
1. Create a doubly linked list. The integer assigned in a node of the list is its sequence (i.e., 0 is assigned in the first node, 1 is assigned in the second node, and so on.). Remember to assign the previous and next pointers properly.
2. Print a list.
3. Print a list reversely.
4. Insert a node at the front of a list
5. Remove a node from the front of a list
6. Insert a node at the back of a list
7. Remove a node from the back of a list
8. Insert a node before the node with a given value in a list
9. Remove a node with a give value in a list
The output should look like this provided from the link of the google doc
https://docs.google.com/document/d/183YzOMouAQ42DgeS7J5x0x3zNc9OfQ6mlAfrTcq1HoU/edit?usp=sharing
As for the functions coding, just make it as a beginner's level and dont make it complicated, also add explanations on the comments of the coding so it allows me to understand how does the coding of the nine function works as a beginner