Linked Lists

Hi! I have a problem. I'm trying to compare a single value with a value from my Linked list and if they are same, I want to add the value from the list to new list. In other words I want to create a new List with values with the first one. Here is the code that I made, but it's not working. Can someone help me please?

This is the code with which I search in the first list for a node with a value. In the main() function I have A.find_city(), so it can start from the start_pointer from the first list:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void List::find_city()
      {
        List *temp1;
        int b = 0;
        char city[20];
        cout << "Enter city: ";
        cin >> city;
        temp1 = start_ptr;
            while(temp1 != NULL)
              {
                if(temp1->city != city)
                  {
                    B.in(temp1);
                  }
                temp1 = temp1->next;
              }     
        B.print_all();       
      }


This is the code with which I add a node to the new list:
1
2
3
4
5
6
7
8
9
10
11
12
13
void List::in(List *temp1)
      {
        List *temp2;
        if(start_ptr == NULL) 
            start_ptr = temp1;
        else
          {
            temp2 = start_ptr;
            while(temp2->next != NULL)
              temp2 = temp2->next;
            temp2->next = temp1;      
          }                 
      }


I hope you can help me because I'm trying to do this for two days, but there is no result :/
Last edited on
Hi there!

Part of the problem might be that you're comparing your character string wrong. In void List::find_city() you compare both cities using the following if-statement: if(temp1->city != city). It looks to like the statement will always return true this way.

Since you are using C++ I recommend you use the String class, especially when handling certain operations like string manipulation or comparisons. You could do this as the following:

1
2
3
4
5
  std::string str1 ("green apple");
  std::string str2 ("red apple");

  if (str1.compare(str2) != 0)
    std::cout << str1 << " is not " << str2 << '\n';

(taken from: http://www.cplusplus.com/reference/string/string/compare/)

Using your character array code you would want to look in to strcmp http://en.cppreference.com/w/cpp/string/byte/strcmp. An example of this would be:
 
if (!strcmp(temp1->city, city))


Be sure to check your program with a debugger to see where it goes wrong. I hope this helps!
Last edited on
Thanks for the reply.
I changed my statement to
if(strcmp(temp1->city, city) == 0)
but it adds every node no matter if it's the same or not with 'city' to the second list :/
This sounds odd to me cause I programmed out the following example which works perfectly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstring>

using namespace std;

int main( void )
{
    char city1[20] = "New Y0rk";
    char city2[20] = "New York";

    cout << "City one: " << city1 << endl;
    cout << "City two: " << city2 << endl;

    if ( strcmp(city1, city2) )
        cout << "Match!" << endl;
    else
        cout << "No match!" << endl;

    return 0;
}


Since you're using cin() to get the value name there is bound to be a newline \n an the end of the input. The cities which you are checking this with might not have this? As I mentioned before, try to use a debugging tool to see if the strings are what you are expecting them to be. It's really a useful tool :-)
It's still not working :/
Do you want to give you the code and test it by yourself?
Here it is. I will post it in two separate posts because it's too long:

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
#include<iostream>
    #include<string>
    using namespace std;

    class List
      {
         public:
         int code;
         char city[20];
         char street[100];
         double plosht;
         int rooms;
         double naem;
         List *next; 
         List *start_ptr;  
         List();
         void add_node();
         void sort();
         void modify();
         void find_city();
         void in(List *temp1);
         void print_all();
      };
