比较相邻链表节点中包含的字符串中的各个字符

时间:2018-05-05 20:37:41

标签: string sorting char

我是C的新手。我正在尝试创建一个程序,接受网站名称和用户名,直到用户输入EOF。在输入数据时,它将存储在链接列表中,然后根据网站名称对列表进行按字母顺序排序。我无法找到从两个节点中获取“网站”字符串的方法,并比较每个字符以确定应插入新节点的位置。任何帮助将不胜感激,我已经尝试搜索此网站和其他人的解决方案。我太野心了吗?它正在编译但在两次输入后返回此错误。

test.c:68:24: runtime error: null pointer passed as argument 1, which is 
declared to never be null
/usr/include/string.h:130:14: note: nonnull attribute specified here
Segmentation fault

这恰好是我收到最少错误的点。我意识到字符串t& s不应该是NULL,但我做的任何改变似乎都会使它变得更糟。 我的代码是

#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

typedef struct node
{
char *website;
char *username;
int sitelen;
int userlen;
struct node *next;
}
node;

int main(void)
{
// memory for numbers
node *list = NULL;

// Prompt for numbers (until EOF)
while (true)
{
    // Prompt for number
    char *site = get_string("Website: ");
    // Check for EOF
    if (strcmp(site, "") == 0)
    {
        break;
    }
    char *user = get_string("Username: ");
    // Check whether website is already in list
    bool found = false;
    for (node *ptr = list; ptr != NULL; ptr = ptr->next)
    {
        if (ptr->website == site)
        {
            found = true;
            printf("Website already present");
            break;
        }
    }
    // If number not found in list, add to list
    if (!found)
    {
        // Allocate space for number
        node *nw = malloc(sizeof(node));
        if (!nw)
        {
            return 1;
        }
        // Add details to list
        nw->website = site;
        nw->username = user;
        nw->next = NULL;
        int m = strlen(site);
        if (list)
        {   // if list head of list exists
            node *pre = NULL; // pre pointer
            string t = NULL;
            strcpy(t, nw->website); // t is newly entered website in lowercase string
            for (node *ptr = list; ptr != NULL; pre  = ptr, ptr = ptr->next)
            {   // move along linked list
                string s = NULL;
                strcpy(s, ptr->website); // t is newly entered website in lowercase string
                if (ptr != list && !ptr->next)
                {   // end of list, place new node
                    ptr->next = nw;
                    break;
                }
                else
                {   // compare websites to see if node remains or ptr moves on
                    for (int y = m + 1, i = 0; i < y;)
                    {   // if a website starts the same but is shorter it is inserted
                        if (ptr == list && t[i] < s[i])
                        {
                            nw->next = list;
                            list = nw;
                            break;
                        }
                        else if (t[i] < s[i])
                        {
                            nw->next = ptr->next;
                            pre->next = nw;
                            break;
                        }
                        else if (t[i] == s[i])
                        {
                            i++;
                        }
                        else
                        {
                            break;
                        }
                    }
                    break;
                }
            }

        }
        else
        {   // first itteration assigns list to nw
            list = nw;
        }
    }
}
node *ptr = list;
while (ptr != NULL)
{
    node *next = ptr->next;
    free(ptr);
    ptr = next;
}

}

0 个答案:

没有答案