Why is this int printing as an memory address?
Why is this printing
The value 0x100103a60 was deleted
Instead of this printing
The value 7 was deleted
output
1 2 3 4 5 6
|
5
7
9
The value 0x100103a60 was deleted
5
9
|
main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
#include "list.h"
int main(int argc, const char * argv[])
{
list brian;
brian.AddNode(5);
brian.AddNode(7);
brian.AddNode(9);
brian.PrintList();
brian.DeleteNode(7);
brian.PrintList();
return 0;
}
|
list.h
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
|
#ifndef __linkedlist__list__
#define __linkedlist__list__
#include <iostream>
class list{
private:
typedef struct node
{
int data;
node* next;
}* nodePtr;
nodePtr head;
nodePtr curr;
nodePtr temp;
public:
list();
void AddNode(int addData);
void DeleteNode(int delData);
void PrintList();
};
#endif /* defined(__linkedlist__list__) */
|
list.cpp
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
|
#include <cstdlib>
#include <iostream>
#include "list.h"
using namespace std;
list::list()
{
head = nullptr;
curr = nullptr;
temp = nullptr;
}
void list::AddNode(int addData){
nodePtr n = new node;
n->next = nullptr;
n->data = addData;
if (head != nullptr)
{
curr = head;
while (curr->next != nullptr)
{
curr = curr->next;
}
curr->next = n;
}
else
{
head = n;
}
}
void list::DeleteNode(int delData){
nodePtr delPtr = nullptr;
temp = head;
curr = head;
while (curr != nullptr && curr->data != delData)
{
temp = curr;
curr = curr->next;
}
if (curr == nullptr)
{
cout << delData << " was not found in the list \n";
delete delPtr;
}
else
{
delPtr = curr;
curr = curr->next;
temp->next = curr;
cout << "The value " << delPtr << " was deleted\n";
delete delPtr;
}
}
void list::PrintList(){
curr = head;
while (curr != nullptr)
{
cout << curr->data << endl;
curr = curr->next;
}
}
|
Last edited on
delPtr is a pointer. delPtr->data is the integer member variable of the object that delPtr points to (unless it is a nullptr).
Topic archived. No new replies allowed.