data structers

hi
this code it is for union two dynamic lists

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
66
67
68
69
#include <string>
#include <sstream>
#include <iostream>

using namespace 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;
            }


        }

        else
            break;
    }

    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;
        }

        else
            break;
    }

    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?

sorry for my bad english
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.

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
#ifndef NULL
#define NULL 0
#endif
template<typename T>
struct list_t
{
	//type
	struct node
	{
		//data
		T data;
		node *pred;
		node *succ;
		node() : pred(NULL), succ(NULL)
		{
		}
	};

	//data
	node *head;
	node *tail;
	//constructors/overload functions
	list_t() : head(NULL), tail(NULL) //constructor
	{
	}
	list_t(const list_t &source)
	{
		//fill this out
	}
	list_t & operator = (const list_t &source) //assignment operator
	{
		//fill this out
	}
	~list_t() //destructor
	{
		//fill this out
	}
	
	//methods
	void push_back(T const &source)
	{
		node *part= new node;
		part>pred= tail;
		((head)? tail->succ : head) = part;
		tail= part;
	}
	void push_front(T const &source)
	{
		node *part= new node;
		part->succ= head;
		((tail)? head->pred : tail) = part;
		head= part;
	}	
};
Last edited on
thank you very much Nexius!

plz add the main part to your code, cuz i didn't understand it very well

sorry for my bad english
Topic archived. No new replies allowed.