Link List Search

Im not too sure what i am doing wrong here. It seems to be a problem in my getIndex; however, I am unsure of what is wrong... It is compiling with out any errors and when it runs and you do a search it closes out. Any Pointers anyone?
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <iostream>
using namespace std;

class floatlist
{
	
	private:
	int element;
	
	class listnode
	{
		friend class floatlist;
		
	   float value;
	  
	   listnode*next; // declare the the nest node 

       listnode (float value1, listnode *next1=NULL)
         {
            value = value1;
            next = next1;
         }
	};
	//
	// DECLARE listnode HEAD POINTER
	// ADD STATEMENT 6 BELOW
    listnode *head;
	//
	// DECLARE PUBLIC VARIABLES
	public:
		// CONSTRUCTOR FOR CLASS floatlist
		floatlist()	
		{ 
			head = NULL; 
		}
		
		void appendnode(float);
		//
	
		void deletenode(float);
		int getIndex();
		
		void displaylist(); 
};

void floatlist::appendnode(float num)
{
	
   if(head==NULL)
      head= new listnode(num);
	//
	// HEAD NOT EMPTY
	else
	{
		// HEAD NOT EMPTY = CREATE A SECOND POINTER
		// ADD STATEMENT 8 BELOW
	listnode *nodeptr; // 
		//
		// ASSIGN HEAD VALUE TO SECOND POINTER
		// ADD STATEMENT 9 BELOW
	nodeptr = head; 	// asstign the head value to the second pointer 
		
	while (nodeptr ->next!=NULL)
          nodeptr=nodeptr ->next; // go through list until a null pointer is found 
		//
		// NULL POINTER FOUND, ADD NEW NODE
		// ADD STATEMENT 11 BELOW
		nodeptr->next = new listnode (num); // Null pointer found, add new node
		
	}
}
//

// DEFINE FUNCTION displaylist
// IT IS A MEMBER OF THE CLASS floatlist
void floatlist::displaylist()
{
	// CREATE A SECOND POINTER
	listnode *nodeptr;
	//
	// // ASSIGN HEAD VALUE TO SECOND POINTER
	nodeptr=head;
	//
	// IF THE LIST IS EMPTY, DO NOTHING.
	// ADD STATEMENTS 13
  if(!head)
  cout<<"NO DATA IN THE LINKED LIST.";
    //
    //
	// OUTPUT DATA AS LONG AS NODE IS NOT NULL
	// ADD STATEMENTS 14
	while (nodeptr)
	{
          cout<<nodeptr->value<<endl;
          nodeptr=nodeptr->next;
          }
}

// DEFINE FUNCTION deletenode
// IT IS A MEMBER OF THE CLASS floatlist
// RECEIVES THE NUMBER TO BE DELETED FROM THE LIST
void floatlist::deletenode(float num)
{
	// DECLARE TWO NODE POINTERS
	// ADD STATEMENT 16 BELOW
       listnode *nodeptr, *previousnodeptr;      //
	//
	// IF THE LIST IS EMPTY, DO NOTHING.
	if(!head)
	{
             cout << "NO DATA IN THE LINKED LIST.\n";
		return;
    }
	//
	// DETERMINE IF THE FIRST NODE IS THE ONE TO DELETE.
	// ADD STATEMENTS 17 BELOW
    if (head-> value == num)              //determines ifthe first node is the one to delete
    {
               nodeptr = head;         //assign head to pointer
               head = head -> next;   //assign head as next node
               delete nodeptr;        //delete the pointer, the node immediately following head is 
                                      //what gets deleted
    }
    
    
	//
	// FIRST NODE IS NOT THE ONE TO BE DELETED
	else
	{
		//INITIALIZE nodeptr TO HEAD OF LIST
		nodeptr = head;
		//
		// SKIP ALL NODES WHOSE VALUE MEMBER IS NOT EQUAL TO num
		// ADD STATEMENTS 18 BELOW
        while (nodeptr != NULL && nodeptr -> value != num)   //while the pointer is not null
                                                            //and it's not the value we are looking for
        {
              previousnodeptr = nodeptr;      // then assign current node to previuos node
              nodeptr = nodeptr -> next;      //and advance to the next node
        }
              
		// FOUND THE NUMBER TO BE DELETED.  LINK THE PREVIOUS NOVE
		// TO THE NODE AFTER nodeptr, THEN DELETE nodeptr
		// ADD STATEMENTS 19 BELOW
  	    if (nodeptr)
  	    {
                    previousnodeptr -> next = nodeptr -> next; //the previous node pointer is the address of the next poniner
                    delete nodeptr;          //delete the node pointer, not the value held in node
         }
	}
}


