I'm a coding newbie and I've been lost to what my program is doing. Basically, my add() method abruptly stops when I call it by case 1, 3 times, and then it terminates the program when I want to add more student ID to my node. I'm at a loss and I have been trying to find out what's wrong. Any help would be appreciated!
#include <iostream>
using namespace std;
struct Node
{
int ID;
Node *next;
}; Node *head = NULL;
void Menu();
void Add();
void Delete();
void Search();
void Display();
int main()
{
int Options = 0;
cout << "Welcome to Student Records. Choose an option:\n"
<< "1.) Add student\n"
<< "2.) Delete student\n"
<< "3.) Search student\n"
<< "4.) Display list of students\n"
<< "5.) Exit\n\n";
cout << "Input: ";
cin >> Options;
do{
switch(Options)
{
case 1:
Add();
break;
case 2:
Delete();
break;
case 3:
Search();
break;
case 4:
Display();
break;
default:
cout << "Operation terminated.";
}
cout << "\nWhat's next?\n";
Menu();
cin >> Options;
}while(Options == 1 || Options == 2 || Options == 3 || Options == 4);
cout << "Exit program?";
return 0;
}//end main
void Menu()
{
cout << "1.) Add student\n"
<< "2.) Delete student\n"
<< "3.) Search student\n"
<< "4.) Display list of students\n"
<< "5.) Exit\n\n";
}
void Add()
{
int id;
Node *node_new;
Node *pointer;
Node *lastNode = NULL;
node_new = new Node;
cout << "Enter 3-digit Student ID#: ";
cin >> id;
node_new -> ID = id;
if (head == NULL) //if list is empty
{
head = node_new;
node_new -> next = NULL;
}
else //if head is not NULL
{
pointer = head;
lastNode = NULL;
while(pointer -> ID < id && pointer != NULL)
{
lastNode = pointer;
pointer = pointer -> next;
}
if (lastNode == NULL)
{
head = node_new;
node_new -> next = pointer;
}
else
{
lastNode -> next = node_new;
node_new -> next = pointer;
}
}
}
void Delete()
{
int id;
Node *pointer;
Node *lastNode;
cout << "Enter Student ID# you wish to delete: ";
cin >> id;
if (head == NULL)
{
cout << "The list is already empty.";
}
else if (head -> ID == id)
{
pointer = head;
head = head -> next;
delete pointer;
}
else
{
pointer = head;
while(pointer -> ID != id && pointer != NULL)
{
lastNode = pointer;
pointer = pointer -> next;
}
if(pointer == NULL)
cout << "Student does not exist.";
else
lastNode -> next = pointer -> next;
delete pointer;
}
}
void Display()
{
Node *pointer;
pointer = head;
do{
if(pointer == NULL)
cout << "The list is already empty!";
else
{
cout << "Student ID# " << pointer -> ID << endl;
pointer = pointer -> next;
}
}while(pointer != NULL);
}
void Search()
{
int id;
Node *pointer;
pointer = head;
cout << "Who do you want to search? Enter ID#: ";
cin >> id;
do{
if(pointer == NULL)
cout << "Empty";
else
{
cout << "ID: " << pointer -> ID << endl;
pointer = pointer -> next;
}
}while(pointer != NULL);
}
Related
This question already has an answer here:
How do I properly delete nodes of linked list in C++
(1 answer)
Closed 2 years ago.
I've been asked by a friend to help him with an exercise, basically the idea is like below.
Car number = fr50000 Car owner = AlexNelson Parking time = 3.5 hours.
He was told not to use stuff like string or getline, so it's simple app just to learn the idea of working with linked lists.
So I made this program. When I'm calling the remove() function the first time, it says the list is empty at the beginning (as it should do). But the second and third time, it's saying that the car is removed, but when I call the display() function, the car is still there (it didn't remove from the list)
Can you tell me what's wrong with my code?
#include <iostream>
using namespace std;
int length = 0;//variable of how many items in the list
struct node
{
char carNumber[15];
char carOwner[20];
float parkingTime;
node *link;
};
typedef struct node node;
node *head;//the begining of a list;
bool isempty()
{
if (length == 0)
return true;
else
return false;
}
void insert()
{
if (isempty())
{
head = new node;
cout << "Enter car number: ";
cin >> head->carNumber;
cout << "Enter Car owner: ";
cin >> head->carOwner;
cout << "Enter parking time: ";
cin >> head->parkingTime;
head->link = NULL;
length++;
}
else
{
node *p = head;
node *pnext = new node;
while (true)
{
if (p->link == NULL)
{
p->link = pnext;
break;
}
p = p->link;
}
cout << "Enter car number: ";
cin >> pnext->carNumber;
cout << "Enter Car owner: ";
cin >> pnext->carOwner;
cout << "Enter parking time: ";
cin >> pnext->parkingTime;
pnext->link = NULL;
length++;
}
}
void remove()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
char carnumber[15];
cout << "Enter car number to remove: ";
cin >> carnumber;
node *p;
p = head;
while (p != NULL)
{
if (strcmp(p->carNumber, carnumber) == 0)
{
p = p->link;
cout << "Car removed\n";
return;
}
p = p->link;
}
cout << "Car was not found, check the number\n";
}
void display()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
cout << "Car Number\t\tCar Owner\t\tParking Time\n";
node *p = head;
while (p != NULL)
{
cout << p->carNumber << "\t\t" << p->carOwner << "\t\t" << p->parkingTime << " Hours\n";
p = p->link;
}
}
int main()
{
string number;
display();
insert();
insert();
insert();
display();
remove();
display();
insert();
remove();
display();
}
Your remove() function is not actually removing (or destroying) a node from the list. You need to update the link of the previous node in the list to point to the next node in the list. And you need to update the head too, if removing the 1st node in the list.
Try this instead:
void remove()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
char carnumber[15];
cout << "Enter car number to remove: ";
cin >> carnumber;
node *p = head;
node *prev = NULL;
while (p != NULL)
{
if (strcmp(p->carNumber, carnumber) == 0)
{
if (p == head)
head = p->link;
if (prev)
prev->link = p->link;
delete p;
cout << "Car removed\n";
return;
}
prev = p;
p = p->link;
}
cout << "Car was not found, check the number\n";
}
Alternatively:
void remove()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
char carnumber[15];
cout << "Enter car number to remove: ";
cin >> carnumber;
node **p = &head;
while (*p != NULL)
{
if (strcmp((*p)->carNumber, carnumber) == 0)
{
node *n = *p;
*p = (*p)->link;
delete n;
cout << "Car removed\n";
return;
}
p = &(p->link);
}
cout << "Car was not found, check the number\n";
}
p = p->link; You're modifying the value of the local variable p, not the list. You need to modify the previous node's link field.
In the loop in remove, you are doing p = p->link, when p points to the node you want to remove. But you actually need to update the link field of the node that is pointing to p.
Here's a simple way of doing that:
node **p = &head;
while(*p)
if (strcmp((*p)->carNumber, carnumber) == 0)
break;
else p = &(*p)->link;
if (*p)
{
cout<<"Car removed\n";
delete std::exchange(*p, (*p)->link);
}
else cout << "Car was not found, check the number\n";
If you can't use std::exchange, then you can replace that with:
auto h = *p;
*p = (*p)->link);
delete h;
I'm having hard time to finish the code to get the following steps:
how to read in a file of positive integers (>0)?
how to create a doubly linked list of integers in ascending sorted order?
how to write a method to print the entire list in ascending or descending order (input parameter āAā or āDā tells direction to print)?
how notify the user with a message if the number to add is already in the list or if the number is not in the list if they to delete a number not in the list.
There's no error when i run the program, just trying to solve the 4 steps above.
Here's what I have done so far
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct node
{
int number;
node *next;
};
bool isEmpty(node *head);
char menu();
void insertAsFirstElement(node *&head, node *&last, int number);
void inser(node*&head, node*&last, int number);
void remove(node *&head, node *&last);
void showlist(node *current);
int numIntElement(ifstream&x);
int main() {
string filename;
cout << " Please enter the name of the file = ";
cin >> filename;
ifstream myfile;
myfile.open(filename);
if (!myfile.is_open()) {
cout << endl << " failed to open file, check that it exists and you have access \n" << "\n" << endl;
}
else if (myfile.is_open())
{
ifstream x;
numIntElement(x);
cout << " File exists \n";
////
node * head = NULL;
node *last = NULL;
char choice;
int number;
do {
choice = menu();
switch (choice)
{
case '1':
cout << " Please enter number : ";
cin >> number;
inser(head, last, number);
break;
case '2':
remove(head, last);
case '3':
showlist(head);
break;
default:
break;
}
} while (choice != '4');
{
}
}
system("pause");
return 0;
}
int numIntElement(ifstream&x) {
int n = 0;
int m;
ifstream myfile("file.txt");
int countsingfie = 0;
while (!myfile.eof())
{
myfile >> n;
countsingfie += n;
if (countsingfie == 0) {
cout << "Error : file exist but no data " << endl;
}
cout << "total number " << countsingfie << "\n" << endl;
return countsingfie;
}
}
bool isEmpty(node *head) {
if (head == NULL) {
return true;
}
else
{
return false;
}
}
char menu() {
char choice;
cout << " Main Menu \n";
cout << " 1 . Add a number \n";
cout << " 2 . Delete a number from the list \n";
cout << " 3 . Show the list \n";
cout << " 4. Exit \n";
cin >> choice;
return choice;
}
void insertAsFirstElement(node *&head, node *&last, int number) {
node *temp = new node;
temp->number = number;
temp->next = NULL;
head = temp;
last = temp;
}
void inser(node*&head, node*&last, int number) {
if (isEmpty(head)>0){
insertAsFirstElement(head, last, number);
}
else if (isEmpty(head) < 0)
{
cout << " Please enter a number above 0 " << endl;
}
else
{
node *temp = new node;
temp->number = number;
temp->next = NULL;
last->next = temp;
last = temp;
}
}
void remove(node *&head, node *&last) {
if (isEmpty(head)) {
cout << " Sorry, The list is already empty \n";
}
else if (head == last)
{
delete head;
head = NULL;
last = NULL;
}
else
{
node *temp = head;
head = head->next;
delete temp;
}
}
void showlist(node *current) {
if (isEmpty(current)) {
cout << " The list is empty \n";
}
else
{
cout << " The list contains: \n ";
while (current != NULL)
{
cout << current->number << endl;
current = current->next;
}
}
}
Here is my code i have written. Whats wrong in it. Every time i delete nodes from starting to end its going fine but when deleting randomly its breaking the program.
I have to delete the nodes from the heap/memory permanently not just breaking the links between them.
#include <iostream>
using namespace std;
class Node
{
private:
int data;
Node *next;
public:
Node(){}
Node(int d)
{
data = d;
next = NULL;
}
void SetNext(Node *nextNode)
{
next = nextNode;
}
int Data()
{
return data;
}
Node* Next()
{
return next;
}
};
class List
{
Node *head;
public:
List() { head = NULL; };
void Insert(int d)
{
int value;
value = d;
Node* n1 = new Node(value);
n1->SetNext(NULL);
Node *temp = head;
if(temp != NULL)
{
while(temp->Next() != NULL)
{
temp = temp->Next();
}
temp->SetNext(n1);
}
else
head = n1;
};
void Delete()
{
int value;
cout<<"enter a value to Delete"<<endl;
cin>>value;
Node* temp;
temp = head;
Node *prev;
if(temp == NULL)
{
cout<<"List is empty, deletion is not possible"<<endl;
}
else
{
if(temp->Next() == NULL)
{
if(temp->Data() == value)
{
delete temp;
head = NULL;
}
else
{
cout<<"Entered Data value is not available in the List"<<endl;
}
}
else if((temp->Data() == value))
{
head = temp->Next();
delete temp;
}
else
{
while(temp->Next() != NULL)
{
prev = temp;
temp = temp->Next();
if((temp->Data() == value) && (temp->Next() != NULL))
{
prev->SetNext(temp->Next());
delete temp;
}
else if(temp->Data() == value && temp->Next() == NULL)
{
prev->SetNext(NULL);
delete temp;
}
}
}
}
};
void Print()
{
Node *temp = head;
if ( temp == NULL )
{
cout << "EMPTY" << endl;
return;
}
if ( temp->Next() == NULL )
{
cout << temp->Data();
cout << " --> ";
cout << "NULL" << endl;
}
else
{
do
{
cout << temp->Data();
cout << " --> ";
temp = temp->Next();
}
while ( temp != NULL );
cout << "NULL" << endl;
}
};
void isEmpty()
{
if(head == NULL)
cout<<"List is Empty"<<endl;
else
cout<<"List is not Empty"<<endl;
};
};
int main()
{
List l1;
char ch;
do
{
cout<<"\n Linked List "<<endl;
cout<<"I. Insert \t D. Delete"<<endl;
cout<<"P. Print \t E. isEmpty \t X.EXIT"<<endl;
cout<<"Enter Your Choice :"<<endl;
cin>>ch;
if((ch>=97)&&(ch<=122))
{
ch=ch-32;
}
switch(ch)
{
case 'I': int value;
cout<<"\n***** Inserting *****"<<endl;
cout<<"enter a value to insert"<<endl;
cin>>value;
l1.Insert(value);
break;
case 'D': cout<<"\n***** Delete *****"<<endl;
l1.Print();
cout<<"\nDelete any value from the above listed"<<endl;
l1.Delete();
system("pause");
break;
case 'P': cout<<"\n***** Print *****"<<endl;
l1.Print();
system("pause");
break;
case 'E': cout<<"\n***** isEmpty *****"<<endl;
l1.isEmpty();
system("pause");
break;
case 'X': exit(1);
break;
default: cout<<"\n Invalid Choice"<<endl;
}
system("cls");
}
while(1);
system("pause");
return 0;
}
Your Delete() function is overly complicated. It can be greatly simplified. For that matter, most of your List code can be simplified. Try something more like this instead:
#include <iostream>
using namespace std;
class Node
{
private:
int data;
Node *next;
public:
Node(int d = 0) : data(d), next(NULL) {}
void SetNext(Node *nextNode)
{
next = nextNode;
}
int Data() const
{
return data;
}
Node* Next() const
{
return next;
}
};
class List
{
private:
Node *head;
Node *tail;
public:
List() : head(NULL), tail(NULL) {}
~List() { Clear(); }
void Clear()
{
Node *temp = head;
Node *next;
head = tail = NULL;
while ( temp != NULL )
{
next = temp->Next();
delete temp;
temp = next;
}
}
void Insert(int d)
{
Node* n1 = new Node(d);
if ( head == NULL )
head = n1;
if ( tail != NULL )
tail->SetNext(n1);
tail = n1;
}
bool Delete(int value)
{
Node* temp = head;
Node *prev = NULL;
while ( temp != NULL )
{
Node* next = temp->Next();
if ( temp->Data() == value )
{
if( prev != NULL )
prev->SetNext(next);
if( head == temp )
head = next;
if( tail == temp )
tail = prev;
delete temp;
return true;
}
prev = temp;
temp = next;
}
return false;
}
void Print() const
{
Node *temp = head;
if ( temp == NULL )
{
cout << "EMPTY" << endl;
}
else
{
do
{
cout << temp->Data() << " --> ";
temp = temp->Next();
}
while ( temp != NULL );
cout << " NULL" << endl;
}
}
bool isEmpty() const
{
return (head == NULL);
}
};
int main()
{
List l1;
char ch;
do
{
cout << "\n Linked List " < <endl;
cout << "I. Insert \t D. Delete \t C. Clear" << endl;
cout << "P. Print \t E. isEmpty \t X. EXIT" << endl;
cout << "Enter Your Choice :" << endl;
cin >> ch;
if ( (ch >= 'a') && (ch <= 'z') )
{
ch -= 32;
}
switch (ch)
{
case 'I':
{
int value;
cout << "\n***** Inserting *****" << endl;
cout << "enter a number to insert" << endl;
if ( cin >> value )
l1.Insert(value);
else
{
cout << "\nYou did not enter a valid number" << endl;
system("pause");
}
break;
}
case 'D':
{
cout << "\n***** Delete *****" << endl;
if ( l1.isEmpty() )
{
cout << "List is empty, deletion is not possible" << endl;
break;
}
l1.Print();
cout << "\nDelete any number from the above list" << endl;
int value;
cout << "enter a number to delete" << endl;
if ( cin >> value )
{
if ( l1.Delete(value) )
cout << "Entered number has been deleted from the List" << endl;
else
cout << "Entered number is not available in the List" << endl;
}
else
cout << "\nYou did not enter a valid number" << endl;
system("pause");
break;
}
case 'C':
{
cout << "\n***** Clear *****" << endl;
l1.Clear();
cout << "List is now empty" << endl;
system("pause");
break;
}
case 'P':
{
cout << "\n***** Print *****" << endl;
l1.Print();
system("pause");
break;
}
case 'E':
{
cout << "\n***** isEmpty *****" << endl;
if ( l1.isEmpty() )
cout << "List is Empty" << endl;
else
cout << "List is not Empty" << endl;
system("pause");
break;
}
case 'X':
exit(1);
break;
default:
cout << "\n Invalid Choice" << endl;
system("pause");
break;
}
system("cls");
}
while (1);
system("pause");
return 0;
}
With that said, you really should be using the std::list class instead, or even the std::forward_list class in C++11 and later. Let the STL manage the nodes for you, eg:
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
class List
{
private:
list<int> l;
public:
void Clear()
{
l.clear();
}
void Insert(int d)
{
l.push_back(d);
}
bool Delete(int value)
{
list<int>::iterator iter = find(l.begin(), l.end(), value);
if( iter != l.end() )
{
l.erase(iter);
return true;
}
return false;
}
void Print() const
{
if ( l.empty() )
{
cout << "EMPTY" << endl;
}
else
{
list<int>::iterator iter = l.begin();
do
{
cout << *iter << " --> ";
}
while ( ++iter != l.end() );
cout << " NULL" << endl;
}
}
bool isEmpty() const
{
return l.empty();
}
};
I've been writing a program using a linked list, but for some reason my program is making a false node at the start of the program that is messing with everything else
this file controls the functions in the header file
Node *createNode() {
Node *newNode = new Node;
cout << "Enter in Students last Name" << endl;
cin >> newNode->lastName;
cout << "Enter in Students first Name" << endl;
cin >> newNode->firstName;
cout << "Enter in Students id" << endl;
cin >> newNode->idNumber;
return newNode;
}
Node *insertNode(Node *ptr) {
Node *newNode = createNode();
if( ptr == NULL ) {
ptr = newNode;
} else if ( ptr->idNumber > newNode->idNumber ) {
newNode->next = ptr;
ptr = newNode;
} else {
Node *temp = ptr;
while( temp->next != NULL &&
temp->next->idNumber < newNode->idNumber )
temp = temp->next;
if( temp->next == NULL ) {
temp->next = newNode;
} else {
newNode->next = temp->next;
temp->next = newNode;
}
}
return ptr;
}
Node *searchNode(Node *ptr, int id) {
Node *temp;
Node *found;
bool isfound = false;
temp = ptr;
if(temp == NULL) {
cout << "There are no students in the list" << endl;
isfound = true;
}
while( isfound != true && temp->next != NULL ) {
if( temp->idNumber == id ) {
found = temp;
isfound = true;
cout << found->lastName << ", " << found->firstName
<< ": " << found->idNumber << endl;
} else {
temp = temp->next;
}
}
return found;
}
void printList( Node *ptr ){
Node *temp = ptr;
while( temp != NULL ) {
cout << temp->lastName << ", "
<< temp->firstName<< ": "
<< temp->idNumber << endl;
temp = temp->next;
}
}
and this is the main file where the program is executed
int main() {
Node *list = new Node();
displayList(list);
return 0;
}
void displayList( Node *node ) {
int option = -1;
cout << node->idNumber << endl;
while( option != 5 ) {
cout << endl;
cout << "What do you want to do?" << endl;
cout << "1: Add a student" << endl;
cout << "2: Search for student" << endl;
cout << "3: Delete Student" << endl;
cout << "4: Print Student List" << endl;
cout << "5: Exit" << endl;
cout << "Enter a number for the choice: ";
cin >> option;
if( cin.fail() || option > 5 || option < 0 ) {
cin.clear();
cin.ignore();
option = -1;
}
switch( option ) {
case 1:{
insertNode(node);
}
break;
case 2:{
if(node != NULL){
cout<<"Enter the id you want to find: ";
int sid;
cin >> sid;
searchNode(node, sid);
} else {
cout<<"No Ids in the list"<<endl;
}
}
break;
case 4: printList(node);
break;
case 5: cout<<"GoodBye."<<endl ;
break;
default: cout<<"you have entered a invalid character"<<endl;
}
}
}
this is what is being created before a new node is even created
What do you want to do?
1: Add a student
2: Search for student
3: Delete Student
4: Print Student List
5: Exit
Enter a number for the choice: 4
, : 0
Node *list = new Node(); since you created a new Node in main and passing a valid Node address to displayList( Node *node ); and then to printList( Node *ptr ), its printing the zero initialized Node data.
You can change Node *list = new Node(); to Node *list = NULL in main()
Remove cout << node->idNumber << endl; in displayList( Node *node ); otherwise you will be getting segmentation fault or null dereferencing.
In Node *createNode(), add newNode->next = 0; you must set the next pointer of newly created Node to NULL.
In Node *searchNode(Node *ptr, int id) change while( isfound != true && temp->next != NULL ) into while( isfound != true && temp != NULL ), otherwise it will not search the last node.
Store the return value of insertNode(node) to node in displayList( Node *node ); as node = insertNode(node) otherwise you are loosing the newly added node for both the if and else if cases of insertNode
So I've basically have most of the programming almost complete. I wanted to add a menu that displays an output. For example, a list of records is created. After it is all entered, I would like for it to display only the patients that smoke or have high blood pressure or high fat diet when selecting a number from the menu. Now I am stuck trying to figure that out but can't seem to wrap my head around it. Any suggestions of how I can get it work? Right now it is only set to display all of the records at once.
//Program Description: An interface program that displays patients records.
#include <iostream>
#include <string>
using namespace std;
struct Node
{
char name[20];
int age;
string smoker;
string hbp;
string hfd;
int points;
Node *ptr;
};
Node *startPtr = NULL;
void getInfo()
{
Node *p, *p2;
p = new Node;
int agePoints;
int smkPoints;
int hbpPoints;
int hfdPoints;
cout << " Please enter the name of the patient : ";
cin >> p->name;
cout << " Please enter the age of the patient : ";
cin >> p->age;
cout << " Is he/she a smoker (y/n) : ";
cin >> p->smoker;
cout << " Does he/she have high blood pressure? (y/n) : ";
cin >> p->hbp;
cout << " Does he/she have a high fat diet? (y/n) : ";
cin >> p->hfd;
cout << endl;
p->ptr = NULL;
// Age point system
if (p-> age > 30) {
agePoints = 3;
}
else if (p->age > 20)
{
agePoints = 2;
}
else
{
agePoints = 1;
}
// Habits Points System
if (p->smoker == "Y" || p->smoker == "y")
{
p->smoker = "Yes";
smkPoints = 4;
}
else
{
p->smoker = "non smoker";
smkPoints = 0;
}
if (p->hbp == "Y" || p->hbp == "y")
{
p->hbp = "High blood pressure";
hbpPoints = 2;
}
else
{
p->hbp = "Normal blood pressure";
hbpPoints = 0;
}
if (p->hfd == "Y" || p->hfd == "y")
{
p->hfd = "High fat diet";
hfdPoints = 1;
}
else
{
p->hfd = "Low fat diet";
hfdPoints = 0;
}
p->points = agePoints + smkPoints + hbpPoints + hfdPoints;
// Set up link to this node
if (startPtr == NULL) {
startPtr = p;
}
else
{
p2 = startPtr;
while (p2->ptr != NULL)
p2 = p2->ptr;
p2->ptr = p;
}
}
void delete_start_node()
{
Node *p;
p = startPtr;
startPtr = startPtr->ptr;
delete p;
}
void delete_end_node()
{
Node *p1, *p2;
if (startPtr == NULL)
cout << "The List is empty!\n";
else
{
p1 = startPtr;
if (p1->ptr == NULL)
{
delete p1;
startPtr = NULL;
}
else
{
while (p1->ptr != NULL)
{
p2 = p1;
p1 = p1->ptr;
}
delete p1;
p1->ptr = NULL;
}
}
}
void disp()
{
Node *p;
p = startPtr;
if (p == NULL)
cout << "Empty List!\n";
while (p != NULL)
{
if (p == NULL)
cout << "Empty List!\n";
cout << " Name : " << p->name << endl;
cout << " Age : " << p->age << endl;
cout << " Smoker : " << p->smoker << endl;
cout << " Blood Pressure : " << p->hbp << endl;
cout << " Fat Diet : " << p->hfd << endl;
cout << " Points : " << p->points << endl;
cout << endl;
p = p->ptr;
}
}
Node* removeptr(Node* prev)
{
if (prev)
{
if (prev->ptr)
{
Node* p = prev->ptr;
prev->ptr = p->ptr;
return p;
}
}
else if (startPtr)
{
Node* p = startPtr;
startPtr = startPtr->ptr;
return p;
}
return NULL;
}
// sort by age in ascending order
void sortAge()
{
Node* startPtr2 = NULL;
while (startPtr)
{
Node* prev = NULL;
Node* curr = startPtr;
Node* prevMax = NULL;
int max = startPtr->age;
while (curr)
{
if (curr->age > max)
{
max = curr->age;
prevMax = prev;
}
prev = curr;
curr = curr->ptr;
}
// Node with the highest age found pass throug the list.
Node* xferNode = removeptr(prevMax);
if (xferNode)
{
xferNode->ptr = startPtr2;
startPtr2 = xferNode;
}
}
startPtr = startPtr2;
}
int main()
{
Node *p;
p = startPtr;
char selection;
do
{
cout << " Patient Menu\n";
cout << " =============================================\n";
cout << " 1. Insert a new record\n";
cout << " 2. List smoker patients\n"; // smoker patient
cout << " 3. List HBP patients\n"; // high blood pressure patient
cout << " 4. List HFD patients\n"; // high fat diet patient
cout << " 5. Delete a patient record by given name\n";
cout << " 6. Exit the program\n";
cout << " =============================================\n";
cout << " Enter your selection: ";
cin >> selection;
cout << endl;
switch (selection)
{
case '1':
getInfo();
break;
case '2':
disp();
break;
case '3':
break;
case '4':
break;
case '5':
break;
case '6':
cout << " Goodbye!\n";
return 0;
break;
default:
break;
}
}while (selection != 6);
/*
disp();
cout << "________________________________________________\n";
sortAge();
disp();*/
system("pause");
return 0;
}
Node *smokers, *hbp, *hfd = NULL;
You could keep a separate linked list for each type - smoker, hbp, hfd and change
void disp()
// to
void disp(Node* linkedList)
and print list given to it
case '2':
disp(smokers);
break;
case '3':
disp(hbp);
break;
case '4':
disp(hfd);
break;
btw, smells like a homework :)