带有链表

时间:2016-04-09 10:06:46

标签: c struct linked-list

您好,我为链接列表编写了一个库,但是我遇到了这个错误和问题。我不知道该怎么做。如果我用list更改第一个typedef struct listNode,使用listItem更改第二个listNode然后在linked_lists中替换所有listNode和listItem一切正常但我需要实现一个列表初始化函数而我我想创建另一个存储列表头和项目数的结构,当我遇到这个错误时,我就到了这一点。linked_list.c linked_lists.c linked_lists.h

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

2 个答案:

答案 0 :(得分:1)

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

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

建议使用&#39; typedef&#39;来自先前结构定义的名称。

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

struct listaa{

    int count;
    ListNode *head;   
};

4)函数原型中的参数列表都包含&#39;不完整的&#39;结构参考:

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; struct listNode&#39;或者输入typedef&#39; ListNode&#39;

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

注意垂直对齐获得的可读性的提高。

当然,所有实际的功能签名都需要同样的修正才能使用&#39; typedef&#39;名字而不是“不完整”的名字。结构定义

然后,你的头文件,它没有使用&#39; stdio.h&#39;头文件不应该是#include&#39该文件。而是将#include <stdio.h>语句放在代码的主体中。

注意:写一个&#39; #include&#39;声明。对于系统头文件,请使用以下格式:

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

写作“家庭成长”时`#include语句使用格式:

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

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

当纠正上述问题时,代码会干净地编译