Not too sure how to move on from here. Using linked list to search?

Question: Create a Linked list of nodes. Each node should have student names,age,course num,grades and pointer that is used to link the next node in the list. Use appendnode member function to generate linked list of 5 students. Create a search member function to display the name of student who has got 'A' grade . Assume that only one student got "A" grade.


Not too sure how to move on from here. I am new to linked list/nodes and not too sure what I am doing wrong. Any help would be greatly appreciated.

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

struct node
{
  string name;
  int age;
  int coursenum;
  char grade;
  node *next;
};

node *search (node * head)
{
  node *temp = head;
  while (temp != NULL)
    {
     // cout << "grade=" << temp->grade << endl;
      if (temp->grade == 'A')
	{
	  cout << temp->name;
	  return temp;
	}
      temp = temp->next;
    }
}

int main ()
{
  node *head = NULL;

  string name;
  int age;
  int coursenum;
  char grade;

  int i = 0;

  while (i < 2)
    {
      cout << "Enter the students name: ";
      cin >> name;
      cout << "Enter the studens age: ";
      cin >> age;
      cout << "Enter the students course number: ";
      cin >> coursenum;
      cout << "Enter the students grade: ";
      cin >> grade;
      node *temp = new node;
      temp->grade = grade;
      temp->next = head;
      head = temp;
      i++;
    }
  search (head);
}

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
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string> // <cstring>
// using namespace std;

struct list
{
    // Each node should have student names,age,course num,grades and pointer that is used
    // to link the next node in the list. (not the greatest idea; why not have a student class?)
    struct node
    {
      std::string name ;
      int age ;
      int coursenum ;
      char grade ;
      node* next = nullptr ; // default member initializer
                             // http://www.stroustrup.com/C++11FAQ.html#member-init
    };

    node* first = nullptr ;
    node* last = nullptr ;

    // Use appendnode member function ...
    void append_node( std::string name, int age, int coursenum, char grade )
    {
        // if empty add the only node which becomes both the first and last node
        if( last == nullptr ) last = first = new node{ name, age, coursenum, grade } ;

        else
        {
            // otherwise add the new node after he current last node
            last->next = new node{ name, age, coursenum, grade } ;
            last = last->next ; // and bump last to be the newly added node
        }
    }

    //  member function to return the name of the first student who has got 'A' grade
    std::string find_name_with_grade( char what ) const
    {
        for( node* n = first ; n != nullptr ; n = n->next )
            if( n->grade == what ) return n->name ;

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

    // Create a search member function to display the name of student who has got 'A' grade .
    // Assume that only one student got "A" grade ('search' would be a bad name for this function)
    void print_name_with_grade( char what = 'A' ) const
    {
        std::cout << find_name_with_grade(what) << '\n' ;
    }
};

int main ()
{
    list lst ; // Create a Linked list of nodes.

    // Use appendnode member function to generate linked list of 5 students.
    lst.append_node( "bjarne stroustrup", 21, 101, 'D' ) ;
    lst.append_node( "andrew koenig", 92, 102, 'A' ) ;
    lst.append_node( "stephn lavavej", 37, 101, 'C' ) ;
    lst.append_node( "howard hinnant", 59, 104, 'B' ) ;
    lst.append_node( "anthony williams", 59, 104, 'F' ) ;

    lst.print_name_with_grade('A') ;
}
Topic archived. No new replies allowed.