pointers reseting
Feb 1, 2012 at 6:56pm UTC
I am learning how to use linked lists. When I move from one member function to another the pointers reset to NULL. I thought that having the pointers be global variables should mean that the pointers keep the values they are assigned?
If you have any tips for me, including general tips for linked lists, I would really appreciate it.
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
// SingleLinkedList.h
#include <iostream>
#include <string>
using namespace std;
class Node
{
private :
//Pieces of the Node
string data;
//string dataSearchable;
Node* slink;
public :
//Special Constructor Thing
Node ();
//Member Functions
bool searchList ();
void fillNode ();
void insertNode ();
void deleteNode ();
bool clearList ();
void writeList ();
//Special Function
string inputData (string);
//Pointers
Node* headptr;
Node* ptr;
//Global Variable
string dataField;
};
Node::Node()
{
headptr=NULL;
}
bool Node::searchList ()
{
Node* ptr1;
ptr1 = headptr;
cout << "\nPlease input the data string\n" ;
cin.ignore(1000, '\n' );
getline (cin, dataField);
while (ptr1->data != dataField && ptr1 != NULL)
ptr1 = ptr1 -> slink;
if (ptr1 -> data == dataField)
{
cout << endl << ptr1 -> data << endl;
return true ;
}
else
{
if (ptr1 = NULL)
{
cout << endl << dataField << "is not in the list\n" ;
return false ;
}
else
{
cout << endl << "Programmer Error\n" ;
return false ;
}
}
}
void Node::fillNode ()
{
ptr = new Node;
cout << "\nPlease input the data string\n" ;
cin.ignore(1000, '\n' );
getline (cin, dataField);
ptr -> data = dataField;
ptr -> slink = NULL;
}
void Node::insertNode ()
{
Node* ptr1;
Node* ptr2;
ptr1 = headptr;
if (ptr1 == NULL)
{
headptr = ptr;
ptr -> slink = NULL;
}
else
{
while ((ptr1 -> data <= ptr -> data) && (ptr1 != NULL))
{
ptr2 = ptr1;
ptr1 = ptr1 -> slink;
}
if (ptr1 -> data > ptr -> data)
{
ptr2 -> slink = ptr;
ptr -> slink = ptr1;
}
else
{
if (ptr1 == NULL)
{
ptr2 -> slink = ptr;
ptr -> slink = NULL;
}
else
cout << "\nProgramming Error\n" ;
}
}
}
void Node::deleteNode ()
{
Node* ptr1;
Node* ptr2;
ptr1 = headptr;
cout << "\nPlease input the data string\n" ;
cin.ignore(1000, '\n' );
getline (cin, dataField);
while (ptr1->data != dataField && ptr1 != NULL)
{
ptr2 = ptr1;
ptr1 = ptr1 -> slink;
}
if (ptr1 -> data == dataField)
{
ptr2 -> slink = ptr1 -> slinky;
delete ptr1;
}
else
{
if (ptr1 == NULL)
{
cout << "\nThe data entered is not in the list\n" ;
}
else
{
cout << "\nProgrammer Error\n" ;
}
}
}
bool Node::clearList()
{
Node* ptr1;
if (headptr == NULL)
{
delete headptr;
return true ;
}
else
{
do
{
ptr1 = headptr;
headptr = headptr -> slink;
delete ptr1;
}
while (headptr != NULL);
return true ;
}
}
void Node::writeList ()
{
Node* ptr1;
if (headptr == NULL)
cout << "\nList is Empty\n" ;
else
{
ptr1 = headptr;
while (ptr1 != NULL)
{
cout << endl << ptr1 -> data << endl;
ptr1 = ptr1 -> slink;
}
}
}
and the test harness for it
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
#include <iostream>
#include "SingleLinkedList.h"
using namespace std;
void switcher (Node);
int b;
int main ()
{
Node a;
b=2;
while (b>1)
switcher (a);
cout << "\ngoodbye\n" ;
cin.ignore(2);
return 0;
}
void switcher (Node a)
{
int switchingNumber;
cout << "\nPlease Select the Number of the Option You Want.\n" ;
cout << "1) Create a New Node.\n" <<
"2) Print the List.\n" <<
"3) Search the List.\n" <<
"4) Delete a Node.\n" <<
"5) Clear the List.\n" <<
"6) Exit Program.\n" ;
cin >> switchingNumber;
switch (switchingNumber)
{
case 1: a.fillNode ();
a.insertNode ();
b = 2;
break ;
case 2: a.writeList ();
b = 2;
break ;
case 3: a.searchList ();
b = 2;
break ;
case 4: a.deleteNode ();
b = 2;
break ;
case 5: a.clearList ();
b = 2;
break ;
case 6: //cout << "switch working";
b = 0;
break ;
}
}
Thanks
Last edited on Feb 1, 2012 at 7:00pm UTC
Feb 1, 2012 at 7:03pm UTC
On line 56 you use =
instead of ==
.
Feb 1, 2012 at 9:52pm UTC
I first noticed the problem when I ran this section
1 2 3 4
case 1: a.fillNode ();
a.insertNode ();
b = 2;
break ;
though I the problem seems to extend to the rest of the .h library
Topic archived. No new replies allowed.