简体   繁体   中英

How to structure linked list in C with pointers, keep getting error

I'm quite new to C and am still coming to grips with a lot of the syntax and idiosyncrasies. I'm not exactly sure what needs changing in order to make my code work but am open to any ideas. I understand that I need to utilize pointers in order to make this work but I am still lost on the specific implementation. I keep getting an error that my myList function is undeclared but I feel like I have declared it already. Is there something I am missing about how C works? Any help would be appreciated

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


typedef struct node
{
    
int data;
struct node* head;
struct node* next;  
 }node;


node*linkedList ();



int main ()
{
 
 
 struct linkedList* myList = (struct createList*)malloc(sizeof(struct node));
    
    myList.addNode(5);
    myList.addNode(10);
    myList.addNode(13);
    
    
 printf("%d\n", myList.search(10));
 
 printf("The linked list is this big: %d\n", myList.getSize);
 
 
 
    
    
    
}


node* linkedList ()
{   
    node* head;
    node* current;
    node*next;
    
    
    
    
     addNode (int x)
    {
        node keephead = head;
        current = head;
        
        while (current.next = NULL)
        {
        
        if (current.next = NULL)
        {
            
            current.next = node* newnode 
            newnode.data = x;
            newnode.next = NULL;
            newnode.head = keephead
        
        }
        
        if (head = NULL)
        {
            
            head = current;
        }
        
    }
        
    }
   
   int getSize ()
   {
     
     int counter = 0;
       
     node countNodes = head;
        
     while (countNodes.next != NULL)
     {
         countNodes = countNodes.next;
         counter++;

         
     }
       
       return counter;
   }
   
   int search(int value)
   {
       int index = 0;
      node searchNode = head;
      
      while(searchNode.next!= NULL)
      {
            searchNode = searchNode.next;
            index++;
        
         
         
         if (node.value = data)
         {
             break;
            
         }
          
          else {
              
              index = -1;
          }
              
      }
       
       return index;
   }
   
   
   
   
}


I will be simplifying the explanations, so the terms that I will use might not be the correct one.

Proper and consistent indentation would always make your code easier to read and to fix. There were missing semi-colons too, you should watch out for that.

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

typedef struct node
{
    int data;
    struct node* head;
    struct node* next;
} node;

node* linkedList();


int main()
{
    struct linkedList* myList = (struct createList*) malloc(sizeof(struct node));

    myList.addNode(5);
    myList.addNode(10);
    myList.addNode(13);

    printf("%d\n", myList.search(10));
    printf("The linked list is this big: %d\n", myList.getSize);
}


node* linkedList()
{   
    node* head;
    node* current;
    node* next;

    addNode (int x)
    {
        node keephead = head;
        current = head;

        while (current.next = NULL)
        {
            if (current.next = NULL)
            {
                current.next = node* newnode;
                newnode.data = x;
                newnode.next = NULL;
                newnode.head = keephead;
            }

            if (head = NULL)
            {     
                head = current;
            }
        }
    }

    int getSize ()
    {
        int counter = 0;
        node countNodes = head;

        while (countNodes.next != NULL)
        {
            countNodes = countNodes.next;
            counter++;
        }

        return counter;
    }

    int search (int value)
    {
        int index = 0;
        node searchNode = head;

        while (searchNode.next != NULL)
        {
            searchNode = searchNode.next;
            index++;

            if(node.value = data)
            {
                break;
            }

            else
            {
                index = -1;
            }
        }

        return index;
   }
}

In main() , you should add the return 0; at the end of the function as it is an undefined behavior (AKA not a good thing to do). You could also change it to void main() , but it doesn't compile in clang.

    printf("%d\n", myList.search(10));
    printf("The linked list is this big: %d\n", myList.getSize);

    return 0;
}

You can't put a function within a function in C (nested functions). Put addNode() , getSize() , and search() outside the linkedList() function.

node* linkedList()
{   
    node* head;
    node* current;
    node* next;
}

addNode (int x)
{
    node keephead = head;
    ...
}

int getSize ()
{
    int counter = 0;
    ...
    return counter;
}

int search (int value)
{
    int index = 0;
    ...
    return index;
}

linkedList() does literally nothing now and should be removed.

    struct node* next;
} node;


int main()
{
    struct linkedList* myList = (struct createList*) malloc(sizeof(struct node));
    ...
    printf("The linked list is this big: %d\n", myList.getSize);

    return 0;
}

void addNode (int x)
{
    node keephead = head;

In main() , myList is the head of the currently empty linked-list, so it should be initialized to NULL . There's no linkedList data type, only node . Change it to:

node* myList = NULL;

You seem to be applying addNode() , getSize() , and search() to a variable, but C doesn't have that feature (C++ have it though). Add the linked-list head as an argument instead. addNode() needs the & address operator since it will be changing where the head starts.

addNode(&myList, 5);
addNode(&myList, 10);
addNode(&myList, 13);

printf("%d\n", search(myList, 10));
printf("The linked list is this big: %d\n", getSize(myList));

Update the function parameters of addNode() . In the while condition current.next = NULL and if statements, you were using an assignment operator instead of a comparison operator != or == . You used the variables current and newnode , but never declared it anywhere in the function. There were lots of logic errors here. Change it to:

void addNode (node** head, int x)
{
    node* current = *head;
    node* newnode = malloc(sizeof(node));

    newnode->data = x;
    newnode->head = *head;
    newnode->next = NULL;

    if (*head == NULL)
        *head = newnode;
    else
    {
        while (current->next != NULL)
            current = current->next;

        //current is now the last node of linked list
        current->next = newnode;
    }
}

Do the same for the function parameters of getSize() and search() . Use node * instead for countNodes and searchNode . -> is the operator for accessing the members of a struct pointer. if statement should be put before it goes to the next node, as it would always skip the first node if left as it is. index should be put before the if statement so the index would update before it breaks out of the loop.

int getSize (node* head)
{
    int counter = 0;
    node* countNodes = head;

    while (countNodes != NULL)
    {
        countNodes = countNodes->next;
        counter++;
    }

    return counter;
}


int search (node* head, int value)
{
    int index = 0;
    node* searchNode = head;

    while (searchNode != NULL)
    {
        index++;

        if(searchNode->data == value)
            break;

        searchNode = searchNode->next;
    }

    if(searchNode->data != value)
        index = -1;

    return index;
}

Add the function prototypes before main() .

void addNode (node** head, int x);
int getSize (node* head);
int search (node* head, int value);


int main()
{
    node* myList = NULL;

Everything should work properly now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM