link list

I am trying to wtite a programme for link list using class.I don't know the the error in deleting in the fuction del1 in the class linklist . Please check the error.

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  #include<iostream>
#include<string>
using namespace std;
class linklist
{
    public:
    linklist *ptr;
    string n;
    int j,a;
    void input(string* b)
    {
        n=*b;
    };
    string taken()
    {
        return n;
    };
    void input1(int* b)
    {
        a=*b;
    };
    int taken1()
    {
        return a;
    };
    void display()
    {
        cout<<n<<endl;
        if(ptr!=NULL)
        ptr->display();
    };
    void search(string d)
    {
        linklist *ptr1;
        int i=1;
        j=0;
        if(d==n)
        {
            j++;
            cout<<i<<"\n";
        }
        if(ptr!=NULL)
            {
                i++;
            if(d==ptr->taken())
            {
                j++;
                cout<<i<<"\n";
            }
            ptr1=new linklist;
                ptr1=ptr->ptr;
            }
            cc:
            if(ptr1!=NULL)
            {
                i++;
            if(d==ptr1->taken())
            {
                j++;
                cout<<i<<"\n";
            }
                ptr1=ptr1->ptr;
                goto cc;
            }
            else
            {
                if(j==0)
                cout<<"not found\n";
            }
    };
    void position(int m)
    {
        linklist *ptr1;
        if(m==1)
        cout<<n<<"\n";
        else
        {
            if(m==2)
            {
                if(ptr!=NULL)
                cout<<ptr->taken()<<"\n";
                else
                cout<<"there is no number\n";
            }
            else if(m==3)
            {
                if(ptr!=NULL)
            {ptr1=new linklist;
            ptr1=ptr->ptr;
            if(ptr1!=NULL)
            cout<<ptr1->taken();
            else
                cout<<"there is no number\n";}
            else
                cout<<"there is no number\n";
            }
            else
            {
                if(ptr!=NULL)
                {ptr1=new linklist;
                    ptr1=ptr->ptr;
                for(int k=2;k<m-1;k++)
                    {
                        if(ptr1!=NULL)
                        ptr1=ptr1->ptr;
                    }
                    if(ptr1!=NULL)
                cout<<ptr1->taken();
                else
                cout<<"there is no number\n";
                }
                else
                cout<<"there is no number\n";
            }
        }
    };
    int del(string b)
    {
        if(n==b)
            return 1;
        else return 0;
};
int del1(string b)
{
        linklist *ptr1,*ptr2,*ptr4,*ptr3,*ptr5;
        int i=1;
        ptr2=new linklist;
        ptr2=NULL;
        if(ptr!=NULL)
        {if(b==ptr->taken())
            {ptr2->input1(&i);
            ptr2->ptr=NULL;}
            ptr1=new linklist;
            ptr1=ptr->ptr;
            if(ptr1!=NULL)
            {
                i++;
            if(b==ptr1->taken())
            {
                ptr3=new linklist;
                ptr3->input1(&i);
                ptr2->ptr=ptr3;
                ptr3->ptr=NULL;
            }
                ptr1=ptr1->ptr;
            }
            cc:
            if(ptr1!=NULL)
            {
                i++;
            if(b==ptr1->taken())
            {
                ptr4=new linklist;
                ptr4->input1(&i);
                ptr3->ptr=ptr4;
                ptr3=ptr4;
            }
                ptr1=ptr1->ptr;
                goto cc;
            }
            }
        if(ptr2==NULL)
        return 1;
        else
        {
        ee:
        if(ptr2!=NULL)
        {
            int k;
            k=ptr2->taken1();
        if(k==1)
        ptr=ptr->ptr;
        else if(k==2)
        {
        string z;
            ptr1=new linklist;
                ptr1=ptr->ptr;
            ptr5=new linklist;
            ptr5=ptr1->ptr;
            ptr->ptr=ptr5;
        }
        else if(k>2)
        {
        string z;
            ptr1=new linklist;
                ptr1=ptr->ptr;
            for(int l=2;l<k;l++)
                ptr1=ptr1->ptr;
            ptr5=new linklist;
            ptr5=ptr1->ptr;
            ptr->ptr=ptr5;
        }
        ptr2=ptr2->ptr;
        goto ee;
        }
        return 0;
        }
    };
};
int main()
{
    cout<<"first enter strings\n";
    cout<<"enter \"end\" to stop entering strings\n";
    linklist *f,*t,*s;
    string m;
    cin>>m;
    f=new linklist;
    f->input(&m);
    cin>>m;
    if(m=="end")
    {
        f->ptr=NULL;
        goto aa;
    }
    t=new linklist;
    t->input(&m);
    f->ptr=t;
    bb:
    cin>>m;
    if(m=="end")
    {
        t->ptr=NULL;
        goto aa;
    }
    else
    {
        s=new linklist;
        s->input(&m);
        t->ptr=s;
        t=s;
        goto bb;
    }
    aa:
    cout<<"1-display\n2-search\n3-more input\n4-exit\n5-position\n6-delete\n";
    int n;
    cin>>n;
    switch(n)
    {
        case 1:
        f->display();
        break;
        case 2:
        cin>>m;
        f->search(m);
        break;
        case 3:
        goto bb;
        break;
        case 4:
        goto dd;
        break;
        case 5:
        int o;
        cin>>o;
        f->position(o);
        break;
        case 6:
        o=0;
        cin>>m;
        hh:
        if(f->del(m)==1)
        {
            o++;
            f=f->ptr;
            if(f!=NULL)
            goto hh;
        }
        else
        if(f->del1(m)==1&&o==0)
        cout<<"there is no such string\n";
        break;
        default:
        cout<<"error";
    }
    goto aa;
    dd:
    delete f,s,t;
    return 0;
}
Last edited on
No one can help you until you give the error message, anyway it is named Linked List not linklist
Last edited on
Hi there,

