Skip to content

Instantly share code, notes, and snippets.

@ramblingenzyme
Last active October 23, 2015 11:22
Show Gist options
  • Save ramblingenzyme/ddc0fa95697668a08d14 to your computer and use it in GitHub Desktop.
Save ramblingenzyme/ddc0fa95697668a08d14 to your computer and use it in GitHub Desktop.
linked_list
#include <iostream>
#include "list.h"
using namespace std;
List::List() {
head_ = NULL;
}
List::~List() {
Node *del_node = head_;
while (del_node != NULL) {
head_ = head_->next();
delete del_node;
del_node = head_;
}
}
void List::print_list(std::ostream&) {
Node *print_node = head_;
cout << head_ << '\t' << &head_ << endl;
while (print_node != NULL) {
cout << print_node->tag() << '\t'
<< print_node->cost() << '\t'
<< print_node->volume() << endl;
print_node = print_node->next();
}
}
void List::add_new(const char *temptag, const int tempcost, const long tempvol) {
Node *new_node = new Node;
new_node->set_data(temptag, tempcost, tempvol);
new_node->set_next(head_);
head_ = new_node;
}
bool List::is_empty() {
return head_ == NULL;
}
#include <fstream>
#include "node.h"
#ifndef LIST_H
#define LIST_H
class List {
public:
List();
~List();
void print_list(std::ostream&);
void add_new(const char*, const int, const long);
bool is_empty();
private:
Node *head_;
};
#endif
#include <iostream>
#include "node.h"
#include "list.h"
using namespace std;
int main() {
List test;
test.add_new("ASDF", 5, 500);
test.add_new("GHJK", 8, 500);
test.add_new("QWER", 20, 500);
test.print_list(cout);
return 0;
}
#include <iostream>
#include <cstring>
#include "node.h"
Node::Node() {
data_.tag[0] = '\0';
data_.cost = 0;
data_.volume = 0;
next_ = NULL;
}
Node::~Node() { }
void Node::set_data(const char* tag, const int cost, const long vol) {
strcpy(data_.tag, tag);
data_.cost = cost;
data_.volume = vol;
}
void Node::set_next(Node* nextlocation) {
next_ = nextlocation;
}
const char* Node::tag() {
return data_.tag;
}
const int Node::cost() {
return data_.cost;
}
const long Node::volume() {
return data_.volume;
}
Data* Node::data() {
return &data_;
}
Node* Node::next() {
return next_;
}
#ifndef NODE_H
#define NODE_H
struct Data {
char tag[5];
int cost;
long volume;
};
class Node {
public:
Node();
~Node();
void set_data(const char*, const int, const long);
void set_next(Node*);
const char* tag();
const int cost();
const long volume();
Data* data();
Node* next();
private:
Data data_;
Node *next_;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment