can anyone help me on how to delete the front node. also, can anyone help on how to delete given a certain input from the user is being asked. for example if the user wants to delete "6" from the list, the function checks whether there is "6" in the list and deletes it. The code is quite long, but don't worry i've commented on each function...
HERES THE CODE !!!
#include <iostream>
#include<string>
using namespace std;
void enterStudent();
class node // This is one of our node blocks.
{
public:
node(string); // constructor
string name; // the data part of our node will be a string called name
node *link; // this is the link part of our node, link is used to link to other nodes
};
node::node(string x)
{
name = x; // assign the name to a string variable x;
}
class Student // this is the students class
// this is the class that will be use to perform other functions on the "node' Class
{
public:
Student(); // constructor
void addStudent(string x); // add name of student, to the list
void addFront(string x); // add a student from the front.
void deleteFront(); // delete student from the front.
void displayList(); // display the list of students.
node *head; // head pointer, head points to the list that will be created.
};
Student::Student()
{
head = NULL; // head is NULL, when the list is not yet created or the list is empty.
}
void Student::addStudent(string x) // This is the function that adds the student to the list.
{
node *newStudent, *temp; // declaring a newnode named newStudent and temp
if( head == NULL) // if the head is NULL, i.e no list yet, then we create a node.
// this part applies when the first time the user inputs a name.
//because before the user inputs a name, the list is empty.
{
head = new node(x); // creating a new node. Head is created as a new 'node'.
head->name = x; // this command assigns the 'name' part in head to a string x.
head->link = NULL; //the 'link' part of head is assign to NULL, i.e not pointing to any other node.
}
else
{
temp = head;
while(temp->link !=NULL)
{
temp = temp->link;
}
newStudent = new node(x);
newStudent->name = x;
newStudent->link = temp->link;
temp->link = newStudent;
}
}
void Student::addFront(string x) // this is the function that adds the student at the beggining of the list
{
node *newStudent; // declaring new node called newStudent
newStudent = head; // newStudent is assign to be like head
head = new node(x); // head is create as a new node.
head->name = x; // 'name' part of head is assigned the string value x
head->link = newStudent; //'link' part of head is assigned to point to newStudent.
}
void Student::deleteFront()
{
node *temp;
temp =head;
head=head->link;
string key = temp->name;
delete temp;
}
void Student::displayList() // Function to display our list !!!
{
while(head->link != NULL) // while the list does not end, i.e one of the nodes is not NULL
// Note: the last node in the list is always NULL, that is, not pointing to other node
// this loop will keep on looping until one of the 'link' of the nodes is found to be NULL
{
cout << head->name <<","; // print the 'name' to the screen
head = head->link; // increment to the next node
}
}
int main() // Main function: Everyone knows this right ?
{
enterStudent(); // function call
cout << endl;
cout << endl;
system("Pause"); // Dev C++ needs this to pause the program before exiting it.
return 0; // returns 0 when no error in the code.
}
void enterStudent() // this is the function that ask the user's option. it reads the
{
string value; // the string that the user will enter.
Student s; // instantiate an object of class student named s
char cont; // character cont, if the user wants to continue the program
int option; // option is an intiger variable used for the user to choose what function to do.
int no;
do // Starting of the main do loop.
{
do // nested do loop
{
cout <<"1.Add student from the front"<< endl; // |
cout <<"2.Add student from the back"<< endl; // |---------->These are the option the user can choose to do.
cout <<"3.Exit the program"<< endl; // |
cout <<"option:";
cin >> option; // user option will be stored in 'option' as an intiger
cout << endl;
cout << "enter The number of students you want to input:";
cin >> no; // how many students does the user want to input ?
cout << "Enter name:"; // The user is asked to input the students name
cout << endl;
switch(option) // To accomodate with one of the users option from above, Switch is used
{ // Start Switch statement
case 1: // 1st case, if the user chooses to add from the front of the list
{
for (int i=0; i<no; i++) // input is required until the value that the user sets in 'no'
{
cin >> value; // input from user stored in 'value'
s.addFront(value); // each iteration puts the value of 'value' in the list from the front
}
} break;// end of case 1:
case 2: //2nd case, the user chooses to add from the back.
{
for (int i=0; i<no; i++) // input is required until the value that user sets in 'value'
{
cin >> value; // input from the user stored in 'value'
s.addStudent(value); // value is sent to addStudent to be added in the list, adding from the back
} break; // end of case 2:
case 3: break;
}
}
s.addStudent(value);
cout << "The list is:";
s.displayList();
cout << endl;
cout << endl;
cout << "Deleting";
//s.deleteFront();
s.displayList();
cout << endl;
cout << endl;
}while (option !=3); // end of nested do loop
cout << "Do you want to re-enter anoterh list ?:";
cin >>cont;
}while(cont == 'y'); // end of main do loop
}

New Topic/Question
Reply




MultiQuote





|