【问题标题】:struct data types issues with linked list链表的结构数据类型问题
【发布时间】:2016-07-30 15:32:54
【问题描述】:

您好,我为链表编写了一个库,但我遇到了这个错误和问题。我不知道该怎么做。如果我在第一个 typedef struct listNode 中更改为 list 并在第二个 listNode 中更改为 listItem 然后在linked_lists 中将所有 listNode 替换为 listItem 一切正常,但我需要实现一个列表初始化函数,我认为制作另一个存储列表头和项目数的结构,当我遇到此错误时,我到达了这一点。

linked_lists.h 代码:

#ifndef linked_lists_h
#define linked_lists_h

#include <stdio.h>

struct listNode;

typedef struct listNode{

    int value;
    struct list *next;
}listNode;

struct listaa{

    int count;
    listNode *head;
};


void deleteFirst(listNode *head);
void display(listNode *head);
void addInFront (listNode *head, int value);
void addLast (listNode *head, int value);
void deleteLast(listNode *head);
void add_at_poz(listNode *head, int value, int poz);
void insert_at_poz(listNode *head, int value, int poz);
void delete_at_poz(listNode *head, int poz);
void max(listNode *head);
void min(listNode *head);

#endif /* linked_lists_h */

linked_lists.c 代码:

#include "linked_lists.h"
#include "stdlib.h"

// Single Linked Lists

void display(listNode *head) { // Display a linked list.
    if(head->next != NULL){
        listNode *current;
        current = head;
        while (current->next != NULL) {
            current = current->next;
            printf("%d, ", current->value);
        }
    } else {
        printf("Lista este goala");
    }
    printf("\n");
}

//Adding value functions

// Adding in front of the list
void addInFront(listNode *head, int value) {
    listNode *new_item;
    new_item = (listNode *) malloc(sizeof(listNode));
    new_item->next = head->next;
    new_item->value = value;
    head->next = new_item;

    printf("Valoare %d a fost adaugata.\n", value);
}

// Adding at the end of the list
void addLast(listNode *head, int value) {
    listNode *new_item,*current = head;
    new_item = (listNode *) malloc(sizeof(listNode));
    while(current->next != NULL)
        current = current->next;
    new_item->value = value;
    new_item->next = NULL;
    current->next = new_item;

}


// Adding a new item at specified pozition
void add_at_poz(listNode *head, int value, int poz) {

    poz = poz - 1;
    int iter = 0;
    listNode *current = head, *new_item;

    while(iter < poz) {
        current = current->next;
        iter++;
    }

    new_item = (listNode *)malloc(sizeof(listNode));
    new_item = current->next;
    current->next = new_item;
    new_item->value = value;
}

// Insert a new item at specified pozition
void insert_at_poz(listNode *head, int value, int poz) {

    poz = poz - 1;
    int iter = 0;
    listNode *current = head, *new_item;

    while(iter < poz) {
        current = current->next;
        iter++;
    }

    new_item = (listNode *)malloc(sizeof(listNode));
    new_item->next = current->next;
    current->next = new_item;
    new_item->value = value;
}

// Remove items from list

// Remove first item
void deleteFirst(listNode *head) {
    listNode *deletedItem;
    deletedItem = head->next;
    printf("Elementul %d a fost sters din fata.\n", deletedItem->value);
    head->next = deletedItem->next;
    free(deletedItem);
}

// Delete last item
void deleteLast(listNode *head) {

    listNode *deletedItem, *current;

    current = head;
    while(current->next->next != NULL)
        current = current->next;

    deletedItem = current->next;
    printf("Ultimul elementul %d a fost sters\n",deletedItem->value);

    current->next = NULL;
    free(deletedItem);
}

void delete_at_poz(listNode *head,int poz) {

    int iter = 0;
    listNode *deletedItem, *current = head;
    poz = poz - 1;

    while(iter < poz) {
        current = current->next;
        iter++;
    }

    deletedItem = current->next;
    current->next = deletedItem->next;
    printf("Elementul de pe pozitia %d cu valoare %d a fost sters. \n", poz+1,deletedItem->value);
    free(deletedItem);
}

