Linked List and dictionary snippets

Post your C++ code snippets in here.
1 post Page 1 of 1
Contributors
User avatar
reececlarke
VIP - Donator
VIP - Donator
Posts: 245
Joined: Tue Dec 08, 2009 6:47 pm

Thought i would post this in the case that it helps anyone :P (if you find bugs please do inform me :P)
Linked List
Code: Select all
#pragma once
#include <iostream>
/*Liked the lists how they worked in C#, just easier to use, expect a lot of places that could be improved :P
Author: Reece Clarke
Under licence: CC BY ("https://creativecommons.org/licenses/")
*/

template <class T>
class List
{

	typedef struct Node{
		T Data; //Stores data
		Node *Next; //Points to next
		Node(){
			Next = NULL;
		}
	} *nodePtr; //Every time node ptr it means pointer (same as typedef struct node *nodePtr;)


	nodePtr headNode; //The begining of the list
	nodePtr currentNode; //Current accessed node
	nodePtr tempNode; //Temp storage
public:
	/*Constructor and deconstructor*/
	List(){
		headNode = NULL;
		currentNode = NULL;
		tempNode = NULL;
	}

	~List(){
	}
	/*Nodes*/
	void Add(T _Data); //Adds a node
	void Remove(T _Data);//Deletes a node
	void RemoveByIndex(int _Node);//Deletes a node afer going though a certin amount of nodes
	void Clear(); //Clears Items
	int Count(); //Returns the total number of nodes
	bool Contains(T _Data); //Checks if it contains a given object
	/*Operator*/
	T& operator[](int);
};


template<class T>
void List<T>::Add(T _Data){
	nodePtr n = new Node; //Creates a new node and points to it
	n->Next = NULL; //Next is null because it is the tail node
	n->Data = _Data; //sets the data

	if (headNode != NULL){ //If the head node is not null then a list must already and exist and it not be the first node
		currentNode = headNode; //Points the current node to the head node
		while (currentNode->Next != NULL){ //If the current nodes next is not equal to null, then it has not reached the end yet and will move though the nodes until it is
			currentNode = currentNode->Next; //Moves to the next node
		}
		currentNode->Next = n; //Now we are at the end the next node element is pointing to the one we just created.
	}
	else{ //If it is a new list and this its first element
		headNode = n; //sets the head node to n
	}
}

template<class T>
void List<T>::Remove(T _Data){
	nodePtr delPtr = NULL; //We define a delete pointer
	tempNode = headNode;//sets temp to head node aka the beigning of the list
	currentNode = headNode; //sets the same as temp

	while (currentNode != NULL && currentNode->Data != _Data){ //While the pointer is not at the end of the list or at the node we want to delete
		tempNode = currentNode; //sets the temp node to the current
		currentNode = currentNode->Next;// then moves the current to the next node
	}
	if (currentNode != NULL){
		delPtr = currentNode; //points delete node to the current node
		currentNode = currentNode->Next; //advances the current node
		tempNode->Next = currentNode; //patches the last node to point to the current node.
	}
	if (delPtr == headNode){ // If its the head node move the head node to the next one
		headNode = delPtr->Next;
		tempNode = NULL;
	}
	delete delPtr; // Deletes the node the del pointer is pointing too.

}

template <class T>
void List<T>::RemoveByIndex(int _count){
	nodePtr delPtr = NULL; //We define a delete pointer
	tempNode = headNode;//sets temp to head node aka the beigning of the list
	currentNode = headNode; //sets the same as temp
	int _TempCount = 0;

	while (currentNode != NULL && _count == _TempCount){ //While the pointer is not at the end of the list or at the count we wanted to delete
		tempNode = currentNode; //sets the temp node to the current
		currentNode = currentNode->Next;// then moves the current to the next node
		_count++;
	}
	if (currentNode != NULL){
		delPtr = currentNode; //points delete node to the current node
		currentNode = currentNode->Next; //advances the current node
		tempNode->Next = currentNode; //patches the last node to point to the current node.
	}
	if (delPtr == headNode){ // If its the head node move the head node to the next one
		headNode = delPtr->Next;
		tempNode = NULL;
	}
	delete delPtr; // Deletes the node the del pointer is pointing too.

}

