alxndr
alxndr

Reputation: 3

Searching function for linked list in C

I'm sure this is very simple, but I'm new to linked lists and a bit rusty on pointers. I'm used to coding in C++ where you can easily pass parameters, but in C not so much. So I get confused when things don't work as easily.

I basically just want a function inside my program that receives a passed variable and searches the linked list for it. I have it working in main, but having it as a separate function is giving me headaches.

#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 Thanks for advice with the double pointers! I've made a similar function for the access code, but it doesn't read the linked list nodes and segmentation faults. How can the search function know what the access->next location is? What needs to be passed when calling 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");
        }
    }
}

Upvotes: 0

Views: 5066

Answers (4)

don
don

Reputation: 3

What about `NULL' undeclared here (not in a function) when using the library file stdlib.h it even has #define NULL 0

Upvotes: 0

999k
999k

Reputation: 6565

I think double pointer is the answer for your question for creating functions check this...

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);
}

Upvotes: 0

nav_jan
nav_jan

Reputation: 2553

Instead of

access = access+1;

Use

access = access->next;

EDIT:

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");
             }
     }

}

Calling above function as :

search(head);

from main should work.

Upvotes: 2

Roddy
Roddy

Reputation: 68023

I won't write the function for you, but I will write the specification:-

// 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
};

Usage:

  struct node *result;
  result = findNode(head, 5);
  if (result != NULL)
    ...
  else
    ...

Upvotes: 0

Related Questions