fork download
  1. ///////////////////////////////////////////////////////////////////
  2. // Name: Elias Martinez
  3. // Date: 10/20/2025
  4. // Class: CSCI 2380-01
  5. // Semester: Fall 2025
  6. ///////////////////////////////////////////////////////////////////
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. class Node {
  11. public:
  12. string name; // The data stored in the node
  13. Node* next; // Pointer to the next node
  14.  
  15. // Constructor to create a new node
  16. Node(string val) {
  17. name = val;
  18. next = nullptr;
  19. }
  20. };
  21.  
  22. // Add a new node at the end of the list
  23. void insertLast(Node*& head, string value) {
  24. Node* newNode = new Node(value);
  25.  
  26. if (head == nullptr) {
  27. // If the list is empty, new node becomes the head
  28. head = newNode;
  29. } else {
  30. // Otherwise, go to the end and add the new node
  31. Node* temp = head;
  32. while (temp->next != nullptr) {
  33. temp = temp->next;
  34. }
  35. temp->next = newNode;
  36. }
  37. }
  38.  
  39. // Insert a node as the third node in the list
  40. void insertThird(Node*& head, string value) {
  41. Node* newNode = new Node(value);
  42.  
  43. if (head == nullptr) {
  44. // If list is empty, just add the node at the beginning
  45. head = newNode;
  46. return;
  47. }
  48.  
  49. Node* temp = head;
  50. int count = 1;
  51.  
  52. // Move to the second node (if it exists)
  53. while (temp->next != nullptr && count < 2) {
  54. temp = temp->next;
  55. count++;
  56. }
  57.  
  58. // If fewer than 2 nodes, insert at the end
  59. if (count < 2) {
  60. temp->next = newNode;
  61. } else {
  62. // Insert after the second node
  63. newNode->next = temp->next;
  64. temp->next = newNode;
  65. }
  66. }
  67.  
  68. // Delete the first node and return its value
  69. string deleteFront(Node*& head) {
  70. if (head == nullptr) {
  71. return "List is empty";
  72. }
  73.  
  74. Node* temp = head;
  75. string deletedValue = temp->name;
  76. head = head->next; // Move head to next node
  77. delete temp; // Free memory
  78. return deletedValue;
  79. }
  80.  
  81. // Delete the second node and return its value
  82. string deleteSecond(Node*& head) {
  83. if (head == nullptr || head->next == nullptr) {
  84. return "List is too short";
  85. }
  86.  
  87. Node* temp = head->next;
  88. string deletedValue = temp->name;
  89. head->next = temp->next; // Skip over the second node
  90. delete temp;
  91. return deletedValue;
  92. }
  93.  
  94. // Update the value of the last node
  95. void updateLast(Node* head, string value) {
  96. if (head == nullptr) return;
  97.  
  98. Node* temp = head;
  99. while (temp->next != nullptr) {
  100. temp = temp->next;
  101. }
  102. temp->name = value; // Change the last node's name
  103. }
  104.  
  105. // Print all the values in the list
  106. void printList(Node* head) {
  107. Node* temp = head;
  108. while (temp != nullptr) {
  109. cout << temp->name;
  110. if (temp->next != nullptr) cout << " ";
  111. temp = temp->next;
  112. }
  113. cout << endl;
  114. }
  115.  
  116. // Main function to test everything
  117. int main() {
  118. Node* h1 = nullptr;
  119. Node* h2 = nullptr;
  120.  
  121. insertLast(h1, "Daniela");
  122. insertLast(h1, "Daria");
  123. insertLast(h1, "Berta");
  124. insertLast(h1, "Hugo");
  125. insertThird(h1, "Hector");
  126.  
  127. printList(h1); // Output: Daniela Daria Hector Berta Hugo
  128.  
  129. updateLast(h1, "Bernardo");
  130.  
  131. printList(h1); // Output: Daniela Daria Hector Berta Bernardo
  132.  
  133. cout << deleteFront(h1) << endl; // Output: Daniela
  134.  
  135. insertThird(h2, "Daniela");
  136.  
  137. printList(h2); // Output: Daniela
  138.  
  139. cout << deleteSecond(h2) << endl; // Output: List is too short
  140. }
  141.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Daniela Daria Hector Berta Hugo
Daniela Daria Hector Berta Bernardo
Daniela
Daniela
List is too short