Skip to content

Instantly share code, notes, and snippets.

@MichaelStett
Last active October 2, 2019 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelStett/53f391cf42a7579fc999c4ef90173493 to your computer and use it in GitHub Desktop.
Save MichaelStett/53f391cf42a7579fc999c4ef90173493 to your computer and use it in GitHub Desktop.
List Two Way
#pragma once
#include <iostream>
#include <string>
using namespace std;
struct Data {
unsigned long ID;
string name;
string surname;
Data() {
ID = NULL;
name = surname = "";
};
unsigned int GetID() { return ID; }
string GetName() { return name; }
string GetSurname() { return surname; }
Data Enter() {
Data temp;
cout << "Enter ID:"; cin >> temp.ID;
cout << "Enter name:"; cin >> temp.name;
cout << "Enter surname:"; cin >> temp.surname;
return temp;
}
/*/
std::stringstream s; s << this->GetData(); return s.str();
*/
}; // Data
string PrintData(Data obj) {
string out;
out += obj.ID;
out += obj.name;
out += obj.surname;
return out;
}
#pragma once
#include <string>
#include "Node.h"
template<class T>
class List {
private:
Node<T>* head;
Node<T>* tail;
unsigned int length;
public:
List() {
head = tail = NULL;
length = 0;
};
Node<T>* GetHead() { return this->head; };
Node<T>* GetTail() { return this->tail; };
unsigned int GetLength() { return this->length; };
void PushFront(Node<T>* node) {
this->head == NULL ? InitializeEmptyHeadWith(node) : PushFrontWith(node);
}
void PushBack(Node<T>* node) {
this->head == NULL ? InitializeEmptyHeadWith(node) : PushBackWith(node);
}
//Pop First element in List
Node<T>* Pop() {
auto temp = this->head;
if (length == 1) {
this->head = NULL;
this->tail = NULL;
this->length = this->length - 1;
}
else if(length >= 1)
{
this->head = this->head->next;
this->head->previous = NULL;
this->length = this->length - 1;
return temp;
}
}
T ElementAt(int index) {
if (IsEmpty() || IndexOutOfBound(index)) return NULL;
return (index == 0 ? GetHead() : index == length - 1 ? GetTail() : FindElement(index))->GetData();
}
std::string ToString() {
auto temp = this->head;
std::string s = "";
while (temp) {
s += temp->ToString();
s += " -> ";
temp = temp->next;
}
return s;
}
private:
bool IndexOutOfBound(int index) { return index > length ? true : false; }
bool IsEmpty() { return length <= 0 ? true : false; }
Node<T>* FindElement(int index) {
if (index < this->length / 2)
{
auto temp = this->head;
int i = 0;
while (temp) {
if (i == index) { return temp; }
i++;
temp = temp->next;
}
}
else
{
auto temp = this->tail;
int i = this->length - 1;
while (temp) {
if (i == index) { return temp; }
i--;
temp = temp->previous;
}
}
return NULL;
}
void InitializeEmptyHeadWith(Node<T>* node) {
this->head = node;
this->tail = this->head;
this->length = this->length + 1;
}
void PushFrontWith(Node<T>* node) {
this->head->previous = node;
node->next = this->head;
this->head = node;
this->length = this->length + 1;
}
void PushBackWith(Node<T>* node) {
this->tail->next = node;
node->previous = this->tail;
this->tail = node;
this->length = this->length + 1;
}
}; // List
#pragma once
#include <iostream>
#include <string>
#include "List.h"
using namespace std;
int main() {
List<Data> lista;
int c = NULL;
while (true)
{
cout << "================" << endl;
cout << " 1. Get Length" << endl;
cout << " 2. Push Front" << endl;
cout << " 3. Push Back" << endl;
cout << " 4. Pop" << endl;
cout << " 5. Print List" << endl;
cout << "================" << endl;
cout << "Choice:";
cin >> c;
switch (c)
{
case 1:
cout << "Length: " << lista.GetLength();
break;
case 2:
cout << "Enter value:";
lista.PushFront(new Node<Data>(Data().Enter()));
break;
case 3:
cout << "Enter value:";
lista.PushBack(new Node<Data>(Data().Enter()));
break;
case 4:
lista.Pop();
break;
case 5:
cout << lista.ToString();
break;
}
cout << endl << endl;
}
}
#pragma once
#include <string>
#include <sstream>
#include "Data.h"
template<class T>
class Node {
private:
T data;
public:
Node* next;
Node* previous;
string(*test_ptr)(T) = PrintData;
~Node() {};
Node(T data) {
next = previous = NULL;
this->data = data;
}
std::string ToString() { return PrintData(data); }
private:
T GetData() { return this->data; }
Node();
Node(const Node<T>&);
}; // Node
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment