[英]C program for adding and deleting nodes from an ascending order in linked list
[英]Randomizing nodes in linked list, C program
以下功能是帶有問題和答案的游戲的一部分。 我對問題的隨機性有一個問題,那就是它們的復雜性-有些問題不止一次出現,而且第一個問題也出現在每個級別,這是不應該發生的。 請幫忙!
typedef struct
{
char question[300];
char date[30], author[30], ansr1[80], ansr2[80], ansr3[80], ansr4[80];
int id, correctAnsr, level;
} game;
typedef struct Node
{
game data;
struct Node* next;
} node;
void printRandom1(node *head)
{
if (isEmpty(head))
return;
srand(time(NULL));
node *result = head->data.question;
node *current = head;
int n;
for (n = 17; current != NULL; n++)
{
if (rand() % n == 0 && current->data.level == 0)
result = current->data.question;
current = current->next;
}
printf("%s\n", result);
int i, ans;
printf("1.-%s\n", result->data.ansr1);
printf("2.-%s\n", result->data.ansr2);
printf("3.-%s\n", result->data.ansr3);
printf("4.-%s\n", result->data.ansr4);
printf("Enter the correct answer: ");
scanf("%d", &ans);
if (ans == result->data.correctAnsr)
printf("CORRECT ANSWER!\n");
else
{
printf("WRONG ANSWER\n");
printf("GAME OVER");
printf("The correct answer is: %d\n", result->data.correctAnsr);
return menu();
exit(1);
}
}
廣告“ 一些問題出現不止一次 ”:這是因為您無論如何都不會跟蹤使用過的問題-您的隨機選擇方法總是從所有問題的列表中進行選擇(無論是否已經提出問題)。
廣告“ 首先也出現在每個級別 ”:我敢打賭,您的隨機選擇方法(很奇怪)不能保證選擇一個問題(即result = current->data.question
部分可能執行得不是很高)可能性)。 在這種情況下, result
的初始值將保留(恰好是第一個問題)。
下面是代碼的修改版本。 一些說明:
計算鏈接列表中的問題數量。 需要以相等的概率從答案中進行選擇的隨機選擇(正確的做法是-可以忽略不計,但在這里可能並不重要)
在新的鏈接列表中跟蹤使用的答案
沒有實現級別邏輯。 您可能想在游戲開始時從鏈接列表中刪除不適當級別的問題。
current
變量是一個指向指針的指針,它簡化了取消鏈接的過程(您無需以這種方式保留先前的入口指針)
碼:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct
{
char question[300];
char date[30], author[30], ansr1[80], ansr2[80], ansr3[80], ansr4[80];
int id, correctAnsr, level;
} question;
typedef struct questionListNode {
question data;
struct questionListNode* next;
} questionListNode;
typedef struct {
questionListNode* headAvailable; // Your former head variable
questionListNode* headUsed; // Initialize this to NULL
int numberOfAvailableNodes; // Number of nodes in headAvailable
int numberOfCorrectAnswers; // Number of nodes in headUsed
} game;
void printRandom1(game* currentGame)
{
if (currentGame->headAvailable == NULL || currentGame->numberOfAvailableNodes <= 0) {
printf("No more questions, you've won!\n");
exit(1);
}
srand(time(NULL)); // Consider moving this to the start of main()
int chosenIndex = rand() % currentGame->numberOfAvailableNodes;
questionListNode** current = &(currentGame->headAvailable);
while ((chosenIndex > 0) && (*current != NULL)) { // the second check is for safety
current = &((*current)->next);
chosenIndex--;
}
questionListNode* currentQuestion = (*current);
if (currentQuestion == NULL) {
printf("Internal error: available count mismatch!\n");
exit(1);
}
printf("%s\n", currentQuestion->data.question);
int i, ans;
printf("1.-%s\n", currentQuestion->data.ansr1);
printf("2.-%s\n", currentQuestion->data.ansr2);
printf("3.-%s\n", currentQuestion->data.ansr3);
printf("4.-%s\n", currentQuestion->data.ansr4);
printf("Enter the correct answer: ");
scanf("%d", &ans);
if (ans != currentQuestion->data.correctAnsr) {
printf("WRONG ANSWER\n");
printf("GAME OVER\n");
printf("The correct answer is: %d\n", currentQuestion->data.correctAnsr);
exit(1);
}
printf("CORRECT ANSWER!\n");
// Remove currentQuestion from the available list
(*current) = currentQuestion->next;
// Put currentQuestion into used list
currentQuestion->next = currentGame->headUsed;
currentGame->headUsed = currentQuestion;
// Update counters
currentGame->numberOfAvailableNodes--;
currentGame->numberOfCorrectAnswers++;
}
int main(int c, char** t)
{
game g;
g.headAvailable = NULL;
g.headUsed = NULL;
g.numberOfAvailableNodes = 0;
g.numberOfCorrectAnswers = 0;
questionListNode q1 = { { "Question 1", "", "", "A1*", "B1", "C1", "D1", 1, 1, 0 }, NULL };
questionListNode q2 = { { "Question 2", "", "", "A2", "B2*", "C2", "D2", 2, 2, 0 }, &q1 };
questionListNode q3 = { { "Question 3", "", "", "A3", "B3*", "C3", "D3", 3, 2, 0 }, &q2 };
g.headAvailable = &q3;
g.numberOfAvailableNodes = 3;
while (1)
printRandom1(&g);
}
其他一些(隨機)注意事項:
我不確定鏈表是否是此任務的最佳數據結構
考慮使用一些前綴來命名您的typedef(例如t_game
, t_node
)
如果您想重新開始游戲(而不是exit()
),則需要將兩個鏈接列表重新連接在一起並重置計數器
祝好運!
免責聲明:我沒有花太多時間檢查代碼,因此請僅以它為例...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.