0

我确信这很简单,但我是链表的新手,对指针有点生疏。我习惯于在 C++ 中进行编码,您可以轻松地传递参数,但在 C 中却没有这么多。所以当事情不那么容易工作时,我会感到困惑。

我基本上只想要我的程序中的一个函数,它接收一个传递的变量并在链表中搜索它。我让它在 main 中工作,但是将它作为一个单独的函数让我很头疼。

#include <stdio.h>
#include <stdlib.h>

int globalNum = 1; 

typedef char DATA;
struct node
{
    DATA d;
    int nodeNum;
    struct node *next;
};


main()
{
    struct node *head = NULL;
    struct node *tail = NULL;
    int nodeNum;

/*CREATE*/  
    while(globalNum <= 5)
    {
        struct node *new;
        if((new = malloc(sizeof(struct node))) == NULL) abort();

        new->next = NULL;
        new->nodeNum = globalNum;
        globalNum++;
        if(!head) head = new;
        else tail->next = new;
        tail = new;
    }


/*ACCESS*/
    struct node *access;
    access = head;
    while(access)
    {
        if(access->nodeNum != 5)
        {
            printf("%d\n", access->nodeNum);
            access = access->next;
            printf("NEXT\n");
        }
        else
        {
            printf("FOUND\n");
            return 0;
        }
        if(!access)
        {
            printf("CANNOT ACCESS\n");
            return 0;
        }
    }
}

@555k 感谢双指针的建议!我为访问代码做了一个类似的功能,但它不读取链表节点和分段错误。搜索功能如何知道access->next位置是什么?调用时需要传递什么search(&head);

int access(struct node *head)
{
    struct node *access;
        access = head;
    while(access)
        {
            if(access->nodeNum != 5)
            {
                printf("%d\n", access->nodeNum);
                access = access->next;
                printf("NEXT\n");
        }
        else
        {
            printf("FOUND\n");
        }
    if(!access)
        {
                printf("CANNOT ACCESS\n");
        }
    }
}
4

4 回答 4

2

代替

access = access+1;

采用

access = access->next;

编辑:

void search(struct node *head)
{
    struct node *access;
        access = head;
    while(access)
    {
            if(access->nodeNum != 5)
            {
                printf("%d\n", access->nodeNum);
                access = access->next;
                printf("NEXT\n");
             }
             else
             {
                printf("FOUND\n");
                access = access->next;
             }
             if(!access)
             {
                 printf("CANNOT ACCESS\n");
             }
     }

}

将上述函数调用为:

search(head);

从主要应该工作。

于 2013-03-18T11:43:53.670 回答
0

我认为双指针是您创建函数问题的答案,请检查...

int globalNum = 1; 

typedef char DATA;
struct node
{
    DATA d;
    int nodeNum;
    struct node *next;
};

void Create(struct node **head, struct node **tail)
{
    while(globalNum <= 5)
    {
        struct node *new;
        if((new = malloc(sizeof(struct node))) == NULL) abort();

        new->next = NULL;
        new->nodeNum = globalNum;
        globalNum++;
        printf("\nBEFORE CREATE\nhead = %d\nnew = %d\ntail = %d", *head, new, *tail);
        if(!(*head)) *head = new;
        else (*tail)->next = new;
        *tail = new;
        printf("\nAFTER CREATE\nhead = %d\nnew = %d\ntail = %d\n", *head, new, *tail);
    }
 }
 void Access(struct node **head)
 {    
    struct node *access;
    access = *head;
    while(access)
    {
        if(access->nodeNum != 5)
        {
            printf("%d\n", access->nodeNum);
            access = access->next;
            printf("NEXT\n");
        }
        else
        {
            printf("FOUND\n");
            return 0;
        }
        if(!access)
        {
            printf("CANNOT ACCESS\n");
            return 0;
        }
    }
  }

main()
{
    struct node *head = NULL;
    struct node *tail = NULL;
    int nodeNum;

    Create(&head, &tail);

    Access(&head);
}
于 2013-03-18T12:02:44.583 回答
0

我不会为你写函数,但我会写规范:-

// Looks for node with a specific nodeNum value in the list passed in.
// Returns a pointer to the node if found, or NULL if the node isn't there.

 struct node * findNode(structnode * listHead, int valueToFind)
{
// your code here
};

用法:

  struct node *result;
  result = findNode(head, 5);
  if (result != NULL)
    ...
  else
    ...
于 2013-03-18T12:42:08.520 回答
0

使用库文件 stdlib.h 时此处未声明的“NULL”(不在函数中)怎么样,它甚至有 #define NULL 0

于 2013-07-18T17:20:22.213 回答