Print Template data types
want to perform
1 2
|
cout << current->info;
//current is pointer to the Node
|
This line is in function display list.
Whenever I tried to compile, my compiler got stuck :-(
that's why I don't know the errors
How to print list???
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
// Singly_Linear_LinkedList_2
// with sentinal Nodes<Object> and structure Node<Object> is outside class
#include <iostream>
using namespace std;
struct Polynomial
{
int coffecient;
int exponent;
Polynomial(int c=0, int e=0): coffecient(c), exponent(e)
{ }
};
template <class Object>
struct Node
{
Object info;
Node *next;
Node() : info(0,0), next(NULL)
{ }
};
template <class Object>
class Linked_List
{
private:
Node<Object> *head;
Node<Object> *tail;
int no_of_rec;
public:
Linked_List(): head(NULL), tail(NULL), no_of_rec(0)
{ }
Node<Object>* create_node()
{
no_of_rec++;
return new Node<Object>;
}
void free_node(Node<Object> *p)
{
p->next = NULL;
no_of_rec--;
delete p;
}
void insert_at_head(Object data)
{
Node<Object> *p;
p = create_node();
p->info = data;
p->next = head;
if(head == NULL)
tail = p;
head = p;
}
void destroy_list()
{
Node<Object> *temp = head;
while(head != NULL)
{
temp = head;
head = head->next;
free_node(temp);
}
}
void display_list()
{
cout << endl << "Contents of the List are: " << endl;
Node<Object>* current= head;
while(current != NULL)
{
cout << current->info << endl;
current = current->next;
}
}
int size()
{ return no_of_rec; }
~Linked_List()
{ destroy_list(); }
};
int main()
{
Linked_List<Polynomial> A;
Polynomial term3(1,2);
Polynomial term2(2,1);
Polynomial term1(3,0);
A.insert_at_head(term1);
A.insert_at_head(term2);
A.insert_at_head(term3);
A.display_list();
return 0;
}
|
You need to write operator<< for Polynomial.
Thanks
Topic archived. No new replies allowed.