在C中搜索链表的功能

时间:2013-03-18 11:39:25

标签: c pointers search linked-list

我确信这很简单,但我是链接列表的新手,指针上有点生锈。我习惯于在C ++中编码,你可以很容易地传递参数,但在C中并没有那么多。所以当事情变得不那么容易时,我会感到困惑。

我基本上只想在我的程序中使用一个接收传递变量的函数并在链表中搜索它。我把它作为主要工作,但将它作为一个单独的功能让我感到头疼。

#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 个答案:

答案 0 :(得分: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);

来自main应该可以工作。

答案 1 :(得分: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);
}

答案 2 :(得分: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
    ...

答案 3 :(得分:0)

当使用库文件stdlib.h时,这里的'NULL'未声明(不在函数中),它甚至有#define NULL 0