Question
How do you initialize *tmp1 (a pointer within bool NodeSLList::operator!=(NodeSLList & list) Here are my files NodeSLList.h #ifndef INT_LINKED_LIST #define INT_LINKED_LIST #include using std::ostream; using
How do you initialize *tmp1 (a pointer within bool NodeSLList::operator!=(NodeSLList & list)
Here are my files
NodeSLList.h
#ifndef INT_LINKED_LIST
#define INT_LINKED_LIST
#include
using std::ostream;
using std::cout;
using std::cin;
using std::endl;
struct IntNode
{
int data;
IntNode * next;
};
// Class NodeSLList1 Declaration
class NodeSLList {
friend ostream & operator
//friend NodeSLList operator+(NodeSLList&, NodeSLList&);
public:
NodeSLList();
NodeSLList(NodeSLList &);
~NodeSLList();
bool IsEmpty();
int GetSize();
void AddToHead(const IntNode & node);
IntNode DeleteFromHead();
void AddToTail(const IntNode & node);
IntNode DeleteFromTail();
IntNode DeleteNode(int nodeNum);
void InsertNode(int nodeNum, const IntNode &node);
void UpdateNode(int nodeNum, const IntNode &node);
void DestroyList();
NodeSLList & operator=(NodeSLList &);
bool operator==(NodeSLList &);
bool operator!=(NodeSLList &);
NodeSLList operator+(NodeSLList &);
private:
IntNode *head, *tail;
IntNode &RetrieveNode(int nodeNum) const; //helper function
/odeNum is
};
#endif INT_LINKED_LIST
//NodeListSL.cpp
#include "NodeSLList.h" /////////////////////////////////////////////////////////////////////// // default constructor /////////////////////////////////////////////////////////////////////// NodeSLList::NodeSLList() { head = tail = 0; } /////////////////////////////////////////////////////////////////////// // copy constructor /////////////////////////////////////////////////////////////////////// NodeSLList::NodeSLList(NodeSLList & list) { IntNode *tmp; for(int i=1;i
// if this is the first node, make the tail the // same as the head if (head == 0) // tail=tmp; } /////////////////////////////////////////////////////////////////////// // DeleteFromTail /////////////////////////////////////////////////////////////////////// IntNode NodeSLList::DeleteFromTail() { IntNode nodeData; // get the current data at the tail nodeData.data = tail->data; // if there is only one node, delete the only node, and set the // head and tail pointers to NULL (0) if (head == tail) { delete head; head = tail =0; } // otherwise, traverse to the tail node and delete it else { IntNode * temp; // traverse to tail pointer for (temp = head; temp->next != tail; temp = temp->next); delete tail; tail = temp; tail->next = 0; } return nodeData; } /////////////////////////////////////////////////////////////////////// // DeleteNode /////////////////////////////////////////////////////////////////////// IntNode NodeSLList::DeleteNode(int nodeNum) { if (nodeNum next; // check for case where nodeNum is > the number of // nodes in the list. if we reach the tail before // we traverse to the node, delete the tail if ( temp == tail ) return DeleteFromTail(); } // if deleting the head just call // the appropriate member function // and don't repeat that logic here if (temp == head) return DeleteFromHead(); // otherwise, delete the node we traversed to prev->next = temp->next; current.data = temp->data; delete temp; return current; } /////////////////////////////////////////////////////////////////////// // InsertNode /////////////////////////////////////////////////////////////////////// void NodeSLList::InsertNode(int nodeNum, const IntNode &node) { IntNode *prevNode = head; for (int i = 1; i next;
IntNode * insert; insert = new IntNode(node); prevNode->next = insert;
insert->next = prevNode->next->next; } /////////////////////////////////////////////////////////////////////// // UpdateNode /////////////////////////////////////////////////////////////////////// void NodeSLList::UpdateNode(int nodeNum, const IntNode &node) { IntNode *tmp = head; // traverse to the node, or to the last node, whichever comes // first. if the last node is reached, then that is the node // that is updated for (int i=1; inext; tmp->data = node.data; } /////////////////////////////////////////////////////////////////////// // DestroyList /////////////////////////////////////////////////////////////////////// void NodeSLList::DestroyList() { // while the list is NOT empy // keep removing the head node and make // the next node the head node for (IntNode *p; !IsEmpty(); ) { p = head->next; delete head; head = p; } head = tail = 0; } /////////////////////////////////////////////////////////////////////// // operator= /////////////////////////////////////////////////////////////////////// NodeSLList & NodeSLList::operator=(NodeSLList & list) { IntNode *origPtr, *LastPtr; origPtr=list.head; LastPtr= new IntNode(); head=LastPtr;
while(LastPtr != NULL){ LastPtr = new IntNode(); LastPtr = LastPtr->next; } return *this; } /////////////////////////////////////////////////////////////////////// // operator== /////////////////////////////////////////////////////////////////////// bool NodeSLList::operator==(NodeSLList & list) { IntNode *tmp1; *tmp1=RetrieveNode(1); IntNode *tmp2=list.head; int c=0; for(int i=2;i
c=1;
else { c=0; break; } } return(c==1); } /////////////////////////////////////////////////////////////////////// // operator!= /////////////////////////////////////////////////////////////////////// bool NodeSLList::operator!=(NodeSLList & list) { IntNode *tmp1; IntNode *tmp2=list.head; int c=0; for(int i=1;i
///////////////////////////////////////////////////////////////////////
// operator+
///////////////////////////////////////////////////////////////////////
NodeSLList NodeSLList::operator+(NodeSLList & list) { NodeSLList output(*this); IntNode *tmp; for(int i=1;i /////////////////////////////////////////////////////////////////////// // RetrieveNode // // Description: retrieve data from a node without removing it from // the list // Input: node number (1-N; not 0-N-1) // Output: none // Returns: reference to node data /////////////////////////////////////////////////////////////////////// IntNode & NodeSLList::RetrieveNode(int nodeNum) const { IntNode *tmp = head; // traverse to the node, or to the last node, whichever comes // first for (int i=1; inext; return *tmp; } ostream & operatordata; tmp=tmp->next;//movethe pointer till the specified nodeNum } return output; } // end of NodeSLList SimpleTestDriver.cpp #include #include #include #include "NodeSLList.h" void NodeSLList_Test(); //void TestSort(); void main(void) { NodeSLList_Test(); } void NodeSLList_Test() { NodeSLList list1, list2; cout cout cout cout cout cout IntNode temp; IntNode n1; n1.data = 10; IntNode n2; n2.data = 20; IntNode n3; n3.data = 30; IntNode n4; n4.data = 40; IntNode n5; n5.data = 50; cout cout cout cout list1.AddToHead(n5); cout list1.AddToHead(n4); cout list1.AddToHead(n3); cout list1.AddToHead(n2); cout list1.AddToHead(n1); cout cout cout cout cout cout cout cout cout cout cout cout cout cout cout cout cout cout temp = list1.DeleteFromHead(); cout cout cout cout cout cout if (list1 == list2) cout if (list1 != list2) cout cout list1.AddToHead(temp); cout cout cout cout cout cout cout cout temp = list1.DeleteFromTail(); cout cout cout cout cout cout list1.AddToHead(n3); list1.AddToHead(n2); cout cout cout cout cout cout temp = list1.DeleteNode(35); cout cout temp = list1.DeleteNode(3); cout cout cout cout cout cout cout cout cout cout cout cout cout cout cout temp.data = 500; list1.UpdateNode(3, temp); cout cout cout cout cout cout int numNodesToDelete = list1.GetSize(); for (int x = 0; x { cout temp = list1.DeleteFromHead(); cout cout } cout temp = list1.DeleteFromHead(); cout cout cout cout cout cout cout cout list2.DestroyList(); cout cout cout cout cout cout cout cout cout cout cout cout cout cout cout system("pause"); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started