1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. // 1. Define a Node to hold each question, its options, the answer, and a pointer to the next question.
  6. struct QuestionNode {
  7. string question;
  8. string options[4];
  9. int answer;
  10. QuestionNode* next;
  11. };
  12.  
  13. // 2. Helper function to easily create and return a new question node.
  14. QuestionNode* createNode(string q, string opt1, string opt2, string opt3, string opt4, int ans) {
  15. QuestionNode* newNode = new QuestionNode();
  16. newNode->question = q;
  17. newNode->options[0] = opt1;
  18. newNode->options[1] = opt2;
  19. newNode->options[2] = opt3;
  20. newNode->options[3] = opt4;
  21. newNode->answer = ans;
  22. newNode->next = nullptr;
  23. return newNode;
  24. }
  25.  
  26. int main()
  27. {
  28. // 3. Build the linked list by creating nodes and linking them together.
  29. QuestionNode* head = createNode("1. What is the extension of C++ file?", ".cp", ".cpp", ".c", ".java", 2);
  30.  
  31. QuestionNode* q2 = createNode("2. Who developed C++ language?", "Dennis Ritchie", "Bjarne Stroustrup", "James Gosling", "Guido van Rossum", 2);
  32. head->next = q2;
  33.  
  34. QuestionNode* q3 = createNode("3. Which symbol is used for comments in C++?", "//", "**", "##", "$$", 1);
  35. q2->next = q3;
  36.  
  37. QuestionNode* q4 = createNode("4. Which function is used to display output?", "cin", "cout", "printf", "input", 2);
  38. q3->next = q4;
  39.  
  40. QuestionNode* q5 = createNode("5. C++ is which type of language?", "Procedural", "Object Oriented", "Functional", "None", 2);
  41. q4->next = q5;
  42.  
  43. int score = 0;
  44. int userAnswer;
  45.  
  46. // 4. Traverse the linked list to ask the questions
  47. QuestionNode* current = head;
  48.  
  49. while (current != nullptr)
  50. {
  51. cout << current->question << endl;
  52.  
  53. for(int j = 0; j < 4; j++)
  54. {
  55. cout << j+1 << ". " << current->options[j] << endl;
  56. }
  57.  
  58. cout << "Enter your answer (1-4): ";
  59. cin >> userAnswer;
  60.  
  61. if(userAnswer == current->answer)
  62. {
  63. score++;
  64. }
  65.  
  66. cout << endl;
  67.  
  68. // Move to the next question in the list
  69. current = current->next;
  70. }
  71.  
  72. cout << "Quiz Completed!" << endl;
  73. cout << "Your Score: " << score << "/5" << endl;
  74.  
  75. // 5. Clean up dynamically allocated memory to prevent memory leaks
  76. current = head;
  77. while (current != nullptr)
  78. {
  79. QuestionNode* temp = current;
  80. current = current->next;
  81. delete temp;
  82. }
  83.  
  84. return 0;
  85. }