Linked List from Text

sorry for my english hi guys i need help in linked list getting elements from the text file. pls fix it for me. I can't get read from text file.I know forgat c++.It is my midterm questions.

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
  #include<iostream>
#include<fstream>
#include<cstdio>
#include<cstdlib>

using namespace std;

struct node
{
    int info;
    struct node *next;
    struct node *prev;
}*start, *last;
int counter = 0;

class double_clist
{
    public:
        node *create_node(int);
        void insert_last();
        void delete_pos();
        void search();
        void display();
        void reverse();
        double_clist()
        {
            start = NULL;
            last = NULL;
        }
};
int main()
{
    int choice;
    double_clist cdl;
    while (1)
    {

ifstream readFile("C:\\Numbers.txt".c_str());
    bool flag = true;
    if (readFile.is_open()) {
        while(readFile >> line) {
            int_line = atoi(line.c_str());
            cout << int_line << endl;
            double_clist.insert_last(int_line);

	
}
		
		
		cout<<"\n-------------------------------"<<endl;
        cout<<"Veri Yapilari Odevi"<<endl;
        cout<<"\n-------------------------------"<<endl;
        cout<<"1.Yeni Sayi Ekleme,"<<endl;
        cout<<"2.Sayi Arama,"<<endl;
        cout<<"3.Elemanlari Listele,"<<endl;
        cout<<"4.Elemanlari Tersten Sirala,"<<endl;
        cout<<"5.Eleman Sayisi Ogrenme,"<<endl;
        cout<<"6.Eleman Silme,"<<endl;
        cout<<"7.Cikis"<<endl;
        cout<<"\n-------------------------------"<<endl;
        cout<<"Seciminizi Girin : ";
        cin>>choice;
        

        
        switch(choice)
        {

        case 1:
            cdl.insert_last();
            break;
        case 2:
            cdl.search();
            break;
        case 3:
            cdl.display();
            break;
        case 4:
            cdl.reverse();
            break;
        case 5:
            exit(1);
        case 6:
            cdl.delete_pos();
            break;
        case 7:
            exit(1);
            break;
        default:
            cout<<"Hatalı secim!!"<<endl;
        }
    }
    return 0;
}

void double_clist::search()
{
    int pos = 0, value, i;
    bool flag = false;
    struct node *s;
    if (start == last && start == NULL)
    {
        cout<<"Listede hic eleman yok!"<<endl;
        return;
    }
    cout<<endl<<"Aranacak degeri girin : ";
    cin>>value;
    s = start;
    for (i = 0;i < counter;i++)
    {
        pos++;
        if (s->info == value)
        {
            cout<<value<<"Degerli Eleman"<<pos<<"bu adreste."<<endl;
            flag = true;
        }
        s = s->next;
    }
    if (!flag)
        cout<<"Eleman Bulunamadi"<<endl;
}

node* double_clist::create_node(int value)
{
    counter++;
    struct node *temp;
    temp = new(struct node);
    temp->info = value;
    temp->next = NULL;
    temp->prev = NULL;
    return temp;
}

void double_clist::display()
{
    int i;
    struct node *s;
    if (start == last && start == NULL)
    {
        cout<<"Liste bos!"<<endl;
        return;
    }
    s = start;
    for (i = 0;i < counter-1;i++)
    {
        cout<<s->info<<"<->";
        s = s->next;
    }
    cout<<s->info<<endl;
}

/*
 *INSERTS ELEMNET AT LAST
 */

void double_clist::insert_last()
{
    int value;
    cout<<endl<<"Eklenecek Eleman: ";
    cin>>value;
    struct node *temp;
    temp = create_node(value);
    if (start == last && start == NULL)
    {
        cout<<"Sayi Eklendi "<<endl;
        start = last = temp;
        start->next = last->next = NULL;
        start->prev = last->prev = NULL;
    }
    else
    {
        last->next = temp;
        temp->prev = last;
        last = temp;
        start->prev = last;
        last->next = start;
    }
}

/*
 * Delete Node at Particular Position
 */

void double_clist::delete_pos()
{
    int pos, i;
    node *ptr, *s;
    if (start == last && start == NULL)
    {
        cout<<"Liste Bos, Silinebilecek Eleman Yok!"<<endl;
        return;
    }
    cout<<endl<<"Silinmesini Istediginiz Eleman Kacinci Sirada : ";
    cin>>pos;
    if (counter < pos)
    {
        cout<<"Girilen Adreste Eleman Yok!"<<endl;
        return;
    }
    s = start;
    if(pos == 1)
    {
        counter--;
        last->next = s->next;
        s->next->prev = last;
        start = s->next;
        free(s);
        cout<<"Sayi Silindi."<<endl;
        return;
    }
    for (i = 0;i < pos - 1;i++ )
    {
        s = s->next;
        ptr = s->prev;
    }
    ptr->next = s->next;
    s->next->prev = ptr;
    if (pos == counter)
    {
        last = ptr;
    }
    counter--;
    free(s);
    cout<<"Sayi Silindi."<<endl;
}
/*
 * Update value of a particular node
 */


void double_clist::reverse()
{
    if (start == last && start == NULL)
    {
        cout<<"Listede Hic Eleman Yok!"<<endl;
        return;
    }
    struct node *p1, *p2;
    p1 = start;
    p2 = p1->next;
    p1->next = NULL;
    p1->prev = p2;
    while (p2 != start)
    {
        p2->prev = p2->next;
        p2->next = p1;
        p1 = p2;
        p2 = p2->prev;
    }
    last = start;
    start = p1;
    return (display());
}

Last edited on
Hello OrseskO,

OrseskO wrote:

I can't get read from text file.

I can not get it to read from the file either. Maybe that is because I have no file to use.

I would be easier if you post the file, or a good sample if large, so that everyone can use the same information. If I create a file it may work better than what you have because it would be different.

I noticed in your code that you are using 2 different styles of {}s. DO NOT do that. Choose a style and be consistent with it.

Andy
Hello OrseskO,

When I tried to compile your program there are several errors that need fixed.

From the shell program here:

 In function 'int main()':
38:37: error: request for member 'c_str' in '"C:\\Numbers.txt"', which is of non-class type 'const char [15]'
41:27: error: 'line' was not declared in this scope
42:13: error: 'int_line' was not declared in this scope
44:25: error: expected unqualified-id before '.' token
39:10: warning: unused variable 'flag' [-Wunused-variable]
96:26: error: qualified-id in declaration before '(' token
253:1: error: expected '}' at end of input


Watch your {}s. The closing } of "main" is missing.

