HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Linked List program

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
listlinkedprogram

Problem

This is a simple linked list program which creates a list by appending an object at the tail. It compiles and runs perfectly.

Is the coding style, logic etc are fine? How can I improve this program? Is there anything redundant or did I miss out some important things?

#include
#include
using namespace std;
class Link_list {
private:
    string name;
    Link_list *next_node;
public:
    void add_item(Link_list *);
    void add_item();
    friend void show(Link_list *sptr)
    {
        while(sptr) {
        cout name next_node;
        }
     }
};

void Link_list::add_item()
{
    cin >> name;
    next_node = NULL;
}
void Link_list::add_item(Link_list *pptr)
{
    cin >> name;
    next_node = NULL;
     pptr->next_node = this;
 }

int main()
{
    Link_list *str_ptr = NULL;
    Link_list *curr_ptr = str_ptr;
    Link_list *prev_ptr;
    char ch = 'y';

    str_ptr = new(Link_list);
    str_ptr->add_item();
    curr_ptr = str_ptr;
    do
    {
        prev_ptr = curr_ptr;
        curr_ptr = new(Link_list);
        curr_ptr->add_item(prev_ptr);
        cout > ch;
    }while(ch != 'n');
    show(str_ptr);
  }

Solution

You can't remove elements from it.

There is no search feature.

All you can do is add stuff to it and then have its contents streamed to STDOUT.

It can only hold strings.

Blocking for user-input in the List class itself seems odd; usually you'd request the input in the calling scope and then pass the resultant new string to your add member function.

Context

StackExchange Code Review Q#1099, answer score: 7

Revisions (0)

No revisions yet.