fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. return 0;
  7. }
Success #stdin #stdout 0s 5304KB
stdin
#include <iostream>
#include <string>
using namespace std;


class linkedList
{
private:
	struct node
	{
		 char name[20]; // Name of up to 20 letters
		 int age; // Age in integer
		 float height; // In meters
		 node *next; // Pointer to next node
	};
	node *start_ptr = NULL; // Start Pointer (root)

public:
	int menu();
	void add_node_at_end();
	void displayList();
	void delete_start_node();
	void delete_end_node();

};


int linkedList::menu()
{
	int menuOption = 0;

	cout << "Please select from the menu:" << endl;
	cout << "<< 1 >> Add node at the end of the list" << endl;
	cout << "<< 2 >> Display nodes in the list" << endl;
	cout << "<< 3 >> Delete first node in the list" << endl;
	cout << "<< 4 >> Delete last node in the list" << endl;
	cout << "<< 5 >> Exit" << endl;
	cout << ">> ";
	cin >> menuOption;

	switch (menuOption)
	{
	case 1:
		add_node_at_end();
	break;
	case 2:
		displayList();
	break;
	case 3:
		delete_start_node();
	break;
	case 4:
		delete_end_node();
	break;
	case 5:
		return 0;
	break;
	}
	return 0;
}



void linkedList::add_node_at_end()
{
	node *temp, *temp2; // Temporary pointers

	 // Reserve space for new node and fill it with data
	 temp = new node;
	 cout << "Please enter the name of the person: " << endl;
	 cout << ">> ";
	 cin >> temp->name;
	 cout << "Please enter the age of the person : " << endl;
	 cout << ">> ";
	 cin >> temp->age;
	 cout << "Please enter the height of the person : " << endl;
	 cout << ">> ";
	 cin >> temp->height;
	 temp->next = NULL;

	 // Set up link to this node
	 if (start_ptr == NULL)
	 start_ptr = temp;
	 else
	 {
		 temp2 = start_ptr; // We know this is not NULL - list not empty!
		 while (temp2->next != NULL)
		 temp2 = temp2->next; // Move to next link in chain
		 temp2->next = temp;
	 }

	 menu();
}


void linkedList::displayList()
{
	node *temp;
	temp = start_ptr;

	while(temp)
	{
		cout << "Name : " << temp->name << endl;
		cout << "Age : " << temp->age << endl;
		cout << "Height : " << temp->height << endl;
		cout << endl;
		temp = temp->next;
	}

		menu();

}


void linkedList::delete_start_node()
{
	 node *temp;

	 temp = start_ptr;
	 start_ptr = start_ptr->next;
	 delete temp;

	 menu();
}




void linkedList::delete_end_node()
{
	 node *temp1, *temp2;

	 if (start_ptr == NULL)
	 cout << "The list is empty!" << endl;
	 else
	 {
		 temp1 = start_ptr;
		 if (temp1->next == NULL) // This part is new!
	 {
			 // Here comes your code
			 // Fix me!
			 delete temp1;
			 start_ptr = NULL;
	 }
		 else
		 {
			 while (temp1->next != NULL)
			 {
				 temp2 = temp1;
				 temp1 = temp1->next;
			 }
			 delete temp1;
			 temp2->next = NULL;
		 }
	 }

	 menu();
}


int main()
{
	linkedList listStudent;

	listStudent.menu();

	return 0;
}
stdout
Standard output is empty