Skip to content

Instantly share code, notes, and snippets.

@lynnlangit
Created March 29, 2019 18:21
Show Gist options
  • Save lynnlangit/bd0a5e4d2ff9a3c93908df00eedbf2c7 to your computer and use it in GitHub Desktop.
Save lynnlangit/bd0a5e4d2ff9a3c93908df00eedbf2c7 to your computer and use it in GitHub Desktop.
Linked List
#include <iostream>
#include <cstdlib>
class Node
{
public:
Node* next;
int data;
};
using namespace std;
class LinkedList
{
public:
int length;
Node* head;
LinkedList();
~LinkedList();
void add(int data);
void print();
};
LinkedList::LinkedList(){
this->length = 0;
this->head = NULL;
}
LinkedList::~LinkedList(){
std::cout << "LIST DELETED";
}
void LinkedList::add(int data){
Node* node = new Node();
node->data = data;
node->next = this->head;
this->head = node;
this->length++;
}
void LinkedList::print(){
Node* head = this->head;
int i = 1;
while(head){
std::cout << i << ": " << head->data << std::endl;
head = head->next;
i++;
}
}
int main(int argc, char const *argv[])
{
LinkedList* list = new LinkedList();
for (int i = 0; i < 100; ++i)
{
list->add(rand() % 100);
}
list->print();
std::cout << "List Length: " << list->length << std::endl;
delete list;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment