Expand|Select|Wrap|Line Numbers
- #include <iostream.h>
- #include <stdlib.h>
- struct node
- { char name[20];
- int age;
- int id_num;
- node *nxt;
- };
- node *start_ptr = NULL;
- node *current;
- int option = 0;
- void insert_at_end()
- { node *temp, *temp2;
- temp = new node;
- cout << "Please enter the name of the student: ";
- cin >> temp->name;
- cout << "Please enter the age of the student : ";
- cin >> temp->age;
- cout << "Please enter the ID Number of the student : ";
- cin >> temp->id_num;
- temp->nxt = NULL;
- if (start_ptr == NULL)
- { start_ptr = temp;
- current = start_ptr;
- }
- else
- { temp2 = start_ptr;
- while (temp2->nxt != NULL)
- { temp2 = temp2->nxt;
- }
- temp2->nxt = temp;
- }
- }
- void display_list()
- { node *temp;
- temp = start_ptr;
- cout << endl;
- if (temp == NULL)
- cout << "The list is empty!" << endl;
- else
- { while (temp != NULL)
- {
- cout << "Name : " << temp->name << " ";
- cout << "Age : " << temp->age << " ";
- cout << "ID Number : " << temp->id_num;
- if (temp == current)
- cout << " \t\t --> Current First Element";
- cout << endl;
- temp = temp->nxt;
- }
- cout << "\nEnd of list!" << endl;
- }
- }
- void delete_start_node()
- { node *temp;
- temp = start_ptr;
- start_ptr = start_ptr->nxt;
- delete temp;
- }
- void delete_end_node()
- { node *temp1, *temp2;
- if (start_ptr == NULL)
- cout << "The list is empty!" << endl;
- else
- { temp1 = start_ptr;
- if (temp1->nxt == NULL)
- { delete temp1;
- start_ptr = NULL;
- }
- else
- { while (temp1->nxt != NULL)
- { temp2 = temp1;
- temp1 = temp1->nxt;
- }
- delete temp1;
- temp2->nxt = NULL;
- }
- }
- }
- int main() {
- system("CLS");
- start_ptr = NULL;
- do
- {
- display_list();
- cout << endl;
- cout << "1. Add a node to the end of the list." << endl;
- cout << "2. Delete the start node from the list." << endl;
- cout << "3. Delete the end node from the list." << endl;
- cout << "4. Exit application." << endl;
- cout << "\nEnter your choice: ";
- cin >> option;
- switch (option)
- {
- case 1 : insert_at_end(); break;
- case 2 : delete_start_node(); break;
- case 3 : delete_end_node(); break;
- }
- }
- while (option != 4);
- cout << endl;
- return 0;
- }