Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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)

image text in transcribed

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;inext; } return size; } /////////////////////////////////////////////////////////////////////// // AddToHead /////////////////////////////////////////////////////////////////////// void NodeSLList::AddToHead(const IntNode & node) { // create a new node, and make it the head. the // previous head will become head->next IntNode * next = head; head = new IntNode; head->next = next; head->data = node.data; // if this is the first node, make the tail the // same as the head if (tail == 0) tail = head; } /////////////////////////////////////////////////////////////////////// // DeleteFromHead /////////////////////////////////////////////////////////////////////// IntNode NodeSLList::DeleteFromHead() { IntNode temp; temp.data=0; temp.next=NULL; if (IsEmpty()) { cout data; IntNode *tmp = head; // if there is only one node, set the head and pointer tails // to NULL (0) if (head == tail) head = tail = 0; // otherwise, move the head pointer to the next node // in the list else head = head->next; // delete head node delete tmp; // return value of node that was deleted return temp; } /////////////////////////////////////////////////////////////////////// // AddToTail /////////////////////////////////////////////////////////////////////// void NodeSLList::AddToTail(const IntNode & node) { // create a new node, and make it the tail. the // previous tail will become tail->next IntNode *tmp; tmp->data=node.data; tmp->next=NULL;//or nullptr? tail->next=tmp;

// 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;inext) { *tmp1 = RetrieveNode(i); if(tmp1->data==tmp2->data)

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;inext) { *tmp1 = RetrieveNode(i); if(tmp1->data!=tmp2->data) { c=1; break; } } return(c==1); }

///////////////////////////////////////////////////////////////////////

// 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");

}

return(c =-1); 299 300 301 302 303 304 305 306 307 308 309 310 311 312 // operator!= bool NodeSLList: :operator !=(NodeSLList & list) IntNode *tmp1; "tmpl = RetrieveNode(1); IntNode *tmp2 list.head int c = 0; Exception Thrown Run-Time Check Failure #3-The variable 'tmp1' is being used without for (int i = 1; iGetsize being initialized 313 314 315 316 317 E 318 319 320 321 322 323 324 325 326 327 *tmp1 RetrieveNode(: = Copy Details if (tmp1->data != tmp: Exception Settings Break when this exception type is thrown Except when thrown from: break; ] KingLouis-Homework6.exe Open Exception Settings Edit Conditions return(c 1); // operator+

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 2 Lncs 8056

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013th Edition

3642401724, 978-3642401725

More Books

Students also viewed these Databases questions