0

I don't really understand why I've been getting this error : Unknown type name 'DATA'.

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

typedef int DATA;
#include "PriorityQueue.h"


int main(){
    int priority;
    DATA value;
    LINK *head;
    createEmpty(&head);
    add(&head, 1, 7);
    checkIfEmpty(head);
    add(&head, 5, 5);
    add(&head, 3, 4);
    deleteElement(&head, &priority, &value);
    add(&head, 1, 3);
    destroy(head);
    createEmpty(&head);
    add(&head, 10, 10);
    return 0;
}

Main

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

#include "PriorityQueue.h"

void add(LINK **head, int p, DATA v){
    LINK *curr=(*head);
    LINK *beforeCurr=(*head);
    LINK *newElement=NULL;
    if((*head)->priority==-1){
        (*head)->priority=p;
        (*head)->value=v;
    }

    else{
        newElement=(LINK*)malloc(sizeof(LINK));
        newElement->priority=p;
        newElement->value=v;
        newElement->next=NULL;

        while(p>(curr->priority) && curr->next!=NULL){
            beforeCurr=curr;
            curr=curr->next;
        }

        if(curr->next==NULL && p>(curr->priority))
            curr->next=newElement;

        else if(curr==(*head)){
            newElement->next=curr;
            (*head)=newElement;
        }
        else{
            beforeCurr->next=newElement;
            newElement->next=curr;
        }
    }
}

void deleteElement(LINK **head, int *p, DATA *v){
    LINK *curr=(*head);
    LINK *currB=(*head);
    while(curr->next!=NULL) {
        currB=curr;
        curr=curr->next;
    }
    currB->next=NULL;
    (*p)=curr->priority;
    (*v)=curr->value;
    free(curr);
}

void checkIfEmpty(LINK *head){
    if(head->priority==-1)
    printf("Queue is empty\n\n");
    else
    printf("Queue is not empty\n\n");
}

void destroy(LINK *head) {
    LINK *current;
    LINK *next;
    current=head;
    while(current!=NULL){
        next=current->next;
        free(current);
        current=next;
    }
    head=NULL;
}


void createEmpty(LINK **head) {
    LINK *curr=NULL;
    curr=(LINK*)malloc(sizeof(LINK));
    curr->next = NULL;
    curr->priority = -1;
    (*head)=curr;
}

Functions file

#ifndef PRIORITY_QUEUE_H_
#define PRIORITY_QUEUE_H_

typedef struct priorityQueue{
    int priority;
    DATA value;
    struct priorityQueue *next;
}LINK;

void createEmpty(LINK **head);
void add(LINK **head, int p, DATA v);
void deleteElement(LINK **head, int *p, DATA *v);
void checkIfEmpty(LINK *head);
void destroy(LINK *head);
#endif

Header

I use a project on codeblocks to compile it. Also, "typedef int DATA;" on main would be preferred

DevGambit
  • 53
  • 1
  • 9
  • possible duplicate of [Passing defined type in main to header in C](http://stackoverflow.com/questions/28811140/passing-defined-type-in-main-to-header-in-c) – user694733 Mar 03 '15 at 07:46
  • This is duplicate of your earlier post.Please don't double post. Delete this, and edit your old post. – user694733 Mar 03 '15 at 07:48

2 Answers2

1

The typedef keyword obeys scoping rules, as explained here: Are typedef and #define the same in c?

This means that if you declare:

typedef int DATA;

And this line isn't included in another file it wont be declared there.

DATA should be declared in every file that uses it either by including another file which declared it or by explicitly declaring it.

Community
  • 1
  • 1
antonpuz
  • 3,256
  • 4
  • 25
  • 48
0

You should declare typedef int DATA; in a header file and include that header file in all your source files.

Currently there is no way for other source files to know what DATA is.

Vagish
  • 2,520
  • 19
  • 32