functions for linklist dont work

this are two functions that are for adding and removing and item on links list that im using in a nother program
im just not shure how to fix the erors mainly line 6


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 #include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;


struct node {
  char name[50];
  int age;
  struct node *next;
};

int deleteNode(struct node *h, string name){
struct node *p, *q;
  p=h->next;
  q=h;
  while(p!=NULL){
    if(strcmp(p->name, name.c_str())==0){
    q->next=p->next;
    free(p);
    return 1;
    }
    else {
      q=p;
      p=p->next;
    }
  }
  return 0;
}

void printNode(struct node *h){
struct node *p;
  p=h;
  while(p!=NULL){
    cout << p->name << ", " << p->age << endl;
    p=p->next;
  }
}

int addNode(struct node *h, string name, int age){
struct node *p, *q, *r;
  p=h->next;
  q=h;
  r=(struct node *)malloc(sizeof(struct node));
  strcpy(r->name, name.c_str());
  r->age=age;
  while(p!=NULL){
    if(strcmp(name.c_str(), p->name)<0){
 // add the node
      q->next=r;
      r->next=p;
      return 1;
    }
    q=p;
    p=p->next;
  }
  q->next=r;
  r->next=p;
  return 1;
}
Last edited on
What is the error?
it says for line 6 "[Error] invalid use of incomplete type 'struct node'"
Last edited on
So, you need to define struct node

maybe something like that:
1
2
3
4
5
6
7
8
struct node
{
  node *next;

  node() : next()
  {
  }
};
ok I got it thanks so much (:
Topic archived. No new replies allowed.