c++ LinkedList when calling display function - c++

I"m trying to understand link list but every example I've tried to copy gives me a segmentation fault. This is my example program that I am working on.
in contacts.h
struct people{
string last;
string first;
int age;
people *next;
};
class Contacts {
private:
people *head;
public:
Contacts() {
head=NULL;
};
void add(string, string, int);
void display();
};
in main.cpp
//menu items
//cin >> option
//if option == 1, addContact
//if option == 8, displayContact
//basic in and out here no need to show code
in main.h
void addContact() {
Contacts *ptr;
int i, loop=0, a=0;
string l, f;
cout << "number of contacts " << endl;
cin >> loop;
for(i=0; i<loop; i++) {
cout << "enter last name ";
cin >> l;
cout << "enter first name ";
cin >> f;
cout << "enter age ";
cin >> a;
}
ptr->add(l,f,a);
}
void display() {
Contacts *ptr;
ptr->display();
}
in contacts.cpp
void Contacts::add(string l, string f, int a) {
people *node = new people;
node->last=l;
node->first=f;
node->age=a;
node->next=NULL;
}
void Contacts::display() {
people *tmp = head;
while(tmp!=NULL) {
cout << tmp->last << endl;
cout << tmp->first << endl;
cout << tmp->age << endl;
tmp->next;
}
the add function works
then display() gives me a segfault

Member function add should be defined the following way
void Contacts::add( const string &l, const string &f, int a )
{
head = new people { l, f, a, head };
}
Or if your compiler does not support initializer lists with the new operator then
void Contacts::add( const string &l, const string &f, int a )
{
people *p = new people;
p->last = l;
p->first = f;
p->age = a;
p->next = head;
head = p;
}
Member fuunction display should be declared as
void Contacts::display() const;
and defined as
void Contacts::display() const
{
for ( people *tmp = head; tmp; tmp = tmp->next )
{
cout << tmp->last << endl;
cout << tmp->first << endl;
cout << tmp->age << endl;
cout << endl;
}
}
The other two functions should be defined the following way
void addContact( Contacts &contacts )
{
int n = 0;
cout << "number of contacts " << endl;
cin >> n;
for ( int i = 0; i < n; i++ )
{
string l, f;
int a = 0;
cout << "enter last name ";
cin >> l;
cout << "enter first name ";
cin >> f;
cout << "enter age ";
cin >> a;
contacts.add( l, f, a );
}
}
void display( const Contacts &contacts )
{
contacts.display();
}
And in main you should define an object of type
Contacts contacts;
and use it as the argument of the functions above.

Related

I keep getting an error when I try to pass the head pointer of the linked list

I keep getting an error saying undefined symbol insert(*Friend). Can anyone identif and explain the error please? I know it refers to the fact that it cannot find it but I can't identify what is wrong please helppp. I am working on a program that keeps a database of friends' information using a linked-list. Therefore, I am trying to initialize the head node to null once the program starts and check for the first insert if this head is null, if that's the case assign the pointer to the new node.
#include <iostream>
using namespace std;
// MARK: Friend Struct
struct Friend {
string last;
string first;
int yob;
int mob;
int dob;
string sex;
struct Friend* next;
};
Friend* head = NULL;
void insert(struct Friend* head);
void getData();
// MARK: Main Method
int main(int argc, const char* argv[])
{
insert(head);
return 0;
}
// MARK: Get Data Method
void getData(string& f, string& l, string& s, int& y, int& m, int& d)
{
cout << "Please type friend's first name: " << endl;
cin >> f;
cout << "Please type friend's last name: " << endl;
cin >> l;
cout << "Please type friend's year of birth (yyyy): " << endl;
cin >> y;
cout << "Please type friend's month of birth (mm): " << endl;
cin >> m;
cout << "Please type friend's day of birth (dd): " << endl;
cin >> d;
cout << "Please type friend's sex (f/m): " << endl;
cin >> s;
}
// MARK: Insert new Friend Method
void insert(struct Friend** head)
{
string f, l, s;
int y = 0, m = 0, d = 0;
getData(f, l, s, y, m, d);
Friend* tmp = new Friend;
tmp->first = f;
tmp->last = l;
tmp->yob = y;
tmp->mob = m;
tmp->dob = d;
tmp->sex = s;
tmp->next = NULL;
if (*head == NULL) {
*head = tmp;
}
};
You declared void insert(struct Friend* head); (there is only one star), but only defined void insert(struct Friend** head) (there are two stars). They are different functions.
It seems the declaration should be corrected to two stars (match to the definition) and &head should be passed instead of head on calling that.
You passing (Friend*)head in insert(Friend **head) which is not correct.
Change the call to insert(&head);.

Creating a vector in class then using class object in function not working

I have a class Employees. I'm trying to make the user insert and delete an employee but it's not working. The size of the vectors should be 500.
class Employees{
public:
int maxx = 500;
vector<string> Surname;
vector<string> FirstName;
vector<string> birthdate;
int vacation[500];
public:
Employees() : Surname(500) {}
};
This is the function that inserts, but printing elements of the vectors is not working at all:
void Process(Employees ZZ){
string dateyear;
string datemonth;
string dateday;
int dateyear1;
int datemonth1;
int dateday1;
int Realage;
int Vacationi = 0;
for(int i = 0; i < 500; i++) {
string s;
cin >> s;
string d;
cin >> d;
string c;
cin >> c;
ZZ.Surname.push_back(s);
ZZ.FirstName.push_back(d);
ZZ.birthdate.push_back(c);
cout << endl << ZZ.Surname[1] << endl;
}
Now the delete function, if I input a string then search for it in the vector then get his index then delete, but the vector doesn't update any values.
void DeleteEmployee(Employees ZZ){
cout<< endl << ZZ.Surname[1] << endl ;
for (int i = 0; i < ZZ.Surname.size(); i++){
cout << ZZ.Surname[i] ;
}
cout << " delete employee";
string delete1;
cin >> delete1;
auto it = std::find(ZZ.Surname.begin(), ZZ.Surname.end(), delete1);
if (it == ZZ.Surname.end())
{
cout<< " name not in vector " << endl;
}
else
{
//auto index = distance(Names.begin(), find(Names.begin(), Names.end(), old_name_)));
//ZZ.Surname.erase(ZZ.Surname.begin()+index) ;
}
}
This is the main function, also the values of the vector are not printing:
int main()
{
Employees ZZ;
Process(ZZ);
DeleteEmployee(ZZ);
cout << "fyccck";
for (int i = 0; i < ZZ.Surname.size(); i++){
cout << ZZ.Surname[i] ;
}
}
There are a lot of things wrong with this code. But the particular issue you are asking about is caused by your functions passing the Employees object by value, so a copy is made, and any changes you make to the copy are not reflected in the original object in main().
You need to change the parameters to pass the Employees object by reference instead:
void Process(Employees &ZZ)
void DeleteEmployee(Employees &ZZ)
That being said, the whole design of the code is not good in general. The vectors are not being kept in sync properly, and for that matter you are using more vectors then you actually need, 1 single vector will suffice. And Process() and DeleteEmployee() should be members of the Employees class, not separate functions. And they are both accessing out-of-bounds of the Surname vector.
I would suggest completely rewriting the code from scratch, for instance something more like this:
struct Employee{
string Surname;
string FirstName;
string BirthDate;
int Vacation;
string DisplayName() const { return Surname + ", " + FirstName; }
};
class Employees{
public:
static const int maxx = 500;
vector<Employee> employees;
Employees() { employees.reserve(maxx); }
bool Add(const Employee &e);
bool Delete(string Surname, string FirstName);
};
bool Employees::Add(const Employee &e) {
if (employees.size() < maxx) {
employees.push_back(e);
return true;
}
return false;
}
bool Employees::Delete(string Surname, string FirstName) {
auto it = std::find_if(employees.begin(), employees.end(),
[&](const Employee &e){
return e.Surname == Surname && e.FirstName == FirstName;
}
);
if (it != employees.end()) {
employees.erase(it);
return true;
}
return false;
}
int main()
{
Employees ZZ;
for(int i = 0; i < Employees::maxx; ++i) {
Employee e;
cin >> e.Surname;
cin >> e.FirstName;
cin >> e.BirthDate;
e.Vacation = 0;//cin >> e.Vacation;
ZZ.Add(e);
cout << endl << e.DisplayName() << endl;
}
cout << " delete employee";
string Surname, FirstName;
if (cin >> Surname >> FirstName) {
if (ZZ.Delete(Surname, FirstName)) {
cout << " name deleted from vector " << endl;
} else {
cout << " name not in vector " << endl;
}
}
cout << "fyccck";
for (auto &e : ZZ.employees) {
cout << e.DisplayName() << endl;
}
return 0;
}

Visa Transaction simulation program in C++

I am doing a data structure project and it is important to write my own template linked list class implementation ,it is required that:
Each visa account can do a transaction using the visa card and the transaction is recorded in his account with date and amount of money due.
My steps :
make a list of accounts in the system.
each account has it's list of cards, as it is required issuing another visa card with different number but to the same account.
make for each visa a list of transactions.
My problem:
that the transactions made by the card not saved in the list of transactions that is defined in the card class.. you can check the function settrans to see how I implemented it . how can I fix that ?
2.The balance "The sum of all transactions" is not updated, which I need it later to let the user pay.
NOTE : The main is not completed and there is another data I need to do another functions not completed yet so no problem if you found excess paramters.
and I didn't put pre or post conditions yet.
the header file with all functions and classes :
#include <iostream>
#include <string>
using namespace std;
template<class T> class list;
class account;
class card;
class transaction;
//Node class
template<class T> class node
{
public:
node() : data(0), next(NULL){}
T getdata() { return data; }
node * getnext(){ return next; }
void setnext(node * n) { next = n; }
node(T v) : data(v), next(NULL) {}
friend list < T >;
private:
T data;
node * next;
};
template<class T> class list
{
node<T> * head;
public:
list() : head(NULL){}
void add(T data)
{
node <T> *temp = new node<T>(data);
temp->next = head;
head = temp;
}
void printaccounts();
void printcards();
void printtransactions();
bool checkaccount(string n, string p);
account getaccount(string n, string p);
bool checkcard(int i, string p);
card getcard(int i, string p);
};
//Transaction Class
class transaction
{
public:
void pushtrans(int ch, int a, int cat, int m, int d, string co);
void newtrans();
int getcharge() { return charge; }
int getage(){ return age; }
string getcompany(){ return company; }
int getcategory(){ return category; }
private:
int charge;
string company;
int month;
int day;
int category;
int age;
};
void transaction::pushtrans(int ch, int a, int cat, int m, int d, string co)
{
charge = ch;
age = a;
category = cat;
month = m;
day = d;
company = co;
}
//Card Class
class card
{
public:
card();
void pushcard(string p, int m, int d, int x);
int getid(){ return id; }
int getm(){ return month; }
int getd(){ return day; }
int getb(){ return balance; }
void settrans();
string getpassword(){ return password;}
transaction gettransobj(){ return t; }
list<transaction> getlist(){ return tl; }
private:
int id =0;
string password;
int balance =0;
int month;
int day;
list<transaction> tl;
transaction t;
};
card::card()
{
password = "";
balance = 0;
month = 0;
day = 0;
}
void card::pushcard(string p, int m, int d, int x)
{
password = p;
month = m;
day = d;
id = x;
}
//Account Class
class account
{
public:
void pushaccount(string n, string p);
string getname(){ return name; }
string getaccpassword() { return passwrod; }
card getcard(){ return c; }
list<card> getclist() { return cl; }
void setcard();
private:
string name;
string passwrod;
card c;
list<card> cl;
};
void account::pushaccount(string n, string p)
{
name = n;
passwrod = p;
}
void account::setcard()
{
string p; int m, d,x;
cout << "\n _Please enter your Password: ";
cin >> p;
cout << "\n _Please enter the Month: ";
cin >> m;
cout << "\n _Please enter your Date: ";
cin >> d;
x = c.getid() + 1;
cout << "Your ID is : " << x;
c.pushcard(p, m, d, x);
cl.add(c);
}
template<class T>
void list<T>:: printaccounts()
{
cout << "The Accounts: \n";
node <T> *temp = head;
while (temp != nullptr)
{
cout << temp->data.getname() << endl;
temp = temp->next;
}
}
template<class T>
bool list<T>::checkaccount(string n, string p)
{
bool c = false;
node <T> * temp = head;
while (temp != nullptr)
{
if (temp->data.getname() == n && temp->data.getaccpassword() == p)
{
c = true;
return c;
}
temp = temp->next;
}
return c;
}
template<class T>
account list<T>::getaccount(string n, string p)
{
node <T> * temp = head;
account a;
while (temp != nullptr)
{
if (temp->data.getname() == n && temp->data.getaccpassword() == p)
{
a = temp->getdata();
return a;
}
temp = temp->next;
}
return a;
}
template<class T>
void list<T>::printcards()
{
cout << "\nThe Cards: \n";
node <T> *temp = head;
while (temp != nullptr)
{
cout << temp->data.getid() << endl;
temp = temp->next;
}
}
template<class T>
bool list<T>::checkcard(int i, string p)
{
bool c = false;
node <T> * temp = head;
while (temp != nullptr)
{
if (temp->data.getid() == i && temp->data.getpassword() == p)
{
c = true;
return c;
}
temp = temp->next;
}
return c;
}
template<class T>
card list<T>::getcard(int i, string p)
{
node <T> * temp = head;
card c;
while (temp != nullptr)
{
if (temp->data.getid() == i && temp->data.getpassword() == p)
{
c = temp->getdata();
return c;
}
temp = temp->next;
}
return c;
}
void card::settrans()
{
string s; int m, d,a,c,p;
cout << "\n _Please enter charge: ";
cin >> p;
cout << "\n _Please enter the Month: ";
cin >> m;
cout << "\n _Please enter your Date: ";
cin >> d;
cout << "\n _Choose the Category: \n";
cout << "|--------------|" << endl;
cout << "| 1.Food |" << endl;
cout << "| 2.Clothes |" << endl;
cout << "| 3.Medicine |" << endl;
cout << "|--------------|" << endl;
cin >> c;
cout << "\n _Please enter the Company/Shop: ";
cin >> s;
cout << "\n _Please enter your age: ";
cin >> a;
balance += p;
cout << "Balance = " << balance;
t.pushtrans(p, a, c, m, d, s);
tl.add(t);
}
template<class T>
void list<T>::printtransactions()
{
int i = 1;
cout << "\nThe Transactions: \n";
node <T> *temp = head;
while (temp != nullptr)
{
cout << i << "." << temp->data.getcompany() << "\tCHARGE: " << temp->data.getcharge() << endl;
temp = temp->next;
i++;
}
}
The main :
#include <iostream>
#include <string>
#include "Header1.h"
using namespace std;
void main()
{
list<account> A;
account acc;
//int x;
while (true)
{
int choice, choice2, i , m, d;
string n, p;
cout << "*** Welcome to ASU Bank ***" << endl;
cout << "|-------------------------|" << endl;
cout << "| 1. Get a New Account |" << endl;
cout << "| 2. Open My Account |" << endl;
cout << "| 3. Statistics |" << endl;
cout << "|-------------------------|" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << "Please enter your Name: ";
cin >> n;
cout << "\n Please enter your Password: ";
cin >> p;
acc.pushaccount(n, p);
A.add(acc);
A.printaccounts();
break;
case 2:
cout << "-Please enter your Name: ";
cin >> n;
cout << "\n -Please enter your Password: ";
cin >> p;
if (A.checkaccount(n, p))
{
acc = A.getaccount(n, p);
list<card> C;
list<transaction> T;
card c;
transaction t;
while (true)
{
cout << "\n|-------------------------|" << endl;
cout << "| 1. Get a New Card |" << endl;
cout << "| 2. Make Transaction |" << endl;
cout << "| 3. Print Transactions |" << endl;
cout << "| 4. Pay |" << endl;
cout << "| 5. Companies Record |" << endl;
cout << "| 6. Get Balance |" << endl;
cout << "| 7. Main Menu |" << endl;
cout << "|-------------------------|" << endl;
cin >> choice2;
switch (choice2)
{
case 1:
//ba2a ye3ml cards with different ids and push them in the list YEAY :D
acc.setcard();
C = acc.getclist();
C.printcards();
break;
case 2:
cout << "-Please enter your card ID: ";
cin >> i;
cout << "\n -Please enter your card Password: ";
cin >> p;
if (C.checkcard(i, p))
{
//bey3ml add fy list gedida w mesh bey7seb balance..same error
c = C.getcard(i, p);
c.settrans();
c.getlist().printtransactions();
}
else cout << "Invalid Entery !\n";
break;
case 3:
cout << "-Please enter your card ID: ";
cin >> i;
cout << "\n -Please enter your card Password: ";
cin >> p;
if (acc.getclist().checkcard(i, p))
{
c = acc.getclist().getcard(i, p);
c.getlist().printtransactions();
}
else cout << "Invalid Entery !\n";
break;
case 4:
default:
break;
}
}
}
else
cout << "The Account doesn't exist please check your info.\n";
break;
default:
break;
}
}
}
Edited part:
1. changed all the data in node class to pointer to data
2.
card * list<T>::getcard(int i, string p)
{
node <T> * temp = head;
card* c;
while (temp != nullptr)
{
if (temp->data.getid() == i && temp->data.getpassword() == p)
{
c = temp;
return &c;
}
temp = temp->next;
}
return &c;
}
solved part is that there is a transaction list with different transactions saved in it but the account list and card list have the problem:
if I add john to the accounts and then joe
it will have two accounts with the name joe.
and same problem with cards list

I have trouble with some pointers, access location failed at the end of debugging

// header.h
#include <iostream>
#include <list>
#include <fstream>
using namespace std;
class Biblioteca
{
public:
Biblioteca();
void closeFile();
bool chose();
ofstream Intrare;
ofstream Memorare;
};
class Publicatii:public virtual Biblioteca
{
public:
string retTitlu();
string retEditura();
string retAutor();
int retAn();
int retTiraj();
int retNumar();
int retFrecventa_de_aparitii();
protected:
string Titlu, Editura, Autor;
int An, Tiraj, Numar, Frecventa_de_aparitii;
};
class Carti: public Publicatii , public virtual Biblioteca
{
public:
void readBook();
Carti();
void insertMyBook();
void writeBookFile();
void inTitlu(Carti& a);
void inEditura(Carti& a);
void inAutor(Carti& a);
void inTiraj(Carti& a);
void inAn(Carti& a);
protected:
list<Carti*>books;
list<Carti*>::iterator i;
};
class Reviste: public Publicatii , public virtual Biblioteca
{
public:
void readMagazine();
Reviste();
void insertMyMagazine();
void writeMagazineFile();
protected:
list<Reviste*> magazine;
list<Reviste*>::iterator j;
};
The other cpp file of the header
#include<iostream>
#include<string>
#include"biblioteca.h"
#include <list>
#include<fstream>
//-----Biblioteca------
Biblioteca::Biblioteca()
{
Memorare.open("in.txt");
}
void Biblioteca::closeFile()
{
Memorare.close();
}
bool Biblioteca::chose()
{
int k;
cout << "Ce doriti sa introduceti?" << endl << endl;
cout << "1--Carte" << endl;
cout << "2--Biblioteca" << endl;
cin >> k;
switch (k)
{
case 1:
return true;
break;
case 2:
return false;
break;
}
}
//-------Publicatii------
string Publicatii::retTitlu()
{
return Titlu;
}
string Publicatii::retEditura()
{
return Editura;
}
string Publicatii::retAutor()
{
return Autor;
}
int Publicatii::retAn()
{
return An;
}
int Publicatii::retTiraj()
{
return Tiraj;
}
int Publicatii::retNumar()
{
return Numar;
}
int Publicatii::retFrecventa_de_aparitii()
{
return Frecventa_de_aparitii;
}
//---------Carti---------
void Carti::inTitlu(Carti& a)
{
Titlu = a.retTitlu();
}
void Carti::inEditura(Carti& a)
{
Editura = a.retEditura();
}
void Carti::inAutor(Carti& a)
{
Autor = a.retAutor();
}
void Carti::inTiraj(Carti& a)
{
Tiraj = a.retTiraj();
}
void Carti::inAn(Carti& a)
{
An = a.retAn();
}
void Carti::readBook()
{
cout << "\nO noua carte" << endl<<endl;
cout << "\nTitlu= ";
cin >> Titlu;
cout << "\nEditura= ";
cin >> Editura;
cout << "\nAn= ";
cin >> An;
cout << "\nTiraj= ";
cin >> Tiraj;
cout << "\nAutor= ";
cin >> Autor;
}
Carti::Carti()
{
books.resize(1);//one book
}
void Carti::insertMyBook()
{
Carti carti;
for (i = books.begin(); i != books.end(); i++)
{
carti.readBook();
(*i)->inTitlu(carti);
(*i)->inEditura(carti);
(*i)->inAn(carti);
(*i)->inTiraj(carti);
(*i)->inAutor(carti);
//books.insert(i, *i);
}
}
void Carti::writeBookFile()
{
Memorare << "---Carti---" << endl;
for (i = books.begin(); i != books.end(); i++)
Memorare << *i << " ";
}
//-------Reviste--------
void Reviste::readMagazine()
{
cout << "\nO noua revista" << endl<< endl;
cout << "\nTitlu= ";
cin >> Titlu;
cout << "\nEditura= ";
cin >> Editura;
cout << "\nAn= ";
cin >> An;
cout << "\nTiraj= ";
cin >> Tiraj;
cout << "\nNumar= ";
cin >> Numar;
cout << "\nFrecventa de aparitie= ";
cin >> Frecventa_de_aparitii;
}
Reviste::Reviste()
{
magazine.resize(1);//one magazine
}
void Reviste::insertMyMagazine()
{
Reviste reviste;
for (j = magazine.begin(); j != magazine.end(); j++)
{
reviste.readMagazine();
//some conde
magazine.insert(j, *j);
}
}
void Reviste::writeMagazineFile()
{
Memorare << "---Reviste---" << endl;
for (j = magazine.begin(); j != magazine.end(); j++)
cout << *j << " ";
}
Sorry for the code thone here, I'm new to Stackoverflow and I'm in a hurry, that's why I don't write "beautiful code". My problem is, when I want to just insert elements in my list
void Carti::insertMyBook()
{
Carti carti;
for (i = books.begin(); i != books.end(); i++)
{
carti.readBook();
(*i)->inTitlu(carti);
(*i)->inEditura(carti);
(*i)->inAn(carti);
(*i)->inTiraj(carti);
(*i)->inAutor(carti);
books.insert(i, *i);
}
}
it's working like a clockwork and after I compile I type some information from my keyboard and at the end I get a big error like "Acces Violation Reading 00000001C"
Why? I tried other metods like allocating dynamic memory with the new operator, I tried a lot of things but in the end I have like this error or type "example" doesn't match with "example".
Sorry for my bad English spelling, but in this program I just wanted to make a program that reads magazines and boooks and stored to a library named "biblioteca", and "carte" means books and " Reviste" means magazine... and I want it to be memorized in a list because I need to insert elements or delete what ever book or magazine I choose...and all the information I want to be saved in a file for instance "out.txt" or "in.txt"
The crash is because the iterator is NULL at line (*i)->inTitlu(carti);.
The issue is in method:
Carti::Carti()
{
books.resize(1);//one book
}
and books are:
list<Carti*>books;
What you are trying to is to resize the list of Carti's to 1 but as you have a list of pointers to Carti objects rather than Carti objects, resizing operation will not create a Carti object by calling it's constructor but a pointer.
Apart from that you have major issues with your design and coding, a Carti object storing a list of other pointers-to-Carti objects definitely is not a good idea. You may consider creating another 'holding' class to store a list of Carti's you have created.

Printing out linked list elements

I wrote a program that adds elements recursively, and then prints out the elements. The problem is, that the program prints out only the first element in the list. I tried to solve this, but I don't know where is the problem...
#include <iostream>
using namespace std;
struct list
{
int value;
list* next;
};
list* addNewElement(list* p_head, int elems)
{
if (elems >= 1)
{
list* p_list = new list;
cout << "Enter a value: ";
cin >> p_list->value;
p_list->next = p_head;
addNewElement(p_head, elems - 1);
return p_list;
}
}
void printList(list* p_head)
{
list* p_cur = p_head;
cout << "ELEMENTS: " << endl;
while (p_cur != NULL)
{
cout << p_cur->value;
p_cur = p_cur->next;
}
cout << endl;
}
int main()
{
list* p_head = NULL;
int elemNR;
cout << "Enter how many elements do you want in the list: ";
cin >> elemNR;
p_head = addNewElement(p_head, elemNR);
cout << endl;
printList(p_head);
cout << endl;
cout << "PRESS <ENTER> TO CONTINUE...";
cin.ignore();
cin.get();
}
The problem is that after all iterations You have a lot of list objects in which next pointer points to NULL. You should modify your addNewElement method to something like this:
list* addNewElement(list* p_head, int elems) {
if (elems >= 1) {
list* p_list = new list;
cout << "Enter a value: ";
cin >> p_list->value;
p_list->next = addNewElement(p_head, elems - 1);
return p_list;
}
return p_head;
}
What had changed? p_list->next pointer is being set to the beginning of next list's element instead of NULL ;)
EDIT: Here is working code: http://ideone.com/oJ8kX7