Linked Lists & Recursion

I am confused on how to actually create a linked list. The goal of this code is to allow the user to enter an ID and print the name with that ID or an empty string if the ID doesn't exist. Specifically, I need to use the head variable and new command to create a linked list of 4 elements with a name and ID in each.

Here's what I have so far; it prints an output, but not what I need.

#include <iostream>
using namespace std;

class node{
private:
string name;
int ID;
node *next;
public:
void setnext(node *n){next = n;}
void setdata(int, string, node*);
string search(int);
void printlist();
};
void node::printlist(){
cout << ID << " " << name << endl;
if(next != NULL)
next -> printlist();
}

node *head1;
int main(){
cout << "Enter ID to search: " << flush;
node *head, *p;
head = new node();
head -> setdata(1, "Sally", NULL);
head -> setdata(2, "George", NULL);
head -> setdata(3, "Tom", NULL);
head -> setdata(4, "Grace", NULL);
//head -> setnext(NULL);
for(int i = 0; i < 4; i++){
p = new node();
//p -> setdata(i);
p -> setnext(head);
head = p;
}
head1=head;
head -> printlist();
}

string node::search(int ID){
int n = 0;
node *p = head1;
while(p){
if (ID == n)
return name;
p = p -> next;
}
cout << "ID not found." << endl;
}
void node::setdata(int d, string name1, node *ptr){
name = name1;
ID = d;
next = ptr;
}



Output that I am getting:

Enter ID to search: 3
0
0
0
0
4 Grace
Last edited on
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
#include <iostream>
#include <string>
#include <iomanip>

struct list
{
    void push_front( int id, const std::string& name )
    {
        // create a new node; next of this new node is the current head
        node* new_node = new node { id, name, head } ;
        head = new_node ; // and make this new node the head
    }

    void print() const
    {
        for( node* n = head ; n != nullptr ; n = n->next ) // for each node in the list
            std::cout << n->id << ' ' << n->name << '\n' ; // print id and name
    }

    std::string find( int id ) const
    {
        for( node* n = head ; n != nullptr ; n = n->next ) // for each node in the list
            if( n->id == id ) return n->name ; // return the name if the id matches

        return {} ; // not found: return empty string
    }

    // TO DO: destructor, copy/move constructor; copy/move assignment

    private:
    
        struct node
        {
            int id ;
            std::string name ;
            node* next ;
        };

        node* head = nullptr ; // initialised to nullptr
};

int main()
{
     list lst ;
     lst.push_front( 123, "CXXIII" ) ;
     lst.push_front( 766, "DCCLXVI" ) ;
     lst.push_front( 119, "CXIX" ) ;
     lst.push_front( 994, "CMXCIV" ) ;

     lst.print() ;

     std::cout << std::quoted( lst.find(119) ) << '\n' // "CXIX"
               << std::quoted( lst.find(255) ) << '\n' ; // ""
}

http://coliru.stacked-crooked.com/a/7fc6d07351ca9294
Topic archived. No new replies allowed.