我提示創建一個生成數字 0-50 的鏈表。當串列生成 49 時,它不會列印 49,而是列印出串列。我的問題是試圖弄清楚如何進行回圈,以便它可以實際執行此 tasl。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void printList();
struct Node {
int data;
struct Node* next;
};
int main() {
srand(time(0)); //srand sets starting value for series of random ints
int x = rand()%50 1; //gets random number in range 1-50, including 50
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
head->data = x; //assigns head to the random value between 0 and 50
head->next = NULL;
printList(head); //calls funtion to print list
return 0;
}
void printList (struct Node* n){
while (n != NULL){
printf(" %d ", n->data);
n = n->next;
}
}
uj5u.com熱心網友回復:
int rnd;
while((rnd = rand() % 50) != 49)
{
add_tolist(rnd);
}
printlist();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/315512.html
