insert a data to list in c++

I have a function:
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
void list_insert (struct lista *head,int n,int k)
     {
       
      struct lista *wsk=head;
      if(wsk==NULL)

      {wsk=new lista;
      wsk->dana=n;
      wsk->next=NULL;
      head=wsk;
               }
               else{
               
               if(k==0){
               wsk=head;

               do
               {wsk=wsk->next;
               }while(wsk->next==NULL);

     struct lista *p=new lista;
     wsk->next=p;
     p->next=NULL;
     p->dana=n;
     }

               if(k>0){
               int i=0;
               wsk=head;
               do
               {wsk=wsk->next;
               }while(i<k-1);

               struct lista *p=new lista;
               p->next=wsk->next;
               p->dana=n;
               wsk->next=p; }}}

and when I want to insert a data with this function then is an error. where is the problem?
"An error" is not a sufficient description of the problem...
run the debugger, check which line causes the error and why it is caused (by checking the value of the relevant variables).
in main I wrote:
1
2
3
4
5
6
struct lista *head;
    int n,k;
    cin >> k;
    cin>>n;
    cout << endl;
    list_insert(&head,n,k);// here is the error 
Why the ampersand? As head is a pointer to a lista object, you could have just passed head as it was and the types would match up perfectly.

-Albatross
Last edited on
ok. I have a question. How to insert numbers 1-5 to a list?
a wanted to delete a data that choose. Is that correct?
1
2
3
4
5
6
7
8
9
void delete(int element)    
    {wsk=first;                     
      while (wsk->nast != NULL)       
              {          if (wsk->nast->wartosc == element)            
              {             lista *usuwany=wsk->nast;             
     wsk->nast = usuwany->nast;               
     free(usuwany);                    }
  
else          {             wsk = wsk->nast;                }   }    }   
Last edited on
is that program correct??
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
61
struct lista
{
int id;
lista * next;
};

lista *head, *tmp, *tmp2, *nowy;
int index = 1;

void dodajkoniec()
{
if (head==NULL)
{
  lista *nowy = new lista;
head = nowy;
head->id = index;
head->next = NULL;
index++;
}
else
{
tmp=head;
while(tmp->next!=NULL) tmp=tmp->next;
lista *nowy = new lista;
nowy->id = index;
index++;
nowy->next = NULL;
tmp->next = nowy;
}
}

void dodajsrodek(int x) //po którym wstawić
{
tmp=head;
while(tmp->id != x) tmp=tmp->next;
tmp2 = tmp->next;
lista *nowy = new lista;
nowy->id = index;
index++;
tmp->next = nowy;
nowy->next = tmp2;
}

void wyswietl()
{
    tmp=head;
    while(tmp!=NULL) 
    {
    cout<<tmp->id<<endl;
    tmp=tmp->next;
    }
}
//W mainie:
    dodajkoniec();
    dodajkoniec();
    dodajkoniec();
    dodajkoniec();
    dodajkoniec();
    dodajsrodek(3);
    wyswietl();
}
Topic archived. No new replies allowed.