#include <string>
#include <sstream>
#include <iostream>
usingnamespace std;
struct list
{
int data;
list *next;
};
int main()
{
list *head = NULL, *tail = NULL, *node;
string input;
int num;
cout << "\nAdding values to the first dynamic list :\n";
while(true)
{
cout << ">>";
cin >> input;
if(istringstream(input) >> num)
{
node = new list;
node->data = num;
if(head == NULL)
{
head = node;
tail = node;
}
else
{
tail->next = node;
tail = node;
}
}
elsebreak;
}
cout << "Adding values to the second dynamic list :\n";
while(true)
{
cout << ">>";
cin >> input;
if(istringstream(input) >> num)
{
node = new list;
node->data = num;
tail->next =node;
tail = node;
}
elsebreak;
}
return 0;
}
I have studied this method in the university, but I feel that this method is not good for programmers Is there any other way to write such a program by better way?
No, it's terrible design. The university should have taught encapsulation and Object Oriented design.
Here's some simple code to get you started with an abstract container.