Line 93 should that be inside the while loop or outside the while loop?

Andy
Thank you Andy i changed my code today. and it read the text file but i can't add my linked list.here is the code.

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
#include<iostream>
#include<fstream>
#include<cstdio>
#include<cstdlib>

using namespace std;

struct node
{
    int info;
    struct node *next;
    struct node *prev;
}*start, *last;
int counter = 0;

class double_clist
{
    public:
        node *create_node(int);
        void insert_last();
        void delete_pos();
        void search();
        void sayma ();
        void display();
        void reverse();
        double_clist()
        {
            start = NULL;
            last = NULL;
        }
};



int main()
{
  
  
  
  

  

    int choice;
    double_clist cdl;
    while (1)
    {
        cout<<"\n-------------------------------"<<endl;
        cout<<"Veri Yapilari Odevi"<<endl;
        cout<<"\n-------------------------------"<<endl;
        cout<<"1.Yeni Sayi Ekleme,"<<endl;
        cout<<"2.Sayi Arama,"<<endl;
        cout<<"3.Elemanlari Listele,"<<endl;
        cout<<"4.Elemanlari Tersten Sirala,"<<endl;
        cout<<"5.Eleman Sayisi Ogrenme,"<<endl;
        cout<<"6.Eleman Silme,"<<endl;
        cout<<"7.Cikis"<<endl;
        cout<<"\n-------------------------------"<<endl;
        cout<<"Seciminizi Girin : ";
        cin>>choice;
        

        
        switch(choice)
        {

        case 1:
            cdl.insert_last();
            break;
        case 2:
            cdl.search();
            break;
        case 3:
            cdl.display();
            break;
        case 4:
            cdl.reverse();
            break;
        case 5:
            cdl.sayma();
        case 6:
            cdl.delete_pos();
            break;
        case 7:
            exit(1);
            break;
        default:
            cout<<"Hatalı secim!!"<<endl;
        }
    }
    return 0;
}

void double_clist::search()
{
    int pos = 0, value, i;
    bool flag = false;
    struct node *s;
    if (start == last && start == NULL)
    {
        cout<<"Listede hic eleman yok!"<<endl;
        return;
    }
    cout<<endl<<"Aranacak degeri girin : ";
    cin>>value;
    s = start;
    for (i = 0;i < counter;i++)
    {
        pos++;
        if (s->info == value)
        {
            cout<<value<<"Degerli Eleman"<<pos<<"bu adreste."<<endl;
            flag = true;
        }
        s = s->next;
    }
    if (!flag)
        cout<<"Eleman Bulunamadi"<<endl;
}

node* double_clist::create_node(int value)
{
    counter++;
    struct node *temp;
    temp = new(struct node);
    temp->info = value;
    temp->next = NULL;
    temp->prev = NULL;
    return temp;
}

void double_clist::display()
{
    int i;
    struct node *s;
    if (start == last && start == NULL)
    {
        cout<<"Liste bos!"<<endl;
        return;
    }
    s = start;
    for (i = 0;i < counter-1;i++)
    {
        cout<<s->info<<"<->";
        s = s->next;
    }
    cout<<s->info<<endl;
}