List A;
List B;
    
    void print_this(List *temp1);
    
    List::List()
      {
        start_ptr = NULL;          
      }
    
    void List::add_node()
      {
        List *temp1, *temp2;
        temp1 = new List;
        bool yes = false;
        while(!yes)
        {
            yes = true;
            cout << "Code    : ";
            cin >> temp1->code;   
            temp2 = start_ptr;
            while(temp2 != NULL)
              {
                if(temp1->code == temp2->code) yes = false;
                temp2 = temp2->next;  
              } 
        }
        char p[100];
        cin.getline(p, 100);
        cout << "Address : " << endl;
        cout << " " << "-City   : ";
        cin.getline(temp1->city, 20);
        cout << " " << "-Street : ";
        cin.getline(temp1->street, 100);
        cout << "Plosht  : ";
        cin >> temp1->plosht;
        cout << "Stai    : ";
        cin >> temp1->rooms;
        cout << "Naem    : ";
        cin >> temp1->naem;  
        cout << endl;      
        temp1->next = NULL; 
        if(start_ptr == NULL)
          start_ptr = temp1;
        else
          {
            temp2 = start_ptr;
            while(temp2->next != NULL)
              temp2 = temp2->next;
            temp2->next = temp1;          
          }                   
      }
      
    void List::modify()
      {
        List *temp1, *temp2;
        bool find = false;
        int kod;
        while(!find)
          {
            cout << "Enter code: ";
            cin >> kod;
            temp1 = start_ptr;
            if(start_ptr == NULL) cout << "Empty List!" << endl;
            else
              {
                while(temp1 != NULL)
                  {
                    if(kod == temp1->code) 
                    {
                      print_this(temp1);
                      find = true;
                      temp2 = temp1;
                    } 
                    temp1 = temp1->next;         
                  }  
                 if(!find) 
                   {
                     cout << "Can't find such load!" << endl; 
                   }        
              }
          }
        cout << endl;
        char quest;
        cout << "=====================================" << endl;
        cout << "| Press 1 and Enter to change data. |"<< endl;
        cout << "| Press 2 and Enter to delete node. |" << endl;
        cout << "=====================================" << endl;
        cin >> quest;
        if(quest == '1')
          {
             cout << "Do you want to use the old code? (Y/N): ";
             cin >> quest;
             if(quest == 'Y' || quest == 'y')
               {
                 cout << "Code    : ";
                 cout << temp2->code << endl;
                 char p[100];
                 cin.getline(p, 100);
                 cout << "Address : " << endl;
                 cout << " " << "-City   : ";
                 cin.getline(temp2->city, 20);
                 cout << " " << "-Street : ";
                 cin.getline(temp2->street, 100);
                 cout << "Plosht  : ";
                 cin >> temp2->plosht;
                 cout << "Stai    : ";
                 cin >> temp2->rooms;
                 cout << "Naem    : ";
                 cin >> temp2->naem;  
                 cout << endl;       
               }
             else 
               {
                 List *temp3;
                 temp3 = start_ptr;
                 bool yes = false;
                 while(!yes)
                   {
                     yes = true;
                     cout << "Code    : ";
                     cin >> kod;
                     while(temp3 != NULL)
                       {
                         if(kod == temp3->code)  yes = false;
                         temp3 = temp3->next;       
                       }         
                   }    
                  temp2->code = kod;
                  char p[100];
                  cin.getline(p, 100);
                  cout << "Address : " << endl;
                  cout << " " << "-City   : ";
                  cin.getline(temp2->city, 20);
                  cout << " " << "-Street : ";
                  cin.getline(temp2->street, 100);
                  cout << "Plosht  : ";
                  cin >> temp2->plosht;
                  cout << "Stai    : ";
                  cin >> temp2->rooms;
                  cout << "Naem    : ";
                  cin >> temp2->naem;  
                  cout << endl;        
               }
          }
          if(quest == '2')
            {
              List *temp3 = start_ptr;
              while(temp3->next != NULL)
                {
                  if(temp3->next->code == temp2->code)
                    {
                      temp3->next = temp2->next;                  
                    }          
                  temp3 = temp3->next;
                  delete temp2;
                }
            }                
      }
