我遇到了这些挑战:
rand()
函数创建这些结构的1000个实例修改文件code.c中提供的链表代码 所以它适用于插入问题1答案中给出的类型的结构。
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
//#include "users.h"
int i;
//Stime = srand(time(0));
typedef struct users
{
int UserID;
char FullName[50];
int Age;
//double Height;
float Weight;
}users;
typedef struct node
{
//Stime = srand(time(0));
users data;
struct node *next;
} node;
node* insert(node *ptr, users data)
{
node *entry = (node*)malloc(sizeof(node));
//printf("enter the data item\n");
//scanf("%d",*node-> next);
if(entry == NULL)
{
printf("No Free Memory!\n");
exit(0);
}
else
{
entry->data = data;
entry->next = NULL;
if(ptr == NULL)
{
ptr = entry;
}
else
{
node *temp = ptr;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = entry;
}
}
return ptr;
}
/
显然U [i]不是正确的方法。 如果我想拥有独特的构造函数o-n(最大1000)我该怎么做?
int main()
{
int i= 0;
node *first = NULL;
srand(time(0));
users U[i] = {
(U[i].UserID = 600000+rand()% 33331),
(strcpy( U[i].FullName , " Nathanial Rivers")),
(U[i].Age = 18+rand()% 82),
(U[i].Weight = 40+rand()% 99)
};
//users U1 = {600000,"Martin Toomey",19,76.6};
users U2 = {(U2.UserID = 600000+rand()% 33331),"bob boby",21,77.7};
users U3 = {600002,"abcdefg ",17,79.1};
printf(" Name: %s \n",U1.FullName);
printf(" User ID: %d \n Age is: %d \n Weight is: %f \n \n",U1.UserID, U1.Age, U1.Weight );
for (i=0; i<10; i++){
srand(time(0));
first = insert(first, U[i]);
/first = insert(first, U2);
//first = insert(first, U3);
//printf(" User ID: %d \n Age is: %d \n Weight is: %f \n \n",U1.UserID, U1.Age, U1.Weight );}
printf(" User ID: %d \n", U[i].UserID);
printf(" Age %d \n", U[i].Age);
printf(" User ID: %d \n", U[i].UserID);
printf(" Age %d \n", U2.Age);
}
//printf(U1);
first = insert(first, U2);
//printf(*U2);
first = insert(first, U3);
return 0;
}
在main()函数中,我尝试使用rand生成100个唯一用户 我曾经想过,如果我首先有一个打印功能或参数,每次我在循环中首先打电话用C打印用户信息时我不确定是否可能
关于如何改进我的代码的任何指示非常感谢我知道U1 U2 U3 ....不应该被用作var名称它的坏习惯。
答案 0 :(得分:2)
您发布的代码中存在许多问题:
JAR File
:
struct users[] user[i] = new users [100]
;
struct
user* tmp = malloc(sizeof(user))
:
printf(user)
在行尾缺失;
printf(" User ID: %d \n Age is: %d \n Weight is: %f \n",Nathan.UserID, Nathan.Age, Nathan.Weight );
struct users Nathan;
指定类型。
请改用struct
我建议您使用在线资源或书籍,并从更轻松的挑战开始。