Linked List function problems

Hi guys. Thanks for yesterday's help.I still have a few problems. The AddMiddleNode function doesnt not work properly even though I worked it out alot of times. And then there's the DeleteNode function. Whenever i prompt it to delete a roll number, it deletes all of them except the one next to it. Theres also the Status struct that I have NO idea what to do with it. But I want to resolve the other errors first.

Here's the header file
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
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string>

using namespace std;

/* The menu options are defined here */
#define ADD		1        
#define DELETE	2     
#define MODIFY	3
#define SEARCH	4
#define DISPLAY	5
#define EXIT      6  // if you add some thing after this make sure to modify the condition 
                     // of the do while loop.


/************************************/
/* The constant fileds */
const int LEN_NAME =10;
const int LEN_EMAIL =20;
const int LEN_TELNO =15;

/*Enumerated types for the status of the students  */

enum StatusT {REGISTERED, ENROLLED, GRADUATED, CLOSED, DROPEDOUT };

// This Corresponds to above enum to convert the values into string

char *StatusStr[] = {"Registered", "Enrolled", "Graduated", "Closed", "Dropedout"};

/* The declaration of the student recrod type node. You can declare
 * as many variable of this type as you like. Each one of them is 
 * going to have the same fields/atributes but of their own.
 */

struct NodeT {    
	int  RollNo;
	char Name[LEN_NAME];
 	char Section;
	char Email[LEN_EMAIL];
	char TelNo[LEN_TELNO];
	int delRoll;
	StatusT Status;
    
    NodeT *Next;
};

/* This is a global variable 
 * Should always points to the first node of list
 */

NodeT *Head = NULL;   

/* Prototypes of the functions used in the C++ program file. 
 * If you add a new function in your program do not forget to 
 * put its prototype here. Any prototype declaration in the program file
 * will heart your grade.
 */

void AddNewNode();  
void AddFirstNode(NodeT*);
void AddLastNode(NodeT*);           
void DeleteNode();
void ModifyNode(); 
void DisplayNode(NodeT*);
void DisplayCompleteList(); 
bool AddMiddleNode(NodeT*);

NodeT *GetNewNode();
NodeT *SearchNode();



and the this is the cpp file. well part of 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
#include "LinkList.h" // This is our own header file

          

/*****************MAIN*****************/
int main(){
	int choice;
	NodeT *NewNode;
	NodeT *Record;
	while(1){
		do {

			cout << "\n\n\n\n\n";  
			cout << "============================"	<< endl;
			cout << "         MAIN  MENU         "	<< endl;
            cout << "============================"  << endl;
			cout << "    "<< ADD    <<": Add a new record"	<< endl; 
			cout << "    "<< DELETE <<": Delete a record"	<< endl;
			cout << "    "<< MODIFY <<": Modify a record"	<< endl;
			cout << "    "<< SEARCH <<": Search a record"	<< endl;
			cout << "    "<< DISPLAY<<": Display"			<< endl;
			cout << "    "<< EXIT   <<": Exit"				<< endl;
			cout << "============================"			<< endl;	
			cout << "       Your choice :";
			cin  >>  choice;
		} while ((choice < ADD) || (choice > EXIT)); 
                                                  
		switch (choice){
		  case ADD    : if(Head == NULL)
							Head = GetNewNode();
						else
							AddNewNode();
		  			    break;                    
		  case DELETE : DeleteNode(); /* Call the Approprecordriate functions */
					    break;
		  case MODIFY : ModifyNode(); /* Call the appropriate functions */
		  			    break;
		  case SEARCH : SearchNode(); /* Search and dispalay the  of a student */
		  				break;
		  case DISPLAY: DisplayCompleteList(); /* shoudl display the complete list */ 
                        break;
		  
          case EXIT   : cout<<"Thankyou for using this program...Good day"<<endl;
						system("pause");
			            return 0;
		} // switch(choice)
	} // while(1)
	system("pause");
} //main()
.
.
.
.
/*****************AddNewNode*****************/
void AddNewNode(){
	NodeT *NewNode;
	NewNode = GetNewNode();
	NodeT *Current;
	Current = Head;
	while(Current->Next != NULL)
		Current= Current->Next;

	//Current= Current->Next;

	if(NewNode->RollNo < Head->RollNo)
		AddFirstNode(NewNode);

	else if( NewNode->RollNo > Current->RollNo )
		AddLastNode(NewNode);

	else
		AddMiddleNode(NewNode);
/* Do the required declarations
 *	
 *
 * Make sure step I and step II have been done
 *
 *
 * Now check the first possibility: i.e. adding in the front of list.
 * else check for second possibility: i.e adding at the end of list.
 * else it is to be added in the middle. 
 * 
 */ 
}
/*****************AddMiddleNode*****************/
bool AddMiddleNode(NodeT *NewNode){ 
	NodeT *Current;
	Current = Head;
	bool check = false;
	while(Current->Next != NULL)
	{
		if(check = false)
		{
			if(Current->Next->RollNo > NewNode->RollNo)
			{
				NewNode->Next = Current->Next;
				Current->Next = NewNode;
				NewNode = NULL;
			}
			Current= Current->Next;
			check = true;
		}

	}

 return true; // This is a dumy statement to resolve the compiler error
}
.
.
.
.
/*****************DeleteNode*****************/
void DeleteNode(){
	NodeT *temp, *temp1, *ptr;
	temp1 = new NodeT;
	
	cout << "Enter Roll# to delete: ";
	cin >> temp1->delRoll;
	while (Head != NULL && Head->RollNo <= temp1->delRoll)
	{
        temp = Head->Next;
        delete[] Head;
        Head = temp;
	}
    if (Head == NULL)
        return;

    ptr = Head;
    while (ptr->Next != NULL)
	{
		if (ptr->Next->RollNo = temp1->delRoll)
		{
            temp = ptr->Next->Next;
            delete[] ptr->Next;
            ptr->Next = temp;
		}
		else
            ptr = ptr->Next;
	}
    return;

/*
 *  Ask the user which node s/he wants to delete
 *  search the required node in the list
 *  if the node is found then remove it form the list
 *    free the memory reserved for the node by using delete operator.
 *
 *  Make sure to handel differnt possibilites that may arrise due to 
 *  deletion of the node. 
 *
 */

}
On line 92, did you mean == instead of =?
Topic archived. No new replies allowed.