void max(listNode *head) {
    listNode *current;
    int max = head->next->value;

    current = head;
    while (current->next != NULL) {
        current=current->next;
        if(current->value > max)
            max = current->value;

    }
    printf("Maximul este %d.\n", max);
}

void min(listNode *head) {
    listNode *current;
    int min = head->next->value;

    current = head;
    while (current->next != NULL) {
        current=current->next;
        if(current->value < min)
            min = current->value;

    }
    printf("Minimul este %d.\n", min);
}

【问题讨论】:

  • struct list 在哪里定义?你有一个struct listaa,但没有struct list

标签: c struct linked-list


【解决方案1】:

发布的代码包含几个问题。让我们从头文件开始:

#ifndef linked_lists_h
#define linked_lists_h

#include <stdio.h>

struct listNode;

typedef struct listNode{

    int value;
    struct list *next;
}listNode;

struct listaa{

    int count;
    listNode *head;
};


void deleteFirst(listNode *head);
void display(listNode *head);
void addInFront (listNode *head, int value);
void addLast (listNode *head, int value);
void deleteLast(listNode *head);
void add_at_poz(listNode *head, int value, int poz);
void insert_at_poz(listNode *head, int value, int poz);
void delete_at_poz(listNode *head, int poz);
void max(listNode *head);
void min(listNode *head);

#endif /* linked_lists_h */

1)这两行在头文件中完全不需要,应该删除:

#include <stdio.h>

struct listNode;

2) 这个 typedef 声明不正确:

typedef struct listNode{

    int value;
    struct list *next;    <<- no 'list' struct exists
}listNode;

建议:

struct listNode
{
    int value;
    struct listNode *next;  <<- do NOT use the 'typedef' name here
};

typedef struct listNode  ListNode;

3) 此声明包含一个“不完整”的结构定义:

struct listaa{

    int count;
    listNode *head;  <<- incomplete struct
};

建议使用先前结构定义中的“typedef”名称。

(另一个很好的例子,说明为什么名称的不同不仅仅是大写)

struct listaa{

    int count;
    ListNode *head;   
};

4) 函数原型中的参数列表都包含“不完整”的结构引用:

void deleteFirst(listNode *head);
void display(listNode *head);
void addInFront (listNode *head, int value);
void addLast (listNode *head, int value);
void deleteLast(listNode *head);
void add_at_poz(listNode *head, int value, int poz);
void insert_at_poz(listNode *head, int value, int poz);
void delete_at_poz(listNode *head, int poz);
void max(listNode *head);
void min(listNode *head);

原型需要使用 'struct listNode' 或 typedef 'ListNode'

void deleteFirst  (ListNode *head);
void display      (ListNode *head);
void addInFront   (ListNode *head, int value);
void addLast      (ListNode *head, int value);
void deleteLast   (ListNode *head);
void add_at_poz   (ListNode *head, int value, int poz);
void insert_at_poz(ListNode *head, int value, int poz);
void delete_at_poz(ListNode *head, int poz);
void max          (ListNode *head);
void min          (ListNode *head);

请注意垂直对齐方式提高了可读性。

当然,所有实际的函数签名都需要同样的更正才能使用“typedef”名称而不是“不完整”结构定义

然后,您的头文件不使用“stdio.h”头文件中的任何内容,不应该#include'该文件。而是将#include &lt;stdio.h&gt; 语句放在代码主体中。

注意:在编写“#include”语句时。对于系统头文件,使用格式:

#include <stdio.h>   <<- notice the `<` and `>`

在编写“自制”`#include 语句时使用以下格式:

    #include "linked_lists.h"  <<- notice the double quotes

主要区别在于编译器用于查找实际头文件的搜索顺序。这允许用自制的头文件替换系统头文件。使搜索更快一些,并有助于生成“自我记录”代码。

当上述问题更正后,代码编译干净

【讨论】:

    【解决方案2】:

    我在这里看到的主要问题是struct list *next; 在您的第一个 typedef 中应该是 struct listNode *next。我不知道 C 提供的任何通用关键字 list

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-30
      • 2013-04-23
      • 1970-01-01
      • 2017-07-23
      • 2019-09-07
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多