template<class T>
int List<T>::Count(){
	if (headNode == NULL) return 0;
	int _OutPut = 1; //creates a counter starting at 1
	currentNode = headNode; // Sets the head node to the current node
	while (currentNode->Next != NULL){ //Counts through adding one each time
		_OutPut++;
		currentNode = currentNode->Next;
	}
	return _OutPut;
}

template<class T>
bool List<T>::Contains(T _Data){
	tempNode = headNode;//sets temp to head node aka the beigning of the list
	currentNode = headNode; //sets the same as temp
	while (currentNode != NULL && currentNode->Data != _Data){ //While the pointer is not at the end of the list or at the node we want to delete
		tempNode = currentNode; //sets the temp node to the current
		currentNode = currentNode->Next;// then moves the current to the next node
	}
	if (currentNode != NULL) //If teh current node is not null then return true as it has found the data
		return true;
	return false; // else return false because it has not found the data
}

template<class T>
void List<T>::Clear(){ // -----At somepoint rewrite this so its more efficent----
	for (int i = Count(); i != 0; i--){ //Counts though the list
		RemoveByIndex(i); //Deletes each node
	}
	headNode = NULL;
}

template<class T>
T& List<T>::operator[](int _ID){
	currentNode = headNode; //sets the same as temp
	int _TempCount = 0;

	while (currentNode != NULL && _ID != _TempCount){
		currentNode = currentNode->Next;// then moves the current to the next node
		_TempCount++;
	}
		return currentNode->Data;
}
Dictionary
Code: Select all
#pragma once
#include "List.h"
template<class I,class T> //I = Identifier, T = Data

/*
Author: Reece Clarke
Under licence: CC BY ("https://creativecommons.org/licenses/")
*/

class Dictionary
{
	typedef struct Node{
		I Identifier; //What is used to identify the node
		T Data; // The data the node stores
		Node *Next; //The next node along
	} *nodePtr;
	
	nodePtr headNode;
	nodePtr currentNode;
	nodePtr tempNode;
public:
	Dictionary(){
		headNode = NULL;
		currentNode = NULL;
		tempNode = NULL;
	}
	void Add(I _Key, T _Data){
		nodePtr n = new Node; //Creates a new node and assigns it to n
		n->Next = NULL; //Sets next as null as its at the end
		n->Data = _Data; //sets the data to the assigned data
		n->Identifier = _Key; //sets the key

		if (headNode != NULL){ //If a node already exists
			currentNode = headNode;
			while (currentNode->Next != NULL){//Find last node
				currentNode = currentNode->Next; //Move along till last node
			}
			currentNode->Next = n; //sets the last nodes next node to what we just created
		}
		else{//else if it is the first node
			headNode = n; //sets the head node to n
		}
	}