int floatlist::getIndex()
    {   
  listnode *nodeptr = head;
        int index = 0;
        //O(n) search
        cout<<" Enter number to be found in list: ";
        cin>>element;
        
     
        
    while(nodeptr!= NULL)
        {
                if(nodeptr->value== element)
                {
                        return index;
                }
                nodeptr = nodeptr->next;
                index++;
        
        }
        
   	  //print out a not found or some other error handling
        return -1; //or something equally poignant
}

                         //This is the other file floatlist.h.  Open it to see the other
                         //functions that this program will use.  It will work like 
                         //other #include libraries.  Main will compile this file automatically.
//
// DECLARE USERCOMMENTS FUNCTION
// THE FUNCTION DOES NOT RECEIVE OR RETURN DATA
void usercomments(void);
//
// START MAIN
int main()
{
	//
	// CALL FUNCTION usercomments
	usercomments();
	//
	// DECLARE AN OBJECT OF THE CLASS floatlist NAMED list
	// ADD STATEMENT 2 BELOW
    floatlist list; // Calls the object also giving it it the name of list
                    // this operation allows int main to communicate with the headerfile
                    // files 
	//
	// DECLARE VARIABLES
	int operations;  // WHAT OPERATION TO BE DONE (Start or Stop)
	int choice;  // ADD DATA, DELETE DATA, DISPLAY DATA, EXIT
	float value;
    int curNode; // NUMBER TO BE ADDED TO THE LINKED LIST
   
	//
	do
	{
		cout<< "\nChoose an option below: \n";
    	cout<< "\t1) Add data.\n";
	    cout<< "\t2) Delete data.\n"; 
	    cout<< "\t3) Display data. \n";
	    cout<< "\t4) Search. \n;";
     	cout<< "\t5) Exit. \n\n";
           cout << "Enter your choice ( 1, 2, 3, or 4): ";
	   cin>>choice;
	//
	if (choice >=1 && choice <=4)
	{
	// WHICH CHOICE DID USER SPECIFY?
	         switch(choice)
	         {
	          // ADD DATA
	          case 1: 
			cout << "\nEnter a numerical value (-1 to Stop): " << endl;
			cin>>value;
			while (value != -1)
			{
            //
            // ACCESS THE appendnode FUNCTION INSIDE THE CLASS floatlist
            // ADD STATEMENT 3 BELOW
		     list.appendnode(value);// name list is the name given the headerfile 
                                     // appendnode points to the headerfile 
                                     //list and value belong to main.  appendnode
                                     //is in header file and receive user value.
                                     
                                     
			cout << "Enter a numberical value (-1 to Stop): "<<endl;
			cin>>value;
			}
			break;
	//

		// DISPLAY DATA
	case 2:	cout<< "\nEnter a number in the list to delete: "<<endl;
			cin>>value;
            // ACCESS THE deletenode FUNCTION INSIDE THE CLASS floatlist
            // ADD STATEMENT 15 BELOW
            list.deletenode(value);            
			break;
	//
			
	//
	// DELETE DATA
	case 3:
			cout<<"\nNumbers in the linked list are as follows: \n";
            // ACCESS THE displaylist FUNCTION INSIDE THE CLASS floatlist
            // ADD STATEMENT 12 BELOW
		list.displaylist();             //calls displaylist function in the floatlist.h
			cout<<"\n";
			break;
	// 

	// EXIT
	case 4:
      
   		cout<<"Value was found in the list position ";
        list.getIndex(); 
        break;
	case 5:
          cout<<"\n\n\nProgram terminated successfully!\n\n\n";
		system("pause");
		break;
		
        } // end case
    }
	
	else if (choice != 4)
	    {
              cout << "\nThe valid choices are 1 through 4.\n"; 
              cout << "\nSelect one of the valid choices.\n";
              }
        
   } while (choice != 4); // loop for more data
   //
	
	return 0;
	system("pause");
	// DISPLAY TERMINATION MESSAGE
	cout<<"\n\n\nProgram terminated successfully!\n\n\n";

}	// END MAIN
//
//		
// DEFINE FUNCTION usercomments
void usercomments(void)
{
	cout<<"This program uses a linked list.  The user has the options to"
		<<"\nadd data to the list, delete data from the list, or display"
		<<"\nthe contents of the list.\n\n";
}

1
2
3
4
5
void floatlist::appendnode(float num)
{
	
   if(head==NULL)
      head= new listnode(num);

This code is wrong. Forget about indexes with linked lists. The concept doesn't apply.
Last edited on
in the getindex function you are forgeting to return the index variable;
Also your program exits after a search because your do-while loop terminates when your
choice is 4 and not 5 as you would like!
you are also using 2 variables which you don't use: int curNode and int operations
Last edited on
Topic archived. No new replies allowed.