Thanks for sharing your knowladge:)! I just wrote this c program in devc++ and I've got alot of errors about the typdef and the struct like: "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);
}
}
thanks!
The main issue is that you spelled "item" "itme". Happens to the best of us.
I did not looked all your code but it seems there is a typo
typedef struct item* ptr;
typedef struct itme {
Also this code snippet
while(p3)
{
printf("%d ",p3->data);
p3=p3->next;
}
has no sense because p3
was explicitly initialized by NULL
ptr H=NULL;
ptr p3=H;
At least before the loop you should add statement
p3 = H;
Also in function add2list
there is another typo
You declared pointer T
but then further are using pointer t
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;
Function freeList is also wrong.
Instead of statement
while(H)
there shall be
while( *H )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.