	void RemoveFirst(I _Key){
		nodePtr delPtr = NULL; //We define a delete pointer
		tempNode = headNode;//sets temp to head node aka the beigning of the list
		currentNode = headNode; //sets the same as temp

		while (currentNode != NULL && currentNode->Identifier != _Key){ //While the pointer is not at the end of the list or at the node we want to delete
			tempNode = currentNode; //sets the temp node to the current
			currentNode = currentNode->Next;// then moves the current to the next node
		}
		if (currentNode != NULL){
			delPtr = currentNode; //points delete node to the current node
			currentNode = currentNode->Next; //advances the current node
			tempNode->Next = currentNode; //patches the last node to point to the current node.
		}
		if (delPtr == headNode){ // If its the head node move the head node to the next one
			headNode = delPtr->Next;
			tempNode = NULL;
		}
		delete delPtr; // Deletes the node the del pointer is pointing too.

	}
	void RemoveAll(I _Key){ // --- update with more efficent function at some point ---
		for (int i = CountType(_Key); i == 0; i--){ // Checks how many types of that object they are
			RemoveFirst(_Key); //Calls the remove first function that many times
		}
	}
	void Clear(){//Clears everything
		for (int i = Count(); i != 0; i--){ //Counts though the list
			RemoveByIndex(i); //Deletes each node
		}
	}
	void RemoveByIndex(int _Index){
		nodePtr delPtr = NULL; //We define a delete pointer
		tempNode = headNode;//sets temp to head node aka the beigning of the list
		currentNode = headNode; //sets the same as temp
		int _TempCount = 0;

		while (currentNode != NULL && _Index == _TempCount){ //While the pointer is not at the end of the list or at the count we wanted to delete
			tempNode = currentNode; //sets the temp node to the current
			currentNode = currentNode->Next;// then moves the current to the next node
			_TempCount++;
		}
		if (currentNode != NULL){
			delPtr = currentNode; //points delete node to the current node
			currentNode = currentNode->Next; //advances the current node
			tempNode->Next = currentNode; //patches the last node to point to the current node.
		}
		if (delPtr == headNode){ // If its the head node move the head node to the next one
			headNode = delPtr->Next;
			tempNode = NULL;
		}
		delete delPtr; // Deletes the node the del pointer is pointing too.
	}
	bool Contains(I _Key){
		tempNode = headNode;//sets temp to head node aka the beigning of the list
		currentNode = headNode; //sets the same as temp
		while (currentNode != NULL && currentNode->Identifier != _Key){ //While the pointer is not at the end of the list or at the node we want to delete
			tempNode = currentNode; //sets the temp node to the current
			currentNode = currentNode->Next;// then moves the current to the next node
		}
		if (currentNode != NULL) //If teh current node is not null then return true as it has found the data
			return true;
		return false; // else return false because it has not found the data
	}
	int Count(){
		if (headNode == NULL) return 0;
		int _OutPut = 1; //creates a counter starting at 1
		currentNode = headNode; // Sets the head node to the current node
		while (currentNode->Next != NULL){ //Counts through adding one each time
			_OutPut++;
			currentNode = currentNode->Next;
		}
		return _OutPut;
	}
	int CountType(I _Key){
		if (headNode == NULL) return 0;
		int _OutPut = 1; //creates a counter starting at 1
		currentNode = headNode; // Sets the head node to the current node
		while (currentNode->Next != NULL){ //Counts through adding one each time
			if (currentNode->Identifier == _Key)
			_OutPut++;
			currentNode = currentNode->Next;
		}
		return _OutPut;
	}
	T& FindByKey(I _Key){
		currentNode = headNode; //sets the same as temp
		while (currentNode != NULL && currentNode->Identifier != _Key){ //While the pointer is not at the end of the list or at the node we want to delete
			tempNode = currentNode; //sets the temp node to the current
			currentNode = currentNode->Next;// then moves the current to the next node
		}
		return currentNode->Data;
	}
	List<T> FindAllByKey(I _Key){
		&List<T> Return_List;
		while (currentNode != NULL){ //While the pointer is not at the end of the list or at the node we want to return
			currentNode = currentNode->Next;// then moves the current to the next node
			if (currentNode->Identifier == _Key) //if current node contains the key we are looking for
				Return_List.Add(currentNode->Data);//add to the list
		}
		return Return_List;
	}

	I GetKey(int _ID){
		currentNode = headNode; //sets the same as temp
		int _TempCount = 0;
		while (currentNode != NULL && _ID != _TempCount){
			currentNode = currentNode->Next;// then moves the current to the next node
			_TempCount++;
		}
		return currentNode->Identifier;
	}

	T& operator[](int _ID){
		currentNode = headNode; //sets the same as temp
		int _TempCount = 0;
		while (currentNode != NULL && _ID != _TempCount){
			currentNode = currentNode->Next;// then moves the current to the next node
			_TempCount++;
		}
		return currentNode->Data;
	}

};


Image
1 post Page 1 of 1
Return to “Quick Snips”