129
130
if(ptr!=NULL)
{if(b==ptr->taken())


You have declared : linklist *ptr1,*ptr2,*ptr4,*ptr3,*ptr5;, but not ptr.
I'm suspecting you mean:

129
130
if(ptr1!=NULL)
{if(b==ptr1->taken())


As senhor mentioned, it is best to state your problem clearly and specifically, including any error messages you get from the compiler, linker or program crash. That helps us to help you better.

Please do let us know if you need any further help.

All the best,
NwN
Ther is error report in any other function except del1. It is showing "segmentation fault (core dumped)"
Hi, NwN
I have declared *ptr in the class at the beginning.
Which platform are you on (windows/linux/...)?
Can you use a debugger (such as gdb)?

Otherwise, you could add std::cout statements to your code to track in the console where your program crashes exactly.

All the best,
NwN
I am using Linux.
I am wrting in code blocks.
How can i track? I don't know please help.
Please see this link for information specific to code::blocks:
http://wiki.codeblocks.org/index.php?title=Debugging_with_Code::Blocks

With GDB, note that your program needs to be compiled with the "-g" flag, you could do:

#gdb (your program name)
gdb> run
[segfault]
gdb> bt


"bt" will show you a backtrace of where the program crashed. For more information about using GDB to track a segfault, please see: http://www.cplusplus.com/articles/iwTbqMoL/

To use debug output statements, do something like this, but in the whole program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
    cout<<"first enter strings\n";
    cout<<"enter \"end\" to stop entering strings\n";

    std::cout << "--Creating linked list pointers\n";
    linklist *f,*t,*s;
    string m;
    cin>>m;

    std::cout << "--initializing f\n";
    f=new linklist;
    f->input(&m);
    cin>>m;
    if(m=="end")
    {
        std::cout << "--m==end, setting f->ptr=NULL\n";
        f->ptr=NULL;
        goto aa;
    }

//... 


Also, note that the use of "goto" is discouraged. Mostly for purposes like this, it gets really tough to debug a program that just jumps from one place to another with no seeming logic behind it.

All the best,
NwN
Last edited on
Topic archived. No new replies allowed.