如何用指针在C中构造链表,不断出错

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

我对 C 还很陌生,但仍在掌握很多语法和特性。我不确定需要更改哪些内容才能使我的代码正常工作,但我愿意接受任何想法。我知道我需要利用指针来完成这项工作,但我仍然对具体的实现一无所知。我不断收到一个错误消息,指出我的 myList 函数未声明,但我觉得我已经声明了它。关于 C 的工作原理,我有什么遗漏吗?任何帮助将不胜感激

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


我将简化解释,因此我将使用的术语可能不正确。

正确且一致的缩进始终会使您的代码更易于阅读和修复。还缺少分号,你应该注意这一点。

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

main() 中,您应该在函数末尾添加 return 0;,因为它是一个 undefined behavior(也不是一件好事)。您也可以将其更改为 void main(),但它不会在 clang 中编译。

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

    return 0;
}

您不能将函数放在 C 中的函数内(嵌套函数)。将 addNode()getSize()search() 放在 linkedList() 函数之外。

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() 现在几乎什么都不做,应该被删除。

    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;

main()中,myList是当前空链表的头,所以应该初始化为NULL。没有 linkedList 数据类型,只有 node。将其更改为:

node* myList = NULL;

您似乎将 addNode()getSize()search() 应用于变量,但 C 没有该功能(尽管 C++ 有)。而是将链表头添加为参数。 addNode() 需要 & 地址运算符,因为它将改变头部开始的位置。

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

更新addNode()的函数参数。在 while 条件 current.next = NULL 和 if 语句中,您使用的是赋值运算符而不是比较运算符 !===。您使用了变量 currentnewnode,但从未在函数的任何地方声明它。这里有很多逻辑错误。将其更改为:

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

getSize()search()的函数参数做同样的事情。使用 node * 代替 countNodessearchNode-> 是访问结构指针成员的运算符。 if 语句应该在它进入下一个节点之前放置,因为如果保持原样,它总是会跳过第一个节点。 index 应该放在 if 语句之前,以便索引在跳出循环之前更新。

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

main()之前添加函数原型。

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


int main()
{
    node* myList = NULL;

现在一切正常。