2014-03-25 140 views
0

我正在尝试将用户添加到链接列表中。我有两个结构和一个方法来添加名为add_friend的设置节点的添加。程序不要求用户输入,而是通过add_friend方法中的参数传递信息:除了将节点(用户)添加到列表中,我必须检查用户是否已经存在。当我尝试比较字符串以查看用户是否存在时,出现错误。任何帮助?不幸的是C是我最弱的编程语言,我很难理解指针在链接列表中添加节点

struct UserAccountNode{ 

struct UserAccount* content; 
char circle; 
struct UserAccountNode* next; 

    }*head = NULL ; 

    struct UserAccount{ 

char username[255]; 
char lastnm [256]; 
char firstnm[256]; 
char password[256]; 
char gender; 
int phone; 
struct Post* post_list; 
struct UserAccountNode* friend_list; 
    } ; 

int add_friend(UserAccount* user, char Circle, UserAccount* Friend) 
{ 
struct UserAccountNode* friend_list; 

friend_list = (struct UserAccountNode*) malloc (sizeof(struct UserAccountNode)); 

while (friend_list != NULL) 
    if (stricmp(Circle, head->friend_list) == 0) 
    { 
     friend_list -> next = head; 
     head = friend_list; 
    } 

    else{ 

     printf("%d, User Already Exists", Friend); 

     } 

return 0; 
} 
+1

如果这是[的解剖**这**](http://stackoverflow.com/questions/22627293/adding-a-new-user-to-list-in-c-program)的后果,你需要放下StackOverflow并拿起你的课本。认真。 *停止编写代码,花一些时间研究和阅读它。*没有捷径。 – WhozCraig

+0

你想如何确定朋友是否已经在列表中?你能否检查一个具有相同地址的用户是否已经存在? – pat

回答

1

代码不会比较字符串。它将char - Circle - 与UserAccountNode* friend_list进行比较。但是stricmp需要两个参数为const char *。您必须对friend_list中的所有项目进行循环,并将每个username与给定的项目进行比较。

另一个问题:您为UserAccountNode分配内存,但不为其内部字段UserAccount* content分配内存。当您尝试读取数据时,它可能会使应用程序崩溃。

1

类型的Circlecharchar*, 类型的head->friend_listUserAccountNode*

所以,你尝试非String对象这里比较作为字符串:

if (stricmp(Circle, head->friend_list) == 0)

我觉得你的程序不能编译。