In: Computer Science
DEVELOP IN VISUAL STUDIOS C++ PLEASE
1. Develop a main program that does the following
a) Create six nodes of integers such as n0, n1, n2, n3, n4, n5 (n0 is the head)
b) Assign data to each nodes such as n1->data = 2; n2->data = 5, n3->data = 3, n4->data = 10, n5->data = 1.
c) Make n0 ->next to point to n1, and n0->prev to point to NULL 2.) Print out the content of list you have created 3.) Create another Node called n6 with n6->data = 9. Add this new node after n3 (between node 3 and 4). 4.) Print the content of the list that you created.
d) link nodes to each other (see figure 1) (n5->next must point to NULL)
2. Print out the content of list you have created
3. Create another Node called n6 with n6->data = 9. Add this new node after n3 (between node 3 and
4). After you added n6 your list should look like figure 2
NODE.H
#pragma once #ifndef _NODE_H #define _NODE_H #include<iostream> using namespace std; template<class T> class Node { private: T data; Node<T>* next; Node<T>* prev; public: Node(); Node(T anItem); void setItem(T anItem); void setNext(Node<T>* nextNode); void setPrev(Node<T>* prevNode); T getItem(); Node<T>* getNext(); Node<T>* getPrev(); }; #endif // !_NODE_H
NODE.CPP
#include "Node.h" template<class T> Node<T>::Node() { next = NULL; prev = NULL; } template<class T> Node<T>::Node(T anItem) { data = anItem; next = NULL; prev = NULL; } template<class T> void Node<T>::setItem(T anItem) { data = anItem; } template<class T> void Node<T>::setNext(Node<T>* nextNode) { next = nextNode; } template<class T> void Node<T>::setPrev(Node<T>* prevNode) { prev = prevNode; } template<class T> T Node<T>::getItem() { return data; } template<class T> Node<T>* Node<T>::getNext() { return next; } template<class T> Node<T>* Node<T>::getPrev() { return prev; }
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//main.cpp
#include<iostream>
#include "Node.h"
using namespace std;
int main(){
//creating 6 nodes
Node<int> *n0=new Node<int>();
Node<int> *n1=new Node<int>();
Node<int> *n2=new Node<int>();
Node<int> *n3=new Node<int>();
Node<int> *n4=new Node<int>();
Node<int> *n5=new Node<int>();
//adding data to each node except n0 (n0 is the dummy head node)
n1->setItem(2);
n2->setItem(5);
n3->setItem(3);
n4->setItem(10);
n5->setItem(1);
//linking nodes in the order: n0 -> n1 -> n2 -> n3 -> n4 -> n5 -> NULL
n0->setPrev(NULL);
n0->setNext(n1);
n1->setPrev(n0);
n1->setNext(n2);
n2->setPrev(n1);
n2->setNext(n3);
n3->setPrev(n2);
n3->setNext(n4);
n4->setPrev(n3);
n4->setNext(n5);
n5->setPrev(n4);
n5->setNext(NULL);
//looping and printing the current list
cout<<"list: ";
for(Node<int> *n=n0->getNext();n!=NULL;n=n->getNext()){
cout<<n->getItem()<<" ";
}
cout<<endl;
//creating a new node with data=9
Node<int> *n6=new Node<int>(9);
//adding the new node between n3 and n4
n6->setNext(n4);
n4->setPrev(n6);
n3->setNext(n6);
n6->setPrev(n3);
//printing updated list
cout<<"updated list: ";
for(Node<int> *n=n0->getNext();n!=NULL;n=n->getNext()){
cout<<n->getItem()<<" ";
}
cout<<endl;
//deleting nodes from memory to prevent memory leaks.
delete n0, n1, n2, n3, n4, n5, n6;
return 0;
}
/*OUTPUT*/
list: 2 5 3 10 1
updated list: 2 5 3 9 10 1