And the second part:

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
void print_this(List *temp1)
      {
        cout << "Code    : ";
        cout << temp1->code << endl;
        cout << "Address : " << endl;
        cout << " " << "-City   : " << temp1->city << endl;
        cout << " " << "-Street : " << temp1->street << endl;
        cout << "Plosht  : ";
        cout << temp1->plosht << endl;
        cout << "Stai    : ";
        cout << temp1->rooms << endl;
        cout << "Naem    : ";
        cout << temp1->naem << endl;  
        cout << endl;                         
      }
      
    
      
    void List::in(List *temp1)
      {
        List *temp2;
        if(start_ptr == NULL) 
            start_ptr = temp1;
        else
          {
            temp2 = start_ptr;
            while(temp2->next != NULL)
              temp2 = temp2->next;
            temp2->next = temp1;      
          }                 
      }
    
      
    void List::find_city()
      {
        List *temp1;
        int b = 0;
        char city[20];
        cout << "Enter city: ";
        cin >> city;
        temp1 = start_ptr;
            while(temp1->next != NULL)
              {
                if(strcmp(temp1->city, city))
                  {
                    B.in(temp1);
                  }
                temp1 = temp1->next;
              }     
        B.print_all();       
      }
    
    void List::print_all()
      {
        List *temp1;
        cout << "Start the output!" << endl;
        temp1 = start_ptr;
        if(start_ptr == NULL)
          cout << "Empty List!" << endl;
        else
          {
            while(temp1 != NULL)
              {
                cout << "Code    : ";
                cout << temp1->code << endl;
                cout << "Address : " << endl;
                cout << " " << "-City   : " << temp1->city << endl;
                cout << " " << "-Street : " << temp1->street << endl;
                cout << "Plosht  : ";
                cout << temp1->plosht << endl;
                cout << "Stai    : ";
                cout << temp1->rooms << endl;
                cout << "Naem    : ";
                cout << temp1->naem << endl;  
                cout << endl;  
                temp1 = temp1->next;          
              }     
          }                 
      }

    int main()
      {
        int n;
        cin >> n;
        for(int i = 0; i <= n-1; i++)
          A.add_node();
        cout << "--------------------------------" << endl;
        A.find_city();
        system("pause");
        return 0;        
      }
I have been looking but I am unsure on how to get your program to work. Cause whatever I enter, I keep ending up in a endless loop. Maybe you can provide some sample input to run the program?

-----

That being said the strcmp code should work, but you need to include #include<cstring> . In you're example your including the string library... Which is great! But then you might as well use the previous string example which I gave you.
First you should enter the number of the nodes you want to enter in the list. For examle 2.
So this is a sample input:
2
Code: 1234 // Just a random number
Address:
City: London // The name of the city
Street: Some name
Plosht: 2 // Just a random number
Stai: 2 // Just a random number
Naem: 2 // Just a random number

Code: 4321 // Again just a random number but different from the previous (beacause it should be unique)
Address:
City: Manchester// The name of the city
Street: Some name
Plosht: 2 // Just a random number
Stai: 2 // Just a random number
Naem: 2 // Just a random number

Then it will ask you to enter a city name. For example you can enter London and the program should add to the new list every node in which for city is written London. And then it should output the list.
Maybe something with my loops is wrong but I don't know what :/

It's a bit confusing but I hope you get my point :)
Yes, thank you. That was very helpful. The endless loop appears on invalid input (maybe useful to know).

What's going wrong is that in your method find_city() you create a temporarily list named temp1 which points to the start of your list. In your loop here you walk through your list until pointer to the NEXT element is NULL. So, think to yourself what happens if a list has only one element or the element you want to check is the final element in the list? It would not find it ever because the reference to 'next' from that element is NULL. This is a implementation flaw in your list and you should work that out. One way of doing this is always checking the previous element, but I'll leave the implementation up to you.

In your if-else statement the check was still done wrongely. You had if(strcmp(temp1->city, city)) while it should have been if(!strcmp(temp1->city, city)). After this it only goes into your if-statement on the moment the city name matches.

I will have to look into why your code still prints all the items but I have to log of for now :)
Thanks a lot for your efforts :) I will try this :)
Topic archived. No new replies allowed.