Templates For Java - Epaperpress

ADVERTISEMENT

Templates
Without Templates
////////////
// List.h //
////////////
struct Node {
Node *next;
int value;
};
class List {
public:
List();
~List();
void push_front(int x);
private:
Node *head;
};
//////////////
// List.cpp //
//////////////
List::List() {
head = NULL;
}
List::~List() {
Node *p = head;
while (p) {
Node *next = p->next;
delete p;
p = next;
}
}
void List::push_front(int x) {
Node *p = new Node;
p->value = x;
p->next = head;
head = p;
}
//////////////
// main.cpp //
//////////////
#include "List.h"
int main() {
List x;
x.push_front(5);
x.push_front(3);
return 0;
}

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 3