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<iostream>
#include<string>
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 << sptr->name << endl;
sptr = sptr->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 <<"Do you want to add the item" << endl;
cin >> ch;
}while(ch != 'n');
show(str_ptr);
}
std::list
? – AJG85 Mar 2 '11 at 19:53