/*
 *INSERTS ELEMNET AT LAST
 */

void double_clist::insert_last()
{
    int value;
    cout<<endl<<"Eklenecek Eleman: ";
    cin>>value;
    struct node *temp;
    temp = create_node(value);
    if (start == last && start == NULL)
    {
        cout<<"Sayi Eklendi "<<endl;
        start = last = temp;
        start->next = last->next = NULL;
        start->prev = last->prev = NULL;
    }
    else
    {
        last->next = temp;
        temp->prev = last;
        last = temp;
        start->prev = last;
        last->next = start;
    }
}

/*
 * Delete Node at Particular Position
 */

void double_clist::delete_pos()
{
    int pos, i;
    node *ptr, *s;
    if (start == last && start == NULL)
    {
        cout<<"Liste Bos, Silinebilecek Eleman Yok!"<<endl;
        return;
    }
    cout<<endl<<"Silinmesini Istediginiz Eleman Kacinci Sirada : ";
    cin>>pos;
    if (counter < pos)
    {
        cout<<"Girilen Adreste Eleman Yok!"<<endl;
        return;
    }
    s = start;
    if(pos == 1)
    {
        counter--;
        last->next = s->next;
        s->next->prev = last;
        start = s->next;
        free(s);
        cout<<"Sayi Silindi."<<endl;
        return;
    }
    for (i = 0;i < pos - 1;i++ )
    {
        s = s->next;
        ptr = s->prev;
    }
    ptr->next = s->next;
    s->next->prev = ptr;
    if (pos == counter)
    {
        last = ptr;
    }
    counter--;
    free(s);
    cout<<"Sayi Silindi."<<endl;
}
/*
 * Update value of a particular node
 */
void double_clist::sayma(){
	
	  std::ifstream file("C:\\Sayi.txt");
  std::string str;
  while (std::getline(file, str)) {
		
  }	
  
}

void double_clist::reverse()
{
    if (start == last && start == NULL)
    {
        cout<<"Listede Hic Eleman Yok!"<<endl;
        return;
    }
    struct node *p1, *p2;
    p1 = start;
    p2 = p1->next;
    p1->next = NULL;
    p1->prev = p2;
    while (p2 != start)
    {
        p2->prev = p2->next;
        p2->next = p1;
        p1 = p2;
        p2 = p2->prev;
    }
    last = start;
    start = p1;
    return (display());
}



problem is double_clist::sayma()
Last edited on
how can i have return func with integer to insert_last
Hello OrseskO,

I am a bit stuck until I can see what the input file looks like.

Your OP says:
It is my midterm questions

With the include files, the struct and the class I would say that you are off to a poor start.

The class appears to be extra work just to get to the functions. I am wondering if that is a requirement or something you thought of.

In your revised code:
1
2
3
4
5
6
7
8
9
void double_clist::sayma(){
	
	  std::ifstream file("C:\\Sayi.txt");
  std::string str;
  while (std::getline(file, str)) {
		
  }	
  
}

In the rest of your code you use a different style of {}s and then change it here. DO NOT do that. It throws people off when reading and finding some thing different.

I take it that you figured out that the ".c_str()" function does not work. If you do not know this function is part of the "std::string" class and used to change a "std::string" into a C string, a "char" array.

Also ("C:\\Sayi.txt") is already a C string, so there is no need to change it.

The while loop does nothing at this time. I am thinking what you should be doing is reading the file then create the linked list until you reach end of file.

Andy
Yes i am trying to reading end of file.
I'm sorry to waste your time.
Hello OrseskO,

You are not wasting my time except for not posting the input file that I have asked for. What you are using and what I have created may work differently there for any suggestions I have may not work or do you any good.

With my initial testing I have opened the switch and case statements for 1, 3 and 7 ant they all appear to work for now.

At the beginning of your program you have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct node
{
    int info;
    struct node *next;
    struct node *prev;
}*start, *last;  // <--- These two variables are set to "NULL" because they are global variables.

int counter = 0;  // <--- Should not be a global variable.

class double_clist
{
    public:
        node *create_node(int);
        void insert_last();
        void delete_pos();
        void search();
        void display();
        void reverse();
        double_clist()  // <--- Redundant as the variables are already initialized.
        {
            start = NULL;
            last = NULL;
        }
};

In line 6 the two variables, what most call "head" (kafa) and tail (kuyruk) are initialized when the program compiles because they are global variables. This could also be implementation dependent as a different compiler and set of header files may treat this differently.

When it comes to the default ctor and dtor just let the compiler take care of this.

In "main you define bool flag = true;, but never use it in "main". Did you have a reason for this?

The "insert_last()" function works, but the code gives me the feeling that it is not what it should be. I still have to look into this more.

The "display()" function works, but I think the ("<->") part is more than you need. A simple (", ") would work and be easier to read.

Andy
Topic archived. No new replies allowed.