0

Hi I write a library for linked lists but i get this errors and issues. I don't know what to do.If i change in the first typedef struct listNode with list and second listNode with listItem and then replace in linked_lists all listNode with listItem all works fine but i need to implement a list initialisation function and i think to make another struct that store head of list and number of items and i arrived at this point when i have this errors.linked_list.clinked_lists.clinked_lists.h

linked_lists.h code:

#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 code:

#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);
}
1
  • 2
    Where is struct list defined? You have a struct listaa but no struct list. Commented Apr 9, 2016 at 11:16

2 Answers 2

1

the posted code contains several problems. Lets start with the header file:

#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) these two lines are completely unneeded in the header file and should be removed:

#include <stdio.h>

struct listNode;

2) this typedef is incorrectly declared:

typedef struct listNode{

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

suggest:

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

typedef struct listNode  ListNode;

3) this declaration contains a 'incomplete' struct definition:

struct listaa{

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

Suggest using the 'typedef' name from the prior struct definition.

(another excellent example of why names should be different by more than just capitalization)

struct listaa{

    int count;
    ListNode *head;   
};

4) the parameter lists in the function prototypes all contain 'incomplete' struct references:

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

The prototypes need to use either 'struct listNode' or type 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);

Notice the improvement in readability obtain by the vertical alignment.

Of course, all the actual function signatures need this same correction to use the 'typedef' name rather than the 'incomplete' struct definition

Then, your header file, which uses nothing from the 'stdio.h' header file should not be #include'ing that file. Rather place the #include <stdio.h> statement in the body of the code.

Note: when writing a '#include' statement. For system header files, use the format:

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

when writing 'home grown' `#include statements use the format:

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

The major difference is the search order used by the compiler to find the actual header file. This allows the replacement of a system header file with a home grown header file., makes the searchs somewhat faster, and helps with producing 'self documenting' code.

When the above problems are corrected, the code cleanly compiles

Sign up to request clarification or add additional context in comments.

Comments

1

main issue I see here is struct list *next; inside your first typedef it should be struct listNode *next. I am not aware of any generic keyword list provided by C.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.