Linked list

So I have linked list and function which deletes element if next element is bigger, so my code is working but its not working with first element, in the comment I have wrote code which I would code for checking that first element, but when ever I check it is blowing up all program, wtf I just dont understand the problem I have painted all that situation and still dont understand where is the problem, can any1 help?


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
62
63
64
65
66
67
68
69
70
  #include <iostream>
using namespace std;

struct llist
{
     int x;
    llist *next;
};

void trash (llist *first, llist *sec, llist *del, llist *last)
{
    sec = first;
    while (sec -> next != last){

                /*****
                if ( sec==first and (sec->x) > (sec->next->x)){
                    del=sec;
                    first=sec->next;
                    sec=first;
                    delete del;
                }

                else{
                *****/
             if ((sec->next->x) > (sec->next->next->x) and (sec->next->next->x) !=0){
                            del=sec->next;
                            sec->next = del->next;
                            delete del;
                            del=NULL;
                } else sec = sec ->next;

    }
}

int main()
{
    llist *first = new llist;
    llist *last, *sec, *del;

    cin >> first -> x ;

    last = first;

    while (last -> x != 0){
    last = last -> next = new llist;
    cin >> last -> x;
    }
    last -> next = NULL;

    trash(first,sec,del,last);

    sec=first;
    while (sec!=last){
            cout << sec -> x;
            cout << " ";
            sec = sec -> next;
    }

    while (first)
    {
        llist* old = first;
        first = first->next;
        delete old;
    }
    if (first==NULL) cout << "Deleting succesful"; else cout << "Lose";

return 0;

}
You are passing null pointers to your function. Then you dereference those pointers, what do you expect to happen at that point?

http://coliru.stacked-crooked.com/a/085f0f86ff136413

Topic archived. No new replies allowed.