Deleting in a linked list

how to delete a node.

Page 1 of 1

3 Replies - 2161 Views - Last Post: 02 September 2009 - 10:53 PM Rate Topic: -----

#1 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 228
  • View blog
  • Posts: 817
  • Joined: 18-April 09

Deleting in a linked list

Posted 02 September 2009 - 09:57 PM

Hello guys, i have been thinking on deleting the front node of the linked list. but there is always an error(the program crashes)

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
	
	
}	 




Is This A Good Question/Topic? 0
  • +

Replies To: Deleting in a linked list

#2 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Deleting in a linked list

Posted 02 September 2009 - 10:24 PM

Does your code display all the information on the list?
Was This Post Helpful? 0
  • +
  • -

#3 tarmizi_adam2005  Icon User is offline

  • جوروترا

Reputation: 228
  • View blog
  • Posts: 817
  • Joined: 18-April 09

Re: Deleting in a linked list

Posted 02 September 2009 - 10:37 PM

View Postponcho4all, on 2 Sep, 2009 - 09:24 PM, said:

Does your code display all the information on the list?


yes sir indeed... but the prob goes when i want to delete the first node...
Was This Post Helpful? 0
  • +
  • -

#4 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Deleting in a linked list

Posted 02 September 2009 - 10:53 PM

Try changing
void Student::deleteFront()
{
	 node *temp;
	 temp =head;
	 head=temp->link;//this line
	 string key = temp->name;
	 delete temp;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1