Import backup from 2004-05-22
This commit is contained in:
1 parent
6b6e19effd
commit
a982312d2c
11 files changed
+225
-170
No files matched your search
@@ -0,0 +1,180 @@
|
||||
|
||||
template <typename Element>
|
||||
class LinkedList
|
||||
{
|
||||
private:
|
||||
class LinkedNode
|
||||
{
|
||||
public:
|
||||
LinkedNode()
|
||||
{
|
||||
prev = next = 0;
|
||||
}
|
||||
LinkedNode(Element dataElement)
|
||||
{
|
||||
data = dataElement;
|
||||
prev = next = 0;
|
||||
}
|
||||
Element data;
|
||||
LinkedNode *next;
|
||||
LinkedNode *prev;
|
||||
};
|
||||
|
||||
LinkedNode *firstNode;
|
||||
LinkedNode *lastNode;
|
||||
int count;
|
||||
|
||||
public:
|
||||
class iterator
|
||||
{
|
||||
private:
|
||||
LinkedNode *theNode;
|
||||
public:
|
||||
iterator(LinkedNode & node)
|
||||
{
|
||||
theNode = &node;
|
||||
}
|
||||
iterator(LinkedNode *node)
|
||||
{
|
||||
theNode = node;
|
||||
}
|
||||
Element & operator*()
|
||||
{
|
||||
return theNode->data;
|
||||
}
|
||||
Element & operator++()
|
||||
{
|
||||
theNode = theNode->next;
|
||||
return theNode->data;
|
||||
}
|
||||
Element & operator++(int dum)
|
||||
{
|
||||
theNode = theNode->next;
|
||||
return theNode->prev->data;
|
||||
}
|
||||
};
|
||||
|
||||
class reverse_iterator
|
||||
{
|
||||
private:
|
||||
LinkedNode *theNode;
|
||||
public:
|
||||
reverse_iterator(LinkedNode & node)
|
||||
{
|
||||
theNode = &node;
|
||||
}
|
||||
reverse_iterator(LinkedNode *node)
|
||||
{
|
||||
theNode = node;
|
||||
}
|
||||
Element & operator*()
|
||||
{
|
||||
return theNode->data;
|
||||
}
|
||||
Element & operator++()
|
||||
{
|
||||
theNode = theNode->prev;
|
||||
return theNode->data;
|
||||
}
|
||||
Element & operator++(int dum)
|
||||
{
|
||||
theNode = theNode->prev;
|
||||
return theNode->next->data;
|
||||
}
|
||||
};
|
||||
|
||||
LinkedList();
|
||||
int numElements() { return count; }
|
||||
iterator begin() { return new iterator(firstNode); }
|
||||
iterator end() { return new iterator(lastNode->next); }
|
||||
reverse_iterator rbegin() { return new reverse_iterator(lastNode); }
|
||||
reverse_iterator rend() { return new reverse_iterator(firstNode->prev); }
|
||||
LinkedList & insert(int index, Element e);
|
||||
LinkedList & push_back(Element e);
|
||||
LinkedList & remove(int index);
|
||||
LinkedList & pop_back();
|
||||
Element & operator[](int index);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
LinkedList<T>::LinkedList()
|
||||
{
|
||||
firstNode = lastNode = new LinkedNode();
|
||||
count = 0;
|
||||
}
|
||||
|
||||
template <typename Element>
|
||||
LinkedList<Element> & LinkedList<Element>::insert(int index, Element e)
|
||||
{
|
||||
if (index == count)
|
||||
push_back(e);
|
||||
else if (index >= 0 && index < count)
|
||||
{
|
||||
LinkedNode *nptr = firstNode;
|
||||
for (int i = 0; i <= index; i++)
|
||||
nptr = nptr->next;
|
||||
LinkedNode *newptr = new LinkedNode(e);
|
||||
newptr->next = nptr;
|
||||
newptr->prev = nptr->prev;
|
||||
newptr->prev->next = newptr;
|
||||
nptr->prev = newptr;
|
||||
count++;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Element>
|
||||
LinkedList<Element> & LinkedList<Element>::push_back(Element e)
|
||||
{
|
||||
lastNode->next = new LinkedNode(e);
|
||||
lastNode->next->prev = lastNode;
|
||||
lastNode = lastNode->next;
|
||||
count++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Element>
|
||||
LinkedList<Element> & LinkedList<Element>::remove(int index)
|
||||
{
|
||||
if (index == count-1)
|
||||
pop_back();
|
||||
else if (index >= 0 && index < count)
|
||||
{
|
||||
LinkedNode *nptr = firstNode;
|
||||
for (int i = 0; i <= index; i++)
|
||||
nptr = nptr->next;
|
||||
nptr->prev->next = nptr->next;
|
||||
nptr->next->prev = nptr->prev;
|
||||
delete nptr;
|
||||
count--;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Element>
|
||||
LinkedList<Element> & LinkedList<Element>::pop_back()
|
||||
{
|
||||
if (count)
|
||||
{
|
||||
lastNode = lastNode->prev;
|
||||
delete lastNode->next;
|
||||
lastNode->next = 0;
|
||||
count--;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Element>
|
||||
Element & LinkedList<Element>::operator[](int index)
|
||||
{
|
||||
if (index >= 0 && index < count)
|
||||
{
|
||||
LinkedNode *nptr = firstNode;
|
||||
for (int i = 0; i <= index; i++)
|
||||
nptr = nptr->next;
|
||||
return nptr->data;
|
||||
}
|
||||
return firstNode->data;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
// string.c
|
||||
// Author: Josh Holtrop
|
||||
// Created: 02/26/04
|
||||
// Implements string functions
|
||||
|
||||
#include "string.h"
|
||||
#include "asmfuncs.h"
|
||||
|
||||
char *strcat(char *dest, char *src)
|
||||
{
|
||||
strcpy(dest+strlen(dest), src);
|
||||
return dest;
|
||||
}
|
||||
|
||||
|
||||
//Splits a string into multiple strings by turning all characters
|
||||
// equal to delim into null values (string termination character)
|
||||
//Returns the number of strings after the split, 1 if no delim chars
|
||||
int string_split(char *str, char delim)
|
||||
{
|
||||
if (strlen(str) < 1)
|
||||
return 0; //empty string
|
||||
int count = 1;
|
||||
for (; *str; str++)
|
||||
{
|
||||
if (*str == delim)
|
||||
{
|
||||
count++;
|
||||
*str = 0;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
//Advances a char pointer to the byte after the current string's
|
||||
// null-terminating character
|
||||
//Useful after calling string_split()
|
||||
//Returns a pointer to the following string
|
||||
char *string_advance(char *str)
|
||||
{
|
||||
for (; *str; str++);
|
||||
return str+1;
|
||||
}
|
||||
|
||||
|
||||
void rtrim(char *str)
|
||||
{
|
||||
str += strlen(str); //now points to the null character at the end of the string
|
||||
str--;
|
||||
for (;;)
|
||||
{
|
||||
if ((*str == ' ') || (*str == '\t') || (*str == '\n'))
|
||||
*str-- = 0;
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
char *ucase(char *str)
|
||||
{
|
||||
char *ret = str;
|
||||
for (;;)
|
||||
{
|
||||
if (*str == 0)
|
||||
break;
|
||||
if ((*str >= 'a') && (*str <= 'z'))
|
||||
*str = (*str) - 32;
|
||||
str++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
char *lcase(char *str)
|
||||
{
|
||||
char *ret = str;
|
||||
for (;;)
|
||||
{
|
||||
if (*str == 0)
|
||||
break;
|
||||
if ((*str >= 'A') && (*str <= 'Z'))
|
||||
*str = (*str) + 32;
|
||||
str++;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// strings.h
|
||||
// Author: Josh Holtrop
|
||||
// Created: 02/26/04
|
||||
// Implements string functions
|
||||
|
||||
#ifndef __HOS_STRING__
|
||||
#define __HOS_STRING__ __HOS_STRING__
|
||||
|
||||
char *strcat(char *dest, char *src);
|
||||
int string_split(char *str, char delim);
|
||||
char *string_advance(char *str);
|
||||
void rtrim(char *str);
|
||||
char *ucase(char *str);
|
||||
char *lcase(char *str);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in new issue
Block a user