Typedef struct error in c program

Thank you for sharing your know-how :)! I just wrote this c program in devC ++ and I have many errors about typdef and structure eg: "invalid use of undefined type struct item'" for every line with "->" operator, "forward declaration of

struct item '" for line 4 this is the code:

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

typedef struct item* ptr;
typedef struct itme
{
        int data;
        ptr next;
}node;
void add2list(ptr*,int);
void freeList(ptr*);
int main()
{
    ptr H=NULL;
    ptr p3=H;
    int num;
    while (scanf("%d",&num)!=EOF)
          add2list(&H,num);

    while(p3)
    {
            printf("%d  ",p3->data);
            p3=p3->next;
    }
    printf("end\n");   
    freeList(&H);
    return 0;
}

void add2list(ptr* H, int num)
{
     ptr p1,p2,T;
     T=(ptr)malloc(sizeof(node));
     if(!T)
     {
           printf("cannot allocate memory\n");
           exit(0);
     }
     t->data=num;
     p1=*H;
     while(p1)
     {
           if(p1->data==num)
           {
              free(T);
              goto duplicate;
           }   
           else 
           {
                p2=p1;
                p1=p1->next;
           }
     }
     T->next=p1;
     if(p1==*H)
         *H=T; 
     else
     p2->next=T;
     duplicate:;
}   
void freeList(ptr* H)
{
     ptr p1;
     while(H)
     {
         p1=*H;
         (*H)=p1->next;
         free(p1);
     }

}

      

thank!

+3


source to share


2 answers


I haven't looked all your code, but it seems like there is a typo

typedef struct item * ptr;

typedef struct itme {

Also this piece of code

while(p3)
{
        printf("%d  ",p3->data);
        p3=p3->next;
}

      

doesn't make sense because it p3

was explicitly initialized to NULL

ptr H=NULL;
ptr p3=H;

      

At least before the loop, you should add the operator

p3 = H;

      



There add2list

is also another typo in the function

You have specified a pointer T

, but then use a pointerT

void add2list(ptr* H, int num)
{
     ptr p1,p2,T;
     T=(ptr)malloc(sizeof(node));
     if(!T)
     {
           printf("cannot allocate memory\n");
           exit(0);
     }
     t->data=num;

      

The freeList function is also wrong.

Instead of approving

 while(H)

      

should be

 while( *H )

      

0


source


The main problem is that you wrote "item" "itme". Happens among us.



+1


source







All Articles