Add CT331 Programming Paradigms

This commit is contained in:
2023-12-07 01:33:53 +00:00
parent 38a012c323
commit 262614ce83
207 changed files with 4516 additions and 0 deletions

View File

@ -0,0 +1,15 @@
#include <stdio.h>
int main() {
int my_int;
int* my_int_pointer;
long my_long;
double * my_double_pointer;
char ** my_char_pointer_pointer;
printf("The size of my_int is %lu bytes\n", sizeof(my_int));
printf("The size of my_int_pointer is %lu bytes\n", sizeof(my_int_pointer));
printf("The size of my_long is %lu bytes\n", sizeof(my_long));
printf("The size of my_double_pointer is %lu bytes\n", sizeof(my_double_pointer));
printf("The size of my_char_pointer_pointer is %lu bytes\n", sizeof(my_char_pointer_pointer));
}

View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/question2.iml" filepath="$PROJECT_DIR$/.idea/question2.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

View File

@ -0,0 +1,8 @@
#include <stdio.h>
#include "linkedList.h"
#include "tests.h"
int main(int argc, char* argv[]){
runTests();
return 0;
}

View File

@ -0,0 +1,162 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedList.h"
typedef struct listElementStruct{
char* data;
size_t size;
struct listElementStruct* next;
} listElement;
//Creates a new linked list element with given content of size
//Returns a pointer to the element
listElement* createEl(char* data, size_t size){
listElement* e = malloc(sizeof(listElement));
if(e == NULL){
//malloc has had an error
return NULL; //return NULL to indicate an error.
}
char* dataPointer = malloc(sizeof(char)*size);
if(dataPointer == NULL){
//malloc has had an error
free(e); //release the previously allocated memory
return NULL; //return NULL to indicate an error.
}
strcpy(dataPointer, data);
e->data = dataPointer;
e->size = size;
e->next = NULL;
return e;
}
//Prints out each element in the list
void traverse(listElement* start){
listElement* current = start;
while(current != NULL){
printf("%s\n", current->data);
current = current->next;
}
}
//Inserts a new element after the given el
//Returns the pointer to the new element
listElement* insertAfter(listElement* el, char* data, size_t size){
listElement* newEl = createEl(data, size);
listElement* next = el->next;
newEl->next = next;
el->next = newEl;
return newEl;
}
//Delete the element after the given el
void deleteAfter(listElement* after){
listElement* delete = after->next;
listElement* newNext = delete->next;
after->next = newNext;
//need to free the memory because we used malloc
free(delete->data);
free(delete);
}
// returns the number of elements in the list
int length(listElement* list) {
int length = 0;
listElement* current = list;
// traversing the list and counting each element
while(current != NULL){
length++;
current = current->next;
}
return length;
}
// push a new element onto the head of a list and update the list reference using side effects
void push(listElement** list, char* data, size_t size) {
// create the new element
listElement* newElement = createEl(data, size);
// handle malloc errors
if (newElement == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
// make the the new element point to the current head of the list
newElement->next = *list;
// make the list reference to point to the new head element
*list = newElement;
}
// pop an element from the head of a list and update the list reference using side effects
// assuming that the desired return value here is the popped element, as is standard for POP operations
listElement* pop(listElement** list) {
// don't bother if list is non existent
if (*list == NULL) { return NULL; }
;
// getting reference to the element to be popped
listElement* poppedElement = *list;
// make the the second element the new head of the list -- this could be NULL, so the list would be NULL also
*list = (*list)->next;
// detach the popped element from the list
poppedElement->next = NULL;
return poppedElement;
}
// enque a new element onto the head of the list and update the list reference using side effects
// essentially the same as push
void enqueue(listElement** list, char* data, size_t size) {
// create the new element
listElement* newElement = createEl(data, size);
// handle malloc errors
if (newElement == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
// make the the new element point to the current head of the list
newElement->next = *list;
// make the list reference to point to the new head element
*list = newElement;
}
// dequeue an element from the tail of the list by removing the element from the list via side effects, and returning the removed item
// assuming that we want to return the dequeued element rather than the list itself, as enqueue returns nothing and uses side effects, so dequeue should also use side effects
listElement* dequeue(listElement* list) {
// there are three cases that we must consider: a list with 0 elements, a list with 1 element, & a list with >=2 elements
// don't bother if list is non existent
if (list == NULL) { return NULL; }
// if there is only one element in the list, i.e. the head element is also the tail element, just returning this element
// this means that the listElement pointer that was passed to this function won't be updated
// ideally, we would set it to NULL but we can't do that since `list` is a pointer that has been passed by value, so we can't update the pointer itself. we would need a pointer to a pointer to have been passed
if (list->next == NULL) {
return list;
}
// traversing the list to find the second-to-last element
listElement* current = list;
while (current->next->next != NULL) {
current = current->next;
}
// get reference to the element to be dequeued
listElement* dequeuedElement = current->next;
// make the penultimate element the tail by removing reference to the old tail
current->next = NULL;
return list;
}

View File

@ -0,0 +1,36 @@
#ifndef CT331_ASSIGNMENT_LINKED_LIST
#define CT331_ASSIGNMENT_LINKED_LIST
typedef struct listElementStruct listElement;
//Creates a new linked list element with given content of size
//Returns a pointer to the element
listElement* createEl(char* data, size_t size);
//Prints out each element in the list
void traverse(listElement* start);
//Inserts a new element after the given el
//Returns the pointer to the new element
listElement* insertAfter(listElement* after, char* data, size_t size);
//Delete the element after the given el
void deleteAfter(listElement* after);
// returns the number of elements in the list
int length(listElement* list);
// push a new element onto the head of a list and update the list reference using side effects
void push(listElement** list, char* data, size_t size);
// pop an element from the head of a list and update the list reference using side effects
listElement* pop(listElement** list);
// enque a new element onto the head of the list and update the list reference using side effects
void enqueue(listElement** list, char* data, size_t size);
// dequeue an element from the tail of the list
listElement* dequeue(listElement* list);
#endif

View File

@ -0,0 +1,57 @@
#include <stdio.h>
#include "tests.h"
#include "linkedList.h"
void runTests(){
printf("Tests running...\n");
listElement* l = createEl("Test String (1).", sizeof("Test String (1)."));
//printf("%s\n%p\n", l->data, l->next);
//Test create and traverse
traverse(l);
printf("\n");
//Test insert after
printf("Testing insertAfter()\n");
listElement* l2 = insertAfter(l, "another string (2)", sizeof("another test string(2)"));
insertAfter(l2, "a final string (3)", sizeof("a final string(3)"));
traverse(l);
printf("\n");
// test length function
printf("Testing length()\n");
int l_length = length(l);
printf("The length of l is %d\n\n", l_length);
// test push
printf("Testing push()\n");
push(&l, "yet another test string", sizeof("yet another test string"));
traverse(l);
printf("\n\n");
// test pop
printf("Testing pop()\n");
listElement* popped = pop(&l);
traverse(l);
printf("\n\n");
// Test delete after
printf("Testing deleteAfter()\n");
deleteAfter(l);
traverse(l);
printf("\n");
// test enqueue
printf("Testing enqueue()\n");
enqueue(&l, "enqueued test string", sizeof("enqueued test string"));
traverse(l);
printf("\n");
// test dequeue
printf("Testing dequeue()\n");
dequeue(l);
traverse(l);
printf("\n");
printf("\nTests complete.\n");
}

View File

@ -0,0 +1,6 @@
#ifndef CT331_ASSIGNMENT_TESTS
#define CT331_ASSIGNMENT_TESTS
void runTests();
#endif

View File

@ -0,0 +1,8 @@
#include <stdio.h>
#include "genericLinkedList.h"
#include "tests.h"
int main(int argc, char* argv[]){
runTests();
return 0;
}

View File

@ -0,0 +1,167 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "genericLinkedList.h"
typedef struct listElementStruct{
void* data;
void (*printFunction)(void*);
size_t size;
struct listElementStruct* next;
} listElement;
//Creates a new linked list element with given content of size
//Returns a pointer to the element
listElement* createEl(void* data, size_t size, void (*printFunction)(void*)) {
listElement* e = malloc(sizeof(listElement));
if(e == NULL){
//malloc has had an error
return NULL; //return NULL to indicate an error.
}
void* dataPointer = malloc(sizeof(void)*size);
if(dataPointer == NULL){
//malloc has had an error
free(e); //release the previously allocated memory
return NULL; //return NULL to indicate an error.
}
strcpy(dataPointer, data);
e->data = dataPointer;
e->printFunction = printFunction;
e->size = size;
e->next = NULL;
return e;
}
//Prints out each element in the list
void traverse(listElement* start){
listElement* current = start;
while(current != NULL){
current->printFunction(current->data);
// printf("%s\n", current->data);
current = current->next;
}
}
//Inserts a new element after the given el
//Returns the pointer to the new element
listElement* insertAfter(listElement* el, void* data, size_t size, void (*printFunction)(void*)){
listElement* newEl = createEl(data, size, printFunction);
listElement* next = el->next;
newEl->next = next;
el->next = newEl;
return newEl;
}
//Delete the element after the given el
void deleteAfter(listElement* after){
listElement* delete = after->next;
listElement* newNext = delete->next;
after->next = newNext;
//need to free the memory because we used malloc
free(delete->data);
free(delete);
}
// returns the number of elements in the list
int length(listElement* list) {
int length = 0;
listElement* current = list;
// traversing the list and counting each element
while(current != NULL){
length++;
current = current->next;
}
return length;
}
// push a new element onto the head of a list and update the list reference using side effects
void push(listElement** list, void* data, size_t size, void (*printFunction)(void*)) {
// create the new element
listElement* newElement = createEl(data, size, printFunction);
// handle malloc errors
if (newElement == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
// make the the new element point to the current head of the list
newElement->next = *list;
// make the list reference to point to the new head element
*list = newElement;
}
// pop an element from the head of a list and update the list reference using side effects
// assuming that the desired return value here is the popped element, as is standard for POP operations
listElement* pop(listElement** list) {
// don't bother if list is non existent
if (*list == NULL) { return NULL; }
;
// getting reference to the element to be popped
listElement* poppedElement = *list;
// make the the second element the new head of the list -- this could be NULL, so the list would be NULL also
*list = (*list)->next;
// detach the popped element from the list
poppedElement->next = NULL;
return poppedElement;
}
// enque a new element onto the head of the list and update the list reference using side effects
// essentially the same as push
void enqueue(listElement** list, void* data, size_t size, void (*printFunction)(void*)) {
// create the new element
listElement* newElement = createEl(data, size, printFunction);
// handle malloc errors
if (newElement == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
// make the the new element point to the current head of the list
newElement->next = *list;
// make the list reference to point to the new head element
*list = newElement;
}
// dequeue an element from the tail of the list by removing the element from the list via side effects, and returning the removed item
// assuming that we want to return the dequeued element rather than the list itself, as enqueue returns nothing and uses side effects, so dequeue should also use side effects
listElement* dequeue(listElement* list) {
// there are three cases that we must consider: a list with 0 elements, a list with 1 element, & a list with >=2 elements
// don't bother if list is non existent
if (list == NULL) { return NULL; }
// if there is only one element in the list, i.e. the head element is also the tail element, just returning this element
// this means that the listElement pointer that was passed to this function won't be updated
// ideally, we would set it to NULL but we can't do that since `list` is a pointer that has been passed by value, so we can't update the pointer itself. we would need a pointer to a pointer to have been passed
if (list->next == NULL) {
return list;
}
// traversing the list to find the second-to-last element
listElement* current = list;
while (current->next->next != NULL) {
current = current->next;
}
// get reference to the element to be dequeued
listElement* dequeuedElement = current->next;
// make the penultimate element the tail by removing reference to the old tail
current->next = NULL;
return list;
}

View File

@ -0,0 +1,35 @@
#ifndef CT331_ASSIGNMENT_LINKED_LIST
#define CT331_ASSIGNMENT_LINKED_LIST
typedef struct listElementStruct listElement;
//Creates a new linked list element with given content of size
//Returns a pointer to the element
listElement* createEl(void* data, size_t size, void (*printFunction)(void*));
//Prints out each element in the list
void traverse(listElement* start);
//Inserts a new element after the given el
//Returns the pointer to the new element
listElement* insertAfter(listElement* after, void* data, size_t size, void (*printFunction)(void*));
//Delete the element after the given el
void deleteAfter(listElement* after);
// returns the number of elements in the list
int length(listElement* list);
// push a new element onto the head of a list and update the list reference using side effects
void push(listElement** list, void* data, size_t size, void (*printFunction)(void*));
// pop an element from the head of a list and update the list reference using side effects
listElement* pop(listElement** list);
// enque a new element onto the head of the list and update the list reference using side effects
void enqueue(listElement** list, void* data, size_t size, void (*printFunction)(void*));
// dequeue an element from the tail of the list
listElement* dequeue(listElement* list);
#endif

View File

@ -0,0 +1,79 @@
#include <stdio.h>
#include "tests.h"
#include "genericLinkedList.h"
// functions to print out different data types
// a more professional design might be to put these in the genericLinkedList header file but i only need these for testing purposes
void printChar(void* data) {
printf("%c\n", *(char*) data);
}
void printStr(void* data) {
printf("%s\n", (char*) data);
}
void printInt(void* data) {
printf("%d\n", *(int*) data);
}
void runTests(){
printf("Tests running...\n");
listElement* l = createEl("Test String (1).", sizeof("Test String (1)."), printStr);
//printf("%s\n%p\n", l->data, l->next);
//Test create and traverse
traverse(l);
printf("\n");
//Test insert after
printf("Testing insertAfter()\n");
listElement* l2 = insertAfter(l, "another string (2)", sizeof("another string (2)"), printStr);
insertAfter(l2, "a final string (3)", sizeof("a final string (3)"), printStr);
traverse(l);
printf("\n");
// test length function
printf("Testing length()\n");
int l_length = length(l);
printf("The length of l is %d\n\n", l_length);
// test push
printf("Testing push()\n");
push(&l, "yet another test string", sizeof("yet another test string"), printStr);
traverse(l);
printf("\n\n");
// test pop
printf("Testing pop()\n");
listElement* popped = pop(&l);
traverse(l);
printf("\n\n");
// Test delete after
printf("Testing deleteAfter()\n");
deleteAfter(l);
traverse(l);
printf("\n");
// test enqueue
printf("Testing enqueue()\n");
enqueue(&l, "enqueued test string", sizeof("enqueued test string"), printStr);
traverse(l);
printf("\n");
// test dequeue
printf("Testing dequeue()\n");
dequeue(l);
traverse(l);
printf("\n");
printf("Testing pushing different data types\n");
int myint = 42;
push(&l, &myint, sizeof(myint), printInt);
char mychar = 'c';
push(&l, &mychar, sizeof(mychar), printChar);
traverse(l);
printf("\n\n");
printf("\nTests complete.\n");
}

View File

@ -0,0 +1,6 @@
#ifndef CT331_ASSIGNMENT_TESTS
#define CT331_ASSIGNMENT_TESTS
void runTests();
#endif

View File

@ -0,0 +1,343 @@
%! TeX program = lualatex
\documentclass[a4paper]{article}
% packages
\usepackage{microtype} % Slightly tweak font spacing for aesthetics
\usepackage[english]{babel} % Language hyphenation and typographical rules
\usepackage[final, colorlinks = false, urlcolor = cyan]{hyperref}
\usepackage{changepage} % adjust margins on the fly
\usepackage{fontspec}
\usepackage{minted}
\usepackage{xcolor}
\usepackage{pgfplots}
\pgfplotsset{width=\textwidth,compat=1.9}
\usepackage{caption}
\newenvironment{code}{\captionsetup{type=listing, skip=0pt}}{}
% \captionsetup{skip=0pt}
% \setlength{\abovecaptionskip}{3pt}
% \setlength{\belowcaptionskip}{5pt}
\usepackage[yyyymmdd]{datetime}
\renewcommand{\dateseparator}{--}
\setmainfont{EB Garamond}
\setmonofont[Scale=MatchLowercase]{Deja Vu Sans Mono}
\usepackage{titlesec}
% \titleformat{\section}{\LARGE\bfseries}{}{}{}[\titlerule]
% \titleformat{\subsection}{\Large\bfseries}{}{0em}{}
% \titlespacing{\subsection}{0em}{-0.7em}{0em}
%
% \titleformat{\subsubsection}{\large\bfseries}{}{0em}{$\bullet$ }
% \titlespacing{\subsubsection}{1em}{-0.7em}{0em}
% margins
\addtolength{\hoffset}{-2.25cm}
\addtolength{\textwidth}{4.5cm}
\addtolength{\voffset}{-3.25cm}
\addtolength{\textheight}{5cm}
\setlength{\parskip}{0pt}
\setlength{\parindent}{0in}
% \setcounter{secnumdepth}{0}
\begin{document}
\hrule \medskip
\begin{minipage}{0.295\textwidth}
\raggedright
\footnotesize
Name: Andrew Hayes \\
E-mail: \href{mailto://a.hayes18@universityofgalway.ie}{\texttt{a.hayes18@universityofgalway.ie}} \hfill\\
ID: 21321503 \hfill
\end{minipage}
\begin{minipage}{0.4\textwidth}
\centering
\vspace{0.4em}
\Large
\textbf{CT331} \\
\end{minipage}
\begin{minipage}{0.295\textwidth}
\raggedleft
\today
\end{minipage}
\medskip\hrule
\begin{center}
\normalsize
Assignment 1: Procedural Programming with C
\end{center}
\hrule
\section{Question 1}
\subsection{Part (A): Code}
\begin{code}
\inputminted[texcl, mathescape, linenos, breaklines, frame=single]{C}{../code/question1/question1.c}
\caption{\texttt{question1.c}}
\end{code}
\begin{figure}[H]
\centering
\includegraphics[width=0.8\textwidth]{./images/question1.png}
\caption{Console Output of \texttt{question1.c}}
\end{figure}
\subsection{Part (B): Comments}
The amount of memory allocated to variables of different types in C is determined at compile-time, and is dependent on the architecture of
the machine for which it is being compiled and the compiler used.
\begin{itemize}
\item On my machine, using GCC, an \verb|int| is allocated 4 bytes. This is the usual amount allocated on both 32-bit and 64-bit systems (my machine being of the
latter kind), although older 32-bit systems used 2 bytes for an \verb|int| (the same amount as for a \verb|short|).
4 bytes is used even on 64-bit machines to maintain backwards compatibility with older 32-bit architectures.
\item An \verb|int*| (a pointer to a variable of type \verb|int|) is allocated 8 bytes on my machine.
This is because that my machine has a 64-bit architecture, and therefore an address in memory is represented using 64 bits (8 bytes).
If this were compiled for a 32-bit machine, the size of an pointer would be 4 bytes since addresses are 32-bit.
\item A \verb|long| is allocated 8 bytes on my machine. This is because my machine is 64-bit and a \verb|long| is typically 8 bytes in length on such machines.
On 32-bit machines, a \verb|long| is typically 4 bytes.
\item The size of a pointer to a \verb|double| is the same as the size of any other pointer on the same machine; on 64-bit machines, pointers are 8 bytes, and on
32-bit machines, they are 4 bytes.
The type of data to which a pointer points has no effect on the size of the pointer, as the pointer is just a memory address.
\item A pointer to a \verb|char| pointer is the same size as any other pointer: 8 bytes on a 64-bit machine and 4 bytes on a 32-bit machine.
Note: it might be more intuitive to refer to a ``character pointer pointer'' as a pointer to a string in certain situations, as strings are character arrays,
and an array variable acts as a pointer to the first element in the array.
\end{itemize}
\section{Question 2}
\begin{code}
\begin{minted}[texcl, mathescape, linenos, breaklines, frame=single]{C}
// returns the number of elements in the list
int length(listElement* list);
// push a new element onto the head of a list and update the list reference using side effects
void push(listElement** list, char* data, size_t size);
// pop an element from the head of a list and update the list reference using side effects
listElement* pop(listElement** list);
// enque a new element onto the head of the list and update the list reference using side effects
void enqueue(listElement** list, char* data, size_t size);
// dequeue an element from the tail of the list
listElement* dequeue(listElement* list);
\end{minted}
\caption{My Additions to \texttt{linkedList.h}}
\end{code}
\begin{code}
\begin{minted}[texcl, mathescape, linenos, breaklines, frame=single]{C}
// returns the number of elements in the list
int length(listElement* list) {
int length = 0;
listElement* current = list;
// traversing the list and counting each element
while(current != NULL){
length++;
current = current->next;
}
return length;
}
// push a new element onto the head of a list and update the list reference using side effects
void push(listElement** list, char* data, size_t size) {
// create the new element
listElement* newElement = createEl(data, size);
// handle malloc errors
if (newElement == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
// make the the new element point to the current head of the list
newElement->next = *list;
// make the list reference to point to the new head element
*list = newElement;
}
// pop an element from the head of a list and update the list reference using side effects
// assuming that the desired return value here is the popped element, as is standard for POP operations
listElement* pop(listElement** list) {
// don't bother if list is non existent
if (*list == NULL) { return NULL; }
;
// getting reference to the element to be popped
listElement* poppedElement = *list;
// make the the second element the new head of the list -- this could be NULL, so the list would be NULL also
*list = (*list)->next;
// detach the popped element from the list
poppedElement->next = NULL;
return poppedElement;
}
// enque a new element onto the head of the list and update the list reference using side effects
// essentially the same as push
void enqueue(listElement** list, char* data, size_t size) {
// create the new element
listElement* newElement = createEl(data, size);
// handle malloc errors
if (newElement == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
// make the the new element point to the current head of the list
newElement->next = *list;
// make the list reference to point to the new head element
*list = newElement;
}
// dequeue an element from the tail of the list by removing the element from the list via side effects, and returning the removed item
// assuming that we want to return the dequeued element rather than the list itself, as enqueue returns nothing and uses side effects, so dequeue should also use side effects
listElement* dequeue(listElement* list) {
// there are three cases that we must consider: a list with 0 elements, a list with 1 element, & a list with >=2 elements
// don't bother if list is non existent
if (list == NULL) { return NULL; }
// if there is only one element in the list, i.e. the head element is also the tail element, just returning this element
// this means that the listElement pointer that was passed to this function won't be updated
// ideally, we would set it to NULL but we can't do that since `list` is a pointer that has been passed by value, so we can't update the pointer itself. we would need a pointer to a pointer to have been passed
if (list->next == NULL) {
return list;
}
// traversing the list to find the second-to-last element
listElement* current = list;
while (current->next->next != NULL) {
current = current->next;
}
// get reference to the element to be dequeued
listElement* dequeuedElement = current->next;
// make the penultimate element the tail by removing reference to the old tail
current->next = NULL;
return list;
}
\end{minted}
\caption{My Additions to \texttt{linkedList.c}}
\end{code}
\begin{code}
\begin{minted}[texcl, mathescape, linenos, breaklines, frame=single]{C}
// test length function
printf("Testing length()\n");
int l_length = length(l);
printf("The length of l is %d\n\n", l_length);
// test push
printf("Testing push()\n");
push(&l, "yet another test string", sizeof("yet another test string"));
traverse(l);
printf("\n\n");
// test pop
printf("Testing pop()\n");
listElement* popped = pop(&l);
traverse(l);
printf("\n\n");
// Test delete after
printf("Testing deleteAfter()\n");
deleteAfter(l);
traverse(l);
printf("\n");
// test enqueue
printf("Testing enqueue()\n");
enqueue(&l, "enqueued test string", sizeof("enqueued test string"));
traverse(l);
printf("\n");
// test dequeue
printf("Testing dequeue()\n");
dequeue(l);
traverse(l);
printf("\n");
printf("\nTests complete.\n");
\end{minted}
\caption{My Additions to \texttt{tests.c}}
\end{code}
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{./images/question2.png}
\caption{Console Output for Question 2}
\end{figure}
\section{Question 3}
\begin{code}
\inputminted[linenos, breaklines, frame=single]{C}{../code/question3/genericLinkedList.h}
\caption{\texttt{genericLinkedList.h}}
\end{code}
\begin{code}
\inputminted[linenos, breaklines, frame=single]{C}{../code/question3/genericLinkedList.c}
\caption{\texttt{genericLinkedList.c}}
\end{code}
\begin{code}
\inputminted[linenos, breaklines, frame=single]{C}{../code/question3/tests.c}
\caption{\texttt{tests.c}}
\end{code}
\begin{figure}[H]
\centering
\includegraphics[width=0.9\textwidth]{./images/question3.png}
\caption{Console Output for Question 3}
\end{figure}
\section{Question 4}
\subsection{Part 1}
Any algorithm for traversing a singly linked list in reverse will always first require traversing the list forwards, and will therefore be \emph{at least} somewhat
less efficient than a forwards traversal.
One of the simplest ways to traverse a linked list in reverse is to use a recursive function.
\begin{code}
\begin{minted}[linenos, breaklines, frame=single]{C}
void reverse_traverse(listElement* current){
if (current == NULL) { return; }
reverse_traverse(current->next);
current->printFunction(current->data);
}
\end{minted}
\caption{Recursive Function to Traverse a Singly Linked List in Reverse}
\end{code}
This is quite inefficient as it requires that the function call for each node persists on the stack until the last node is reached, using a lot of stack memory.
Another approach would be to iteratively reverse the linked list, by making some kind of data structure, linked list or otherwise, that contains the data of the
original linked list but in reverse, and then iterating over that forwards.
This would likely be more efficient in terms of memory \& computation.
\\\\
Because traversing a linked list in reverse always requires traversing it forwards first, any reverse algorithm will take at least twice as much memory \& computation
as traversing it forwards, which is $O(n)$.
It will also require that some way of storing the data in reverse in memory, either explicitly with a data, like in the iterative approach, or in the same manner
as the recursive approach, wherein the data is stored in reverse by the nested structure of the function calls: as each function call returns, the call structure
is iterated through in reverse.
Therefore, we also have at least $O(n)$ memory usage, as we have to store some sort of reverse data structure.
\subsection{Part 2}
The simplest way in which the structure of a linked list could be changed to make backwards traversal less intensive is to change it from a singly linked list to a
doubly linked list, i.e. instead of each node in the list containing a pointer to just the next node, make each node contain a pointer to both the next node \& the
previous node.
The backwards traversal of a doubly linked list is no more intensive than the forwards traversal of a linked list.
The drawback of using a doubly linked list is that it requires slightly more memory per node than a singly linked list, as you're storing an additional pointer
for every node.
\end{document}

View File

@ -0,0 +1,23 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{c+c1}{// class to implement Method 3 }
\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{class} \PYG{n+nc}{ReverseVSOriginal}\PYG{+w}{ }\PYG{k+kd}{implements}\PYG{+w}{ }\PYG{n}{PalindromeChecker}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// method 1 - reversed order String vs original String }
\PYG{+w}{ }\PYG{n+nd}{@Override}
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kt}{boolean}\PYG{+w}{ }\PYG{n+nf}{checkPalindrome}\PYG{p}{(}\PYG{n}{String}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{String}\PYG{+w}{ }\PYG{n}{reversedStr}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}\PYGZdq{}}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{0}\PYG{o}{]++}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// looping through each character in the String, backwards}
\PYG{+w}{ }\PYG{c+c1}{// incrementing operations counter by 2, 1 for initialisating i, 1 for getting str.length()}
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{0}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{for}\PYG{+w}{ }\PYG{p}{(}\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{length}\PYG{p}{();}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{\PYGZgt{}}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{o}{\PYGZhy{}\PYGZhy{}}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{0}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// for loop condition check & incrementing i}
\PYG{+w}{ }\PYG{n}{reversedStr}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{charAt}\PYG{p}{(}\PYG{n}{i}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{);}\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{0}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// returning true if the Strings are equal, false if not}
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{0}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{length}\PYG{p}{();}\PYG{+w}{ }\PYG{c+c1}{// the equals method must loop through each character of the String to check that they are equal so it is O(n)}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{equals}\PYG{p}{(}\PYG{n}{reversedStr}\PYG{p}{);}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,23 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{c+c1}{// class to implement Method 2 }
\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{class} \PYG{n+nc}{IVersusNMinusI}\PYG{+w}{ }\PYG{k+kd}{implements}\PYG{+w}{ }\PYG{n}{PalindromeChecker}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// method 2 - comparing each element at index i to the element at n - i where n is the last index}
\PYG{+w}{ }\PYG{n+nd}{@Override}
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kt}{boolean}\PYG{+w}{ }\PYG{n+nf}{checkPalindrome}\PYG{p}{(}\PYG{n}{String}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// looping through the first half of the String }
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{1}\PYG{o}{]++}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{for}\PYG{+w}{ }\PYG{p}{(}\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{\PYGZlt{}}\PYG{+w}{ }\PYG{n}{Math}\PYG{p}{.}\PYG{n+na}{floor}\PYG{p}{(}\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{length}\PYG{p}{()}\PYG{+w}{ }\PYG{o}{/}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{p}{);}\PYG{+w}{ }\PYG{n}{i}\PYG{o}{++}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{1}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// 1 for the getting str.length(), 1 for Math,floor, 1 for checking condition, 1 for incrementing}
\PYG{+w}{ }\PYG{c+c1}{// returning false if the digits don't match}
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{1}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// 1 for str.charAt(i), 1 for ((str.lenght() -1) - 1), 1 for the other str.charAt(), 1 for checking the condition}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{charAt}\PYG{p}{(}\PYG{n}{i}\PYG{p}{)}\PYG{+w}{ }\PYG{o}{!=}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{charAt}\PYG{p}{((}\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{length}\PYG{p}{()}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{+w}{ }\PYG{o}{\PYGZhy{}}\PYG{+w}{ }\PYG{n}{i}\PYG{p}{))}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{k+kc}{false}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// returning true as default}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{k+kc}{true}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,5 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{interface} \PYG{n+nc}{PalindromeChecker}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kt}{boolean}\PYG{+w}{ }\PYG{n+nf}{checkPalindrome}\PYG{p}{(}\PYG{n}{String}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{);}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,31 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{c+c1}{// class to implement method 4 }
\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{class} \PYG{n+nc}{RecursiveReverse}\PYG{+w}{ }\PYG{k+kd}{implements}\PYG{+w}{ }\PYG{n}{PalindromeChecker}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// comparing the String reversed using recursion to the original String (essentially method 1 but with recursion)}
\PYG{+w}{ }\PYG{n+nd}{@Override}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kt}{boolean}\PYG{+w}{ }\PYG{n+nf}{checkPalindrome}\PYG{p}{(}\PYG{n}{String}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// returning true if the original String is equal to the reversed String, false if not}
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{3}\PYG{o}{]++}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{equals}\PYG{p}{(}\PYG{n}{reverse}\PYG{p}{(}\PYG{n}{str}\PYG{p}{));}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// method to reverse the characters in a String using recursion }
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{static}\PYG{+w}{ }\PYG{n}{String}\PYG{+w}{ }\PYG{n+nf}{reverse}\PYG{p}{(}\PYG{n}{String}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// base case - returning an empty String if there is no character left in the String}
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{3}\PYG{o}{]++}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{length}\PYG{p}{()}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}\PYGZdq{}}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{k}{else}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k+kt}{char}\PYG{+w}{ }\PYG{n}{firstChar}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{charAt}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{);}\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{3}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{String}\PYG{+w}{ }\PYG{n}{remainder}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{substring}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{);}\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{3}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// selecting the rest of the String, excluding the 0th character}
\PYG{+w}{ }\PYG{c+c1}{// recursing with what's left of the String}
\PYG{+w}{ }\PYG{n}{String}\PYG{+w}{ }\PYG{n}{reversedRemainder}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{reverse}\PYG{p}{(}\PYG{n}{remainder}\PYG{p}{);}\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{3}\PYG{o}{]++}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// returning the reversed rest of String with the first character of the String appended}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{reversedRemainder}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{firstChar}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,50 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{c+c1}{// class to implement method 3 }
\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{class} \PYG{n+nc}{StackVSQueue}\PYG{+w}{ }\PYG{k+kd}{implements}\PYG{+w}{ }\PYG{n}{PalindromeChecker}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// method 3 - using a stack and a queue to do, essentially, what method 2 does (compare the first index to the last index, etc.)}
\PYG{+w}{ }\PYG{n+nd}{@Override}
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kt}{boolean}\PYG{+w}{ }\PYG{n+nf}{checkPalindrome}\PYG{p}{(}\PYG{n}{String}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{ArrayStack}\PYG{+w}{ }\PYG{n}{stack}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{ArrayStack}\PYG{p}{();}\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{2}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{ArrayQueue}\PYG{+w}{ }\PYG{n}{queue}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{ArrayQueue}\PYG{p}{();}\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{2}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// looping through each character in the String and adding the character to the stack & queue }
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{2}\PYG{o}{]++}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{for}\PYG{+w}{ }\PYG{p}{(}\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{\PYGZlt{}}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{length}\PYG{p}{();}\PYG{+w}{ }\PYG{n}{i}\PYG{o}{++}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{2}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{stack}\PYG{p}{.}\PYG{n+na}{push}\PYG{p}{(}\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{charAt}\PYG{p}{(}\PYG{n}{i}\PYG{p}{));}\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{2}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{queue}\PYG{p}{.}\PYG{n+na}{enqueue}\PYG{p}{(}\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{charAt}\PYG{p}{(}\PYG{n}{i}\PYG{p}{));}\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{2}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// looping through each character on the stack & queue and comparing them, returning false if they're different}
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{2}\PYG{o}{]++}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{for}\PYG{+w}{ }\PYG{p}{(}\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{\PYGZlt{}}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{length}\PYG{p}{();}\PYG{+w}{ }\PYG{n}{i}\PYG{o}{++}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{2}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{2}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{!}\PYG{n}{stack}\PYG{p}{.}\PYG{n+na}{pop}\PYG{p}{().}\PYG{n+na}{equals}\PYG{p}{(}\PYG{n}{queue}\PYG{p}{.}\PYG{n+na}{front}\PYG{p}{()))}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{k+kc}{false}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// the complexity of ArrayQueue.dequeue() is 3n+2, where n is the number of items in the queue when dequeue() is called. }
\PYG{+w}{ }\PYG{c+c1}{// we need to determine the number of items in the queue so that we can determine the number of primitive operations performed when queue.dequeue() is called.}
\PYG{+w}{ }\PYG{c+c1}{// to do this, we'll loop through the queue, dequeuing each object and enqueueing it in another ArrayQueue. once complete, we'll reassign the variable queue to point to the new ArrayQueue containing all the objects}
\PYG{+w}{ }\PYG{n}{ArrayQueue}\PYG{+w}{ }\PYG{n}{newQueue}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{ArrayQueue}\PYG{p}{();}\PYG{+w}{ }\PYG{c+c1}{// not counting the operations for this as it's not part of the algorithm, it's part of the operations counting}
\PYG{+w}{ }\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{n}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// n is the number of items in the ArrayQueue when dequeue() is called}
\PYG{+w}{ }\PYG{k}{while}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{!}\PYG{n}{queue}\PYG{p}{.}\PYG{n+na}{isEmpty}\PYG{p}{())}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{newQueue}\PYG{p}{.}\PYG{n+na}{enqueue}\PYG{p}{(}\PYG{n}{queue}\PYG{p}{.}\PYG{n+na}{dequeue}\PYG{p}{());}
\PYG{+w}{ }\PYG{n}{n}\PYG{o}{++}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{queue}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{newQueue}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// setting queue to point to the newQueue, which is just the state that queue would have been in if we didn't do this to calculate the primitive operations}
\PYG{+w}{ }\PYG{n}{newQueue}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k+kc}{null}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// don't need the newQueue object reference anymore}
\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{2}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{3}\PYG{o}{*}\PYG{n}{n}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// complexity of dequeue is 3n+2}
\PYG{+w}{ }\PYG{n}{queue}\PYG{p}{.}\PYG{n+na}{dequeue}\PYG{p}{();}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{k+kc}{true}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,95 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{k+kn}{import}\PYG{+w}{ }\PYG{n+nn}{java.io.*}\PYG{p}{;}
\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{class} \PYG{n+nc}{NewPalindrome}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{static}\PYG{+w}{ }\PYG{k+kt}{long}\PYG{o}{[]}\PYG{+w}{ }\PYG{n}{operations}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{k+kt}{long}\PYG{o}{[}\PYG{l+m+mi}{4}\PYG{o}{]}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// array to contain the global operations count for each method }
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{static}\PYG{+w}{ }\PYG{k+kt}{int}\PYG{o}{[]}\PYG{+w}{ }\PYG{n}{decCount}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{k+kt}{int}\PYG{o}{[}\PYG{l+m+mi}{4}\PYG{o}{]}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// array to hold the count of decimal palindromes found using each method}
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{static}\PYG{+w}{ }\PYG{k+kt}{int}\PYG{o}{[]}\PYG{+w}{ }\PYG{n}{binCount}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{k+kt}{int}\PYG{o}{[}\PYG{l+m+mi}{4}\PYG{o}{]}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// array to hold the count of binary palindromes found using each method}
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{static}\PYG{+w}{ }\PYG{k+kt}{int}\PYG{o}{[]}\PYG{+w}{ }\PYG{n}{bothCount}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{k+kt}{int}\PYG{o}{[}\PYG{l+m+mi}{4}\PYG{o}{]}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// array to hold the count of numbers that are palindromes in both decimal & binary found using each method}
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{static}\PYG{+w}{ }\PYG{k+kt}{long}\PYG{o}{[]}\PYG{+w}{ }\PYG{n}{startTime}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{k+kt}{long}\PYG{o}{[}\PYG{l+m+mi}{4}\PYG{o}{]}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// array to hold the start time of each method's test loop}
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{static}\PYG{+w}{ }\PYG{k+kt}{long}\PYG{o}{[]}\PYG{+w}{ }\PYG{n}{totalTime}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{k+kt}{long}\PYG{o}{[}\PYG{l+m+mi}{4}\PYG{o}{]}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// array to hold the total time of each method's test loop}
\PYG{+w}{ }\PYG{c+c1}{// array to hold all the String versions of the numbers so that they don't have to be generated for each method}
\PYG{+w}{ }\PYG{c+c1}{// 0th column will be decimal, 1st column will be binary}
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{static}\PYG{+w}{ }\PYG{n}{String}\PYG{o}{[][]}\PYG{+w}{ }\PYG{n}{strings}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{String}\PYG{o}{[}\PYG{l+m+mi}{1\PYGZus{}000\PYGZus{}001}\PYG{o}{][}\PYG{l+m+mi}{2}\PYG{o}{]}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// array of StringBuilder objects used to hold the csv data (size of problem, number of operations) for each method}
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{static}\PYG{+w}{ }\PYG{n}{StringBuilder}\PYG{o}{[]}\PYG{+w}{ }\PYG{n}{data}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{StringBuilder}\PYG{o}{[}\PYG{l+m+mi}{4}\PYG{o}{]}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// array of the four classes that will be tested}
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{static}\PYG{+w}{ }\PYG{n}{PalindromeChecker}\PYG{o}{[]}\PYG{+w}{ }\PYG{n}{palindromeCheckers}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{k}{new}\PYG{+w}{ }\PYG{n}{ReverseVSOriginal}\PYG{p}{(),}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{IVersusNMinusI}\PYG{p}{(),}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{StackVSQueue}\PYG{p}{(),}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{RecursiveReverse}\PYG{p}{()\PYGZcb{};}\PYG{+w}{ }
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{static}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{main}\PYG{p}{(}\PYG{n}{String}\PYG{+w}{ }\PYG{n}{args}\PYG{o}{[]}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// initialising the data array to StringBuilder objects}
\PYG{+w}{ }\PYG{k}{for}\PYG{+w}{ }\PYG{p}{(}\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{\PYGZlt{}}\PYG{+w}{ }\PYG{l+m+mi}{4}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{o}{++}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{data}\PYG{o}{[}\PYG{n}{i}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{StringBuilder}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}operations,size\PYGZbs{}n\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// filling up the strings array}
\PYG{+w}{ }\PYG{k}{for}\PYG{+w}{ }\PYG{p}{(}\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{\PYGZlt{}=}\PYG{+w}{ }\PYG{l+m+mi}{1\PYGZus{}000\PYGZus{}000}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{o}{++}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{strings}\PYG{o}{[}\PYG{n}{i}\PYG{o}{][}\PYG{l+m+mi}{0}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{Integer}\PYG{p}{.}\PYG{n+na}{toString}\PYG{p}{(}\PYG{n}{i}\PYG{p}{,}\PYG{+w}{ }\PYG{l+m+mi}{10}\PYG{p}{);}\PYG{+w}{ }\PYG{c+c1}{// converting i to a String base 10}
\PYG{+w}{ }\PYG{n}{strings}\PYG{o}{[}\PYG{n}{i}\PYG{o}{][}\PYG{l+m+mi}{1}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{binary2string}\PYG{p}{(}\PYG{n}{strings}\PYG{o}{[}\PYG{n}{i}\PYG{o}{][}\PYG{l+m+mi}{0}\PYG{o}{]}\PYG{p}{);}\PYG{+w}{ }\PYG{c+c1}{// converting the decimal String to a binary String}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// looping through each PalindromeChecker object in the palindromeCheckers array}
\PYG{+w}{ }\PYG{k}{for}\PYG{+w}{ }\PYG{p}{(}\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{j}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{j}\PYG{+w}{ }\PYG{o}{\PYGZlt{}}\PYG{+w}{ }\PYG{l+m+mi}{4}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{j}\PYG{o}{++}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// getting start time }
\PYG{+w}{ }\PYG{n}{startTime}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{System}\PYG{p}{.}\PYG{n+na}{currentTimeMillis}\PYG{p}{();}\PYG{+w}{ }\PYG{n}{operations}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]++}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// looping through the numbers 0 to 1,000,000 and checking if their binary & decimal representations are palindromic}
\PYG{+w}{ }\PYG{n}{operations}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]++}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{for}\PYG{+w}{ }\PYG{p}{(}\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{\PYGZlt{}=}\PYG{+w}{ }\PYG{l+m+mi}{1\PYGZus{}000\PYGZus{}000}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{i}\PYG{o}{++}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// incrementing the operations count by 2, 1 for the loop condition check and 1 for incrementing i}
\PYG{+w}{ }\PYG{n}{operations}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// converting the number to a decimal or binary String and checking if is a palindrome}
\PYG{+w}{ }\PYG{k+kt}{boolean}\PYG{+w}{ }\PYG{n}{isDecPalindrome}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{palindromeCheckers}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{p}{.}\PYG{n+na}{checkPalindrome}\PYG{p}{(}\PYG{n}{strings}\PYG{o}{[}\PYG{n}{i}\PYG{o}{][}\PYG{l+m+mi}{0}\PYG{o}{]}\PYG{p}{);}\PYG{+w}{ }\PYG{n}{operations}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]++}\PYG{p}{;}
\PYG{+w}{ }\PYG{k+kt}{boolean}\PYG{+w}{ }\PYG{n}{isBinPalindrome}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{palindromeCheckers}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{p}{.}\PYG{n+na}{checkPalindrome}\PYG{p}{(}\PYG{n}{strings}\PYG{o}{[}\PYG{n}{i}\PYG{o}{][}\PYG{l+m+mi}{1}\PYG{o}{]}\PYG{p}{);}\PYG{+w}{ }\PYG{n}{operations}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]++}\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// incrementing the appropriate counter if the number is a palindrome in that base}
\PYG{+w}{ }\PYG{n}{decCount}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{isDecPalindrome}\PYG{+w}{ }\PYG{o}{?}\PYG{+w}{ }\PYG{n}{decCount}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{p}{:}\PYG{+w}{ }\PYG{n}{decCount}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{operations}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// incremnting by 2, 1 for assignment, 1 for condition check}
\PYG{+w}{ }\PYG{n}{binCount}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{isBinPalindrome}\PYG{+w}{ }\PYG{o}{?}\PYG{+w}{ }\PYG{n}{binCount}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{p}{:}\PYG{+w}{ }\PYG{n}{binCount}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{operations}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{bothCount}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{isDecPalindrome}\PYG{+w}{ }\PYG{o}{\PYGZam{}\PYGZam{}}\PYG{+w}{ }\PYG{n}{isBinPalindrome}\PYG{+w}{ }\PYG{o}{?}\PYG{+w}{ }\PYG{n}{bothCount}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{p}{:}\PYG{+w}{ }\PYG{n}{bothCount}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{operations}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// 2 condition checks and one assignment, so incrementing by 3}
\PYG{+w}{ }\PYG{c+c1}{// appending to the data StringBuilder at intervals of 50,000 }
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{i}\PYG{+w}{ }\PYG{o}{\PYGZpc{}}\PYG{+w}{ }\PYG{l+m+mi}{50\PYGZus{}000}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{data}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{p}{.}\PYG{n+na}{append}\PYG{p}{(}\PYG{n}{operations}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{},\PYGZdq{}}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{i}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}\PYGZbs{}n\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// calculating total time taken for method 1 and printing out the results}
\PYG{+w}{ }\PYG{n}{totalTime}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{System}\PYG{p}{.}\PYG{n+na}{currentTimeMillis}\PYG{p}{()}\PYG{+w}{ }\PYG{o}{\PYGZhy{}}\PYG{+w}{ }\PYG{n}{startTime}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{p}{;}\PYG{+w}{ }\PYG{n}{operations}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// incrementing by 2, 1 for getting current time and subtracting start time, 1 for assignment}
\PYG{+w}{ }\PYG{n}{System}\PYG{p}{.}\PYG{n+na}{out}\PYG{p}{.}\PYG{n+na}{println}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Number of decimal palindromes found using Method \PYGZdq{}}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{j}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}: \PYGZdq{}}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{decCount}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{p}{);}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{System}\PYG{p}{.}\PYG{n+na}{out}\PYG{p}{.}\PYG{n+na}{println}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Number of binary palindromes found using Method \PYGZdq{}}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{j}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}: \PYGZdq{}}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{binCount}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{p}{);}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{System}\PYG{p}{.}\PYG{n+na}{out}\PYG{p}{.}\PYG{n+na}{println}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Number of palindromes in both decimal \PYGZam{} binary found using Method \PYGZdq{}}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{j}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}: \PYGZdq{}}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{bothCount}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{p}{);}\PYG{+w}{ }
\PYG{+w}{ }\PYG{n}{System}\PYG{p}{.}\PYG{n+na}{out}\PYG{p}{.}\PYG{n+na}{println}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Number of primitive operations taken in Method \PYGZdq{}}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{j}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}: \PYGZdq{}}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{operations}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{System}\PYG{p}{.}\PYG{n+na}{out}\PYG{p}{.}\PYG{n+na}{println}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Time taken for Method \PYGZdq{}}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{j}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}: \PYGZdq{}}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{totalTime}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{} milliseconds\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{System}\PYG{p}{.}\PYG{n+na}{out}\PYG{p}{.}\PYG{n+na}{println}\PYG{p}{();}
\PYG{+w}{ }\PYG{c+c1}{// outputting the data to separate csv files}
\PYG{+w}{ }\PYG{k}{try}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{String}\PYG{+w}{ }\PYG{n}{filename}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}method\PYGZdq{}}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{j}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}.csv\PYGZdq{}}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{File}\PYG{+w}{ }\PYG{n}{csv}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{File}\PYG{p}{(}\PYG{n}{filename}\PYG{p}{);}
\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// creating file if it doesn't already exist}
\PYG{+w}{ }\PYG{n}{csv}\PYG{p}{.}\PYG{n+na}{createNewFile}\PYG{p}{();}
\PYG{+w}{ }\PYG{n}{FileWriter}\PYG{+w}{ }\PYG{n}{writer}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{k}{new}\PYG{+w}{ }\PYG{n}{FileWriter}\PYG{p}{(}\PYG{n}{filename}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{writer}\PYG{p}{.}\PYG{n+na}{write}\PYG{p}{(}\PYG{n}{data}\PYG{o}{[}\PYG{n}{j}\PYG{o}{]}\PYG{p}{.}\PYG{n+na}{toString}\PYG{p}{());}
\PYG{+w}{ }\PYG{n}{writer}\PYG{p}{.}\PYG{n+na}{close}\PYG{p}{();}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}\PYG{+w}{ }\PYG{k}{catch}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{IOException}\PYG{+w}{ }\PYG{n}{e}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{System}\PYG{p}{.}\PYG{n+na}{out}\PYG{p}{.}\PYG{n+na}{println}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}IO Error occurred\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{e}\PYG{p}{.}\PYG{n+na}{printStackTrace}\PYG{p}{();}
\PYG{+w}{ }\PYG{n}{System}\PYG{p}{.}\PYG{n+na}{exit}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{);}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// utility method to convert a decimal String to its equivalent binary String}
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{static}\PYG{+w}{ }\PYG{n}{String}\PYG{+w}{ }\PYG{n+nf}{binary2string}\PYG{p}{(}\PYG{n}{String}\PYG{+w}{ }\PYG{n}{decimalStr}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{Integer}\PYG{p}{.}\PYG{n+na}{toString}\PYG{p}{(}\PYG{n}{Integer}\PYG{p}{.}\PYG{n+na}{parseInt}\PYG{p}{(}\PYG{n}{decimalStr}\PYG{p}{),}\PYG{+w}{ }\PYG{l+m+mi}{2}\PYG{p}{);}\PYG{+w}{ }\PYG{c+c1}{// parsing the String to an int and then parsing that int to a binary String }
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,102 @@
\makeatletter
\def\PYG@reset{\let\PYG@it=\relax \let\PYG@bf=\relax%
\let\PYG@ul=\relax \let\PYG@tc=\relax%
\let\PYG@bc=\relax \let\PYG@ff=\relax}
\def\PYG@tok#1{\csname PYG@tok@#1\endcsname}
\def\PYG@toks#1+{\ifx\relax#1\empty\else%
\PYG@tok{#1}\expandafter\PYG@toks\fi}
\def\PYG@do#1{\PYG@bc{\PYG@tc{\PYG@ul{%
\PYG@it{\PYG@bf{\PYG@ff{#1}}}}}}}
\def\PYG#1#2{\PYG@reset\PYG@toks#1+\relax+\PYG@do{#2}}
\@namedef{PYG@tok@w}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}}
\@namedef{PYG@tok@c}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@cp}{\def\PYG@tc##1{\textcolor[rgb]{0.61,0.40,0.00}{##1}}}
\@namedef{PYG@tok@k}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kp}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kt}{\def\PYG@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}}
\@namedef{PYG@tok@o}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@ow}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\@namedef{PYG@tok@nb}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@nf}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PYG@tok@nc}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PYG@tok@nn}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PYG@tok@ne}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.80,0.25,0.22}{##1}}}
\@namedef{PYG@tok@nv}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@no}{\def\PYG@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}}
\@namedef{PYG@tok@nl}{\def\PYG@tc##1{\textcolor[rgb]{0.46,0.46,0.00}{##1}}}
\@namedef{PYG@tok@ni}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.44,0.44,0.44}{##1}}}
\@namedef{PYG@tok@na}{\def\PYG@tc##1{\textcolor[rgb]{0.41,0.47,0.13}{##1}}}
\@namedef{PYG@tok@nt}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@nd}{\def\PYG@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\@namedef{PYG@tok@s}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@sd}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@si}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.64,0.35,0.47}{##1}}}
\@namedef{PYG@tok@se}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.67,0.36,0.12}{##1}}}
\@namedef{PYG@tok@sr}{\def\PYG@tc##1{\textcolor[rgb]{0.64,0.35,0.47}{##1}}}
\@namedef{PYG@tok@ss}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@sx}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@m}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@gh}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\@namedef{PYG@tok@gu}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}}
\@namedef{PYG@tok@gd}{\def\PYG@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}}
\@namedef{PYG@tok@gi}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.52,0.00}{##1}}}
\@namedef{PYG@tok@gr}{\def\PYG@tc##1{\textcolor[rgb]{0.89,0.00,0.00}{##1}}}
\@namedef{PYG@tok@ge}{\let\PYG@it=\textit}
\@namedef{PYG@tok@gs}{\let\PYG@bf=\textbf}
\@namedef{PYG@tok@ges}{\let\PYG@bf=\textbf\let\PYG@it=\textit}
\@namedef{PYG@tok@gp}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\@namedef{PYG@tok@go}{\def\PYG@tc##1{\textcolor[rgb]{0.44,0.44,0.44}{##1}}}
\@namedef{PYG@tok@gt}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}}
\@namedef{PYG@tok@err}{\def\PYG@bc##1{{\setlength{\fboxsep}{\string -\fboxrule}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}}}
\@namedef{PYG@tok@kc}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kd}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kn}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kr}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@bp}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@fm}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PYG@tok@vc}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@vg}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@vi}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@vm}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@sa}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@sb}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@sc}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@dl}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@s2}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@sh}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@s1}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@mb}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@mf}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@mh}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@mi}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@il}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@mo}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@ch}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@cm}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@cpf}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@c1}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@cs}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\def\PYGZbs{\char`\\}
\def\PYGZus{\char`\_}
\def\PYGZob{\char`\{}
\def\PYGZcb{\char`\}}
\def\PYGZca{\char`\^}
\def\PYGZam{\char`\&}
\def\PYGZlt{\char`\<}
\def\PYGZgt{\char`\>}
\def\PYGZsh{\char`\#}
\def\PYGZpc{\char`\%}
\def\PYGZdl{\char`\$}
\def\PYGZhy{\char`\-}
\def\PYGZsq{\char`\'}
\def\PYGZdq{\char`\"}
\def\PYGZti{\char`\~}
% for compatibility with earlier versions
\def\PYGZat{@}
\def\PYGZlb{[}
\def\PYGZrb{]}
\makeatother

View File

@ -0,0 +1,11 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{+w}{ }\PYG{c+c1}{// test length function}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{traverse}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{start}\PYG{p}{)\PYGZob{}}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{start}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{while}\PYG{p}{(}\PYG{n}{current}\PYG{+w}{ }\PYG{o}{!=}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)\PYGZob{}}
\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{printFunction}\PYG{p}{(}\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{data}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// printf("%s\n", current->data);}
\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,81 @@
\begin{Verbatim}[commandchars=\\\{\}]
\PYG{c+cp}{\PYGZsh{}include}\PYG{+w}{ }\PYG{c+cpf}{\PYGZlt{}stdio.h\PYGZgt{}}
\PYG{c+cp}{\PYGZsh{}include}\PYG{+w}{ }\PYG{c+cpf}{\PYGZdq{}tests.h\PYGZdq{}}
\PYG{c+cp}{\PYGZsh{}include}\PYG{+w}{ }\PYG{c+cpf}{\PYGZdq{}genericLinkedList.h\PYGZdq{}}
\PYG{c+c1}{// functions to print out different data types}
\PYG{c+c1}{// a more professional design might be to put these in the genericLinkedList header file but i only need these for testing purposes}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{printChar}\PYG{p}{(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}\PYGZpc{}c}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{o}{*}\PYG{p}{(}\PYG{k+kt}{char}\PYG{o}{*}\PYG{p}{)}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{);}
\PYG{p}{\PYGZcb{}}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{printStr}\PYG{p}{(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}\PYGZpc{}s}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{p}{(}\PYG{k+kt}{char}\PYG{o}{*}\PYG{p}{)}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{);}
\PYG{p}{\PYGZcb{}}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{printInt}\PYG{p}{(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}\PYGZpc{}d}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{o}{*}\PYG{p}{(}\PYG{k+kt}{int}\PYG{o}{*}\PYG{p}{)}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{);}
\PYG{p}{\PYGZcb{}}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{runTests}\PYG{p}{()\PYGZob{}}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Tests running...}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{l}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{createEl}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Test String (1).\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{sizeof}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Test String (1).\PYGZdq{}}\PYG{p}{),}\PYG{+w}{ }\PYG{n}{printStr}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{//printf(\PYGZdq{}\PYGZpc{}s\PYGZbs{}n\PYGZpc{}p\PYGZbs{}n\PYGZdq{}, l\PYGZhy{}\PYGZgt{}data, l\PYGZhy{}\PYGZgt{}next);}
\PYG{+w}{ }\PYG{c+c1}{//Test create and traverse}
\PYG{+w}{ }\PYG{n}{traverse}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{//Test insert after}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Testing insertAfter()}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{l2}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{insertAfter}\PYG{p}{(}\PYG{n}{l}\PYG{p}{,}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}another string (2)\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{sizeof}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}another string (2)\PYGZdq{}}\PYG{p}{),}\PYG{+w}{ }\PYG{n}{printStr}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{insertAfter}\PYG{p}{(}\PYG{n}{l2}\PYG{p}{,}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}a final string (3)\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{sizeof}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}a final string (3)\PYGZdq{}}\PYG{p}{),}\PYG{+w}{ }\PYG{n}{printStr}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{traverse}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// test length function}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Testing length()}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{l\PYGZus{}length}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{length}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}The length of l is \PYGZpc{}d}\PYG{l+s+se}{\PYGZbs{}n\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{l\PYGZus{}length}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// test push}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Testing push()}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{push}\PYG{p}{(}\PYG{o}{\PYGZam{}}\PYG{n}{l}\PYG{p}{,}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}yet another test string\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{sizeof}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}yet another test string\PYGZdq{}}\PYG{p}{),}\PYG{+w}{ }\PYG{n}{printStr}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{traverse}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// test pop}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Testing pop()}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{popped}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{pop}\PYG{p}{(}\PYG{o}{\PYGZam{}}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{traverse}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// Test delete after}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Testing deleteAfter()}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{deleteAfter}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{traverse}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// test enqueue}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Testing enqueue()}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{enqueue}\PYG{p}{(}\PYG{o}{\PYGZam{}}\PYG{n}{l}\PYG{p}{,}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}enqueued test string\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{sizeof}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}enqueued test string\PYGZdq{}}\PYG{p}{),}\PYG{+w}{ }\PYG{n}{printStr}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{traverse}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// test dequeue}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Testing dequeue()}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{dequeue}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{traverse}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Testing pushing different data types}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{myint}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{42}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{push}\PYG{p}{(}\PYG{o}{\PYGZam{}}\PYG{n}{l}\PYG{p}{,}\PYG{+w}{ }\PYG{o}{\PYGZam{}}\PYG{n}{myint}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{sizeof}\PYG{p}{(}\PYG{n}{myint}\PYG{p}{),}\PYG{+w}{ }\PYG{n}{printInt}\PYG{p}{);}
\PYG{+w}{ }\PYG{k+kt}{char}\PYG{+w}{ }\PYG{n}{mychar}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+s+sc}{\PYGZsq{}c\PYGZsq{}}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{push}\PYG{p}{(}\PYG{o}{\PYGZam{}}\PYG{n}{l}\PYG{p}{,}\PYG{+w}{ }\PYG{o}{\PYGZam{}}\PYG{n}{mychar}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{sizeof}\PYG{p}{(}\PYG{n}{mychar}\PYG{p}{),}\PYG{+w}{ }\PYG{n}{printChar}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{traverse}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{Tests complete.}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,38 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{+w}{ }\PYG{c+c1}{// test length function}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Testing length()}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{l\PYGZus{}length}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{length}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}The length of l is \PYGZpc{}d}\PYG{l+s+se}{\PYGZbs{}n\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{l\PYGZus{}length}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// test push}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Testing push()}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{push}\PYG{p}{(}\PYG{o}{\PYGZam{}}\PYG{n}{l}\PYG{p}{,}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}yet another test string\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{sizeof}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}yet another test string\PYGZdq{}}\PYG{p}{));}
\PYG{+w}{ }\PYG{n}{traverse}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// test pop}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Testing pop()}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{popped}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{pop}\PYG{p}{(}\PYG{o}{\PYGZam{}}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{traverse}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// Test delete after}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Testing deleteAfter()}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{deleteAfter}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{traverse}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// test enqueue}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Testing enqueue()}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{enqueue}\PYG{p}{(}\PYG{o}{\PYGZam{}}\PYG{n}{l}\PYG{p}{,}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}enqueued test string\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{sizeof}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}enqueued test string\PYGZdq{}}\PYG{p}{));}
\PYG{+w}{ }\PYG{n}{traverse}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// test dequeue}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}Testing dequeue()}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{dequeue}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{traverse}\PYG{p}{(}\PYG{n}{l}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{Tests complete.}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\end{Verbatim}

View File

@ -0,0 +1,37 @@
\begin{Verbatim}[commandchars=\\\{\}]
\PYG{c+cp}{\PYGZsh{}ifndef CT331\PYGZus{}ASSIGNMENT\PYGZus{}LINKED\PYGZus{}LIST}
\PYG{c+cp}{\PYGZsh{}define CT331\PYGZus{}ASSIGNMENT\PYGZus{}LINKED\PYGZus{}LIST}
\PYG{k}{typedef}\PYG{+w}{ }\PYG{k}{struct}\PYG{+w}{ }\PYG{n+nc}{listElementStruct}\PYG{+w}{ }\PYG{n}{listElement}\PYG{p}{;}
\PYG{c+c1}{//Creates a new linked list element with given content of size}
\PYG{c+c1}{//Returns a pointer to the element}
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{createEl}\PYG{p}{(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{printFunction}\PYG{p}{)(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{p}{));}
\PYG{c+c1}{//Prints out each element in the list}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{traverse}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{start}\PYG{p}{);}
\PYG{c+c1}{//Inserts a new element after the given el}
\PYG{c+c1}{//Returns the pointer to the new element}
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{insertAfter}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{after}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{printFunction}\PYG{p}{)(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{p}{));}
\PYG{c+c1}{//Delete the element after the given el}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{deleteAfter}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{after}\PYG{p}{);}
\PYG{c+c1}{// returns the number of elements in the list}
\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n+nf}{length}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{);}
\PYG{c+c1}{// push a new element onto the head of a list and update the list reference using side effects}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{push}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{printFunction}\PYG{p}{)(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{p}{));}
\PYG{c+c1}{// pop an element from the head of a list and update the list reference using side effects}
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{pop}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{);}
\PYG{c+c1}{// enque a new element onto the head of the list and update the list reference using side effects}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{enqueue}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{printFunction}\PYG{p}{)(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{p}{));}\PYG{+w}{ }
\PYG{c+c1}{// dequeue an element from the tail of the list }
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{dequeue}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{);}\PYG{+w}{ }
\PYG{c+cp}{\PYGZsh{}endif}
\end{Verbatim}

View File

@ -0,0 +1,37 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{c+cp}{\PYGZsh{}ifndef CT331_ASSIGNMENT_LINKED_LIST}
\PYG{c+cp}{#define CT331_ASSIGNMENT_LINKED_LIST}
\PYG{k}{typedef}\PYG{+w}{ }\PYG{k}{struct}\PYG{+w}{ }\PYG{n+nc}{listElementStruct}\PYG{+w}{ }\PYG{n}{listElement}\PYG{p}{;}
\PYG{c+c1}{//Creates a new linked list element with given content of size}
\PYG{c+c1}{//Returns a pointer to the element}
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{createEl}\PYG{p}{(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{printFunction}\PYG{p}{)(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{p}{));}
\PYG{c+c1}{//Prints out each element in the list}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{traverse}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{start}\PYG{p}{);}
\PYG{c+c1}{//Inserts a new element after the given el}
\PYG{c+c1}{//Returns the pointer to the new element}
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{insertAfter}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{after}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{printFunction}\PYG{p}{)(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{p}{));}
\PYG{c+c1}{//Delete the element after the given el}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{deleteAfter}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{after}\PYG{p}{);}
\PYG{c+c1}{// returns the number of elements in the list}
\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n+nf}{length}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{);}
\PYG{c+c1}{// push a new element onto the head of a list and update the list reference using side effects}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{push}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{printFunction}\PYG{p}{)(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{p}{));}
\PYG{c+c1}{// pop an element from the head of a list and update the list reference using side effects}
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{pop}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{);}
\PYG{c+c1}{// enque a new element onto the head of the list and update the list reference using side effects}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{enqueue}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{printFunction}\PYG{p}{)(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{p}{));}\PYG{+w}{ }
\PYG{c+c1}{// dequeue an element from the tail of the list }
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{dequeue}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{);}\PYG{+w}{ }
\PYG{c+cp}{\PYGZsh{}endif}
\end{Verbatim}

View File

@ -0,0 +1,3 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\end{Verbatim}

View File

@ -0,0 +1,16 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{c+c1}{// returns the number of elements in the list}
\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n+nf}{length}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{);}
\PYG{c+c1}{// push a new element onto the head of a list and update the list reference using side effects}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{push}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{char}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{);}
\PYG{c+c1}{// pop an element from the head of a list and update the list reference using side effects}
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{pop}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{);}
\PYG{c+c1}{// enque a new element onto the head of the list and update the list reference using side effects}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{enqueue}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{char}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{);}
\PYG{c+c1}{// dequeue an element from the tail of the list}
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{dequeue}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{);}
\end{Verbatim}

View File

@ -0,0 +1,103 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{c+c1}{// returns the number of elements in the list}
\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n+nf}{length}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{length}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// traversing the list and counting each element}
\PYG{+w}{ }\PYG{k}{while}\PYG{p}{(}\PYG{n}{current}\PYG{+w}{ }\PYG{o}{!=}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)\PYGZob{}}
\PYG{+w}{ }\PYG{n}{length}\PYG{o}{++}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{length}\PYG{p}{;}
\PYG{p}{\PYGZcb{}}
\PYG{c+c1}{// push a new element onto the head of a list and update the list reference using side effects}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{push}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{char}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// create the new element}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{newElement}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{createEl}\PYG{p}{(}\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// handle malloc errors}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{newElement}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{fprintf}\PYG{p}{(}\PYG{n}{stderr}\PYG{p}{,}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}Memory allocation failed.}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{exit}\PYG{p}{(}\PYG{n}{EXIT\PYGZus{}FAILURE}\PYG{p}{);}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// make the the new element point to the current head of the list}
\PYG{+w}{ }\PYG{n}{newElement}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{o}{*}\PYG{n}{list}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// make the list reference to point to the new head element}
\PYG{+w}{ }\PYG{o}{*}\PYG{n}{list}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{newElement}\PYG{p}{;}
\PYG{p}{\PYGZcb{}}
\PYG{c+c1}{// pop an element from the head of a list and update the list reference using side effects}
\PYG{c+c1}{// assuming that the desired return value here is the popped element, as is standard for POP operations}
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{pop}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// don't bother if list is non existent}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{list}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{;}\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// getting reference to the element to be popped}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{poppedElement}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{o}{*}\PYG{n}{list}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// make the the second element the new head of the list -- this could be NULL, so the list would be NULL also}
\PYG{+w}{ }\PYG{o}{*}\PYG{n}{list}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{list}\PYG{p}{)}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// detach the popped element from the list}
\PYG{+w}{ }\PYG{n}{poppedElement}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{poppedElement}\PYG{p}{;}
\PYG{p}{\PYGZcb{}}
\PYG{c+c1}{// enque a new element onto the head of the list and update the list reference using side effects}
\PYG{c+c1}{// essentially the same as push}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{enqueue}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{char}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// create the new element}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{newElement}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{createEl}\PYG{p}{(}\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// handle malloc errors}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{newElement}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{fprintf}\PYG{p}{(}\PYG{n}{stderr}\PYG{p}{,}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}Memory allocation failed.}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{exit}\PYG{p}{(}\PYG{n}{EXIT\PYGZus{}FAILURE}\PYG{p}{);}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// make the the new element point to the current head of the list}
\PYG{+w}{ }\PYG{n}{newElement}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{o}{*}\PYG{n}{list}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// make the list reference to point to the new head element}
\PYG{+w}{ }\PYG{o}{*}\PYG{n}{list}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{newElement}\PYG{p}{;}
\PYG{p}{\PYGZcb{}}
\PYG{c+c1}{// dequeue an element from the tail of the list by removing the element from the list via side effects, and returning the removed item}
\PYG{c+c1}{// assuming that we want to return the dequeued element rather than the list itself, as enqueue returns nothing and uses side effects, so dequeue should also use side effects}
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{dequeue}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// there are three cases that we must consider: a list with 0 elements, a list with 1 element, & a list with >=2 elements}
\PYG{+w}{ }\PYG{c+c1}{// don't bother if list is non existent}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{list}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{;}\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// if there is only one element in the list, i.e. the head element is also the tail element, just returning this element}
\PYG{+w}{ }\PYG{c+c1}{// this means that the listElement pointer that was passed to this function won't be updated}
\PYG{+w}{ }\PYG{c+c1}{// ideally, we would set it to NULL but we can't do that since `list` is a pointer that has been passed by value, so we can't update the pointer itself. we would need a pointer to a pointer to have been passed}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{list}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// traversing the list to find the second-to-last element}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{while}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{!=}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// get reference to the element to be dequeued}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{dequeuedElement}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// make the penultimate element the tail by removing reference to the old tail}
\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{;}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,169 @@
\begin{Verbatim}[commandchars=\\\{\}]
\PYG{c+cp}{\PYGZsh{}include}\PYG{+w}{ }\PYG{c+cpf}{\PYGZlt{}stdio.h\PYGZgt{}}
\PYG{c+cp}{\PYGZsh{}include}\PYG{+w}{ }\PYG{c+cpf}{\PYGZlt{}stdlib.h\PYGZgt{}}
\PYG{c+cp}{\PYGZsh{}include}\PYG{+w}{ }\PYG{c+cpf}{\PYGZlt{}string.h\PYGZgt{}}
\PYG{c+cp}{\PYGZsh{}include}\PYG{+w}{ }\PYG{c+cpf}{\PYGZdq{}genericLinkedList.h\PYGZdq{}}
\PYG{k}{typedef}\PYG{+w}{ }\PYG{k}{struct}\PYG{+w}{ }\PYG{n+nc}{listElementStruct}\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{;}
\PYG{+w}{ }\PYG{k+kt}{void}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{printFunction}\PYG{p}{)(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{p}{);}
\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{struct}\PYG{+w}{ }\PYG{n+nc}{listElementStruct}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{next}\PYG{p}{;}
\PYG{p}{\PYGZcb{}}\PYG{+w}{ }\PYG{n}{listElement}\PYG{p}{;}
\PYG{c+c1}{//Creates a new linked list element with given content of size}
\PYG{c+c1}{//Returns a pointer to the element}
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{createEl}\PYG{p}{(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{printFunction}\PYG{p}{)(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{p}{))}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{e}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{malloc}\PYG{p}{(}\PYG{k}{sizeof}\PYG{p}{(}\PYG{n}{listElement}\PYG{p}{));}
\PYG{+w}{ }\PYG{k}{if}\PYG{p}{(}\PYG{n}{e}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{//malloc has had an error}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{//return NULL to indicate an error.}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{dataPointer}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{malloc}\PYG{p}{(}\PYG{k}{sizeof}\PYG{p}{(}\PYG{k+kt}{void}\PYG{p}{)}\PYG{o}{*}\PYG{n}{size}\PYG{p}{);}
\PYG{+w}{ }\PYG{k}{if}\PYG{p}{(}\PYG{n}{dataPointer}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{//malloc has had an error}
\PYG{+w}{ }\PYG{n}{free}\PYG{p}{(}\PYG{n}{e}\PYG{p}{);}\PYG{+w}{ }\PYG{c+c1}{//release the previously allocated memory}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{//return NULL to indicate an error.}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{n}{strcpy}\PYG{p}{(}\PYG{n}{dataPointer}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{e}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{data}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{dataPointer}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{e}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{printFunction}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{printFunction}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{e}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{size}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{e}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{e}\PYG{p}{;}
\PYG{p}{\PYGZcb{}}
\PYG{c+c1}{//Prints out each element in the list}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{traverse}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{start}\PYG{p}{)\PYGZob{}}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{start}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{while}\PYG{p}{(}\PYG{n}{current}\PYG{+w}{ }\PYG{o}{!=}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)\PYGZob{}}
\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{printFunction}\PYG{p}{(}\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{data}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// printf(\PYGZdq{}\PYGZpc{}s\PYGZbs{}n\PYGZdq{}, current\PYGZhy{}\PYGZgt{}data);}
\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{p}{\PYGZcb{}}
\PYG{c+c1}{//Inserts a new element after the given el}
\PYG{c+c1}{//Returns the pointer to the new element}
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{insertAfter}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{el}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{printFunction}\PYG{p}{)(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{p}{))\PYGZob{}}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{newEl}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{createEl}\PYG{p}{(}\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{printFunction}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{next}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{el}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{newEl}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{el}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{newEl}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{newEl}\PYG{p}{;}
\PYG{p}{\PYGZcb{}}
\PYG{c+c1}{//Delete the element after the given el}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{deleteAfter}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{after}\PYG{p}{)\PYGZob{}}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{delete}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{after}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{newNext}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{delete}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{after}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{newNext}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{//need to free the memory because we used malloc}
\PYG{+w}{ }\PYG{n}{free}\PYG{p}{(}\PYG{n}{delete}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{data}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{free}\PYG{p}{(}\PYG{n}{delete}\PYG{p}{);}
\PYG{p}{\PYGZcb{}}
\PYG{c+c1}{// returns the number of elements in the list}
\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n+nf}{length}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{length}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// traversing the list and counting each element}
\PYG{+w}{ }\PYG{k}{while}\PYG{p}{(}\PYG{n}{current}\PYG{+w}{ }\PYG{o}{!=}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)\PYGZob{}}
\PYG{+w}{ }\PYG{n}{length}\PYG{o}{++}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{length}\PYG{p}{;}
\PYG{p}{\PYGZcb{}}
\PYG{c+c1}{// push a new element onto the head of a list and update the list reference using side effects}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{push}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{printFunction}\PYG{p}{)(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{p}{))}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// create the new element}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{newElement}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{createEl}\PYG{p}{(}\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{printFunction}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// handle malloc errors}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{newElement}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{fprintf}\PYG{p}{(}\PYG{n}{stderr}\PYG{p}{,}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}Memory allocation failed.}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{exit}\PYG{p}{(}\PYG{n}{EXIT\PYGZus{}FAILURE}\PYG{p}{);}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// make the the new element point to the current head of the list}
\PYG{+w}{ }\PYG{n}{newElement}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{o}{*}\PYG{n}{list}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// make the list reference to point to the new head element }
\PYG{+w}{ }\PYG{o}{*}\PYG{n}{list}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{newElement}\PYG{p}{;}
\PYG{p}{\PYGZcb{}}
\PYG{c+c1}{// pop an element from the head of a list and update the list reference using side effects}
\PYG{c+c1}{// assuming that the desired return value here is the popped element, as is standard for POP operations}
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{pop}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// don\PYGZsq{}t bother if list is non existent}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{list}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{;}\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{p}{;}\PYG{+w}{ }
\PYG{+w}{ }\PYG{c+c1}{// getting reference to the element to be popped}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{poppedElement}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{o}{*}\PYG{n}{list}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// make the the second element the new head of the list \PYGZhy{}\PYGZhy{} this could be NULL, so the list would be NULL also}
\PYG{+w}{ }\PYG{o}{*}\PYG{n}{list}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{list}\PYG{p}{)}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// detach the popped element from the list}
\PYG{+w}{ }\PYG{n}{poppedElement}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{poppedElement}\PYG{p}{;}
\PYG{p}{\PYGZcb{}}
\PYG{c+c1}{// enque a new element onto the head of the list and update the list reference using side effects}
\PYG{c+c1}{// essentially the same as push}
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{enqueue}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{**}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{size\PYGZus{}t}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{k+kt}{void}\PYG{+w}{ }\PYG{p}{(}\PYG{o}{*}\PYG{n}{printFunction}\PYG{p}{)(}\PYG{k+kt}{void}\PYG{o}{*}\PYG{p}{))}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// create the new element}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{newElement}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{createEl}\PYG{p}{(}\PYG{n}{data}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{size}\PYG{p}{,}\PYG{+w}{ }\PYG{n}{printFunction}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// handle malloc errors}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{newElement}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{fprintf}\PYG{p}{(}\PYG{n}{stderr}\PYG{p}{,}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}Memory allocation failed.}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{exit}\PYG{p}{(}\PYG{n}{EXIT\PYGZus{}FAILURE}\PYG{p}{);}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// make the the new element point to the current head of the list}
\PYG{+w}{ }\PYG{n}{newElement}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{o}{*}\PYG{n}{list}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// make the list reference to point to the new head element }
\PYG{+w}{ }\PYG{o}{*}\PYG{n}{list}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{newElement}\PYG{p}{;}
\PYG{p}{\PYGZcb{}}\PYG{+w}{ }
\PYG{c+c1}{// dequeue an element from the tail of the list by removing the element from the list via side effects, and returning the removed item}
\PYG{c+c1}{// assuming that we want to return the dequeued element rather than the list itself, as enqueue returns nothing and uses side effects, so dequeue should also use side effects}
\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n+nf}{dequeue}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{c+c1}{// there are three cases that we must consider: a list with 0 elements, a list with 1 element, \PYGZam{} a list with \PYGZgt{}=2 elements}
\PYG{+w}{ }\PYG{c+c1}{// don\PYGZsq{}t bother if list is non existent}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{list}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{;}\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// if there is only one element in the list, i.e. the head element is also the tail element, just returning this element}
\PYG{+w}{ }\PYG{c+c1}{// this means that the listElement pointer that was passed to this function won\PYGZsq{}t be updated}
\PYG{+w}{ }\PYG{c+c1}{// ideally, we would set it to NULL but we can\PYGZsq{}t do that since `list` is a pointer that has been passed by value, so we can\PYGZsq{}t update the pointer itself. we would need a pointer to a pointer to have been passed }
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{list}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// traversing the list to find the second\PYGZhy{}to\PYGZhy{}last element}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{while}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{!=}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{c+c1}{// get reference to the element to be dequeued}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{dequeuedElement}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{c+c1}{// make the penultimate element the tail by removing reference to the old tail}
\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{list}\PYG{p}{;}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,17 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{c+cp}{\PYGZsh{}include}\PYG{+w}{ }\PYG{c+cpf}{\PYGZlt{}stdio.h>}
\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n+nf}{main}\PYG{p}{()}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
\PYG{+w}{ }\PYG{k+kt}{int}\PYG{+w}{ }\PYG{n}{my\PYGZus{}int}\PYG{p}{;}
\PYG{+w}{ }\PYG{k+kt}{int}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{my\PYGZus{}int\PYGZus{}pointer}\PYG{p}{;}
\PYG{+w}{ }\PYG{k+kt}{long}\PYG{+w}{ }\PYG{n}{my\PYGZus{}long}\PYG{p}{;}
\PYG{+w}{ }\PYG{k+kt}{double}\PYG{+w}{ }\PYG{o}{*}\PYG{+w}{ }\PYG{n}{my\PYGZus{}double\PYGZus{}pointer}\PYG{p}{;}
\PYG{+w}{ }\PYG{k+kt}{char}\PYG{+w}{ }\PYG{o}{**}\PYG{+w}{ }\PYG{n}{my\PYGZus{}char\PYGZus{}pointer\PYGZus{}pointer}\PYG{p}{;}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}The size of my\PYGZus{}int is \PYGZpc{}lu bytes}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{sizeof}\PYG{p}{(}\PYG{n}{my\PYGZus{}int}\PYG{p}{));}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}The size of my\PYGZus{}int\PYGZus{}pointer is \PYGZpc{}lu bytes}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{sizeof}\PYG{p}{(}\PYG{n}{my\PYGZus{}int\PYGZus{}pointer}\PYG{p}{));}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}The size of my\PYGZus{}long is \PYGZpc{}lu bytes}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{sizeof}\PYG{p}{(}\PYG{n}{my\PYGZus{}long}\PYG{p}{));}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}The size of my\PYGZus{}double\PYGZus{}pointer is \PYGZpc{}lu bytes}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{sizeof}\PYG{p}{(}\PYG{n}{my\PYGZus{}double\PYGZus{}pointer}\PYG{p}{));}
\PYG{+w}{ }\PYG{n}{printf}\PYG{p}{(}\PYG{l+s}{\PYGZdq{}The size of my\PYGZus{}char\PYGZus{}pointer\PYGZus{}pointer is \PYGZpc{}lu bytes}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s}{\PYGZdq{}}\PYG{p}{,}\PYG{+w}{ }\PYG{k}{sizeof}\PYG{p}{(}\PYG{n}{my\PYGZus{}char\PYGZus{}pointer\PYGZus{}pointer}\PYG{p}{));}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,10 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{traverse}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{start}\PYG{p}{)\PYGZob{}}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{start}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{while}\PYG{p}{(}\PYG{n}{current}\PYG{+w}{ }\PYG{o}{!=}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)\PYGZob{}}
\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{printFunction}\PYG{p}{(}\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{data}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// printf(\PYGZdq{}\PYGZpc{}s\PYGZbs{}n\PYGZdq{}, current\PYGZhy{}\PYGZgt{}data);}
\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,10 @@
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{traverse}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{start}\PYG{p}{)\PYGZob{}}
\PYG{+w}{ }\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{start}\PYG{p}{;}
\PYG{+w}{ }\PYG{k}{while}\PYG{p}{(}\PYG{n}{current}\PYG{+w}{ }\PYG{o}{!=}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)\PYGZob{}}
\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{printFunction}\PYG{p}{(}\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{data}\PYG{p}{);}
\PYG{+w}{ }\PYG{c+c1}{// printf("%s\n", current->data);}
\PYG{+w}{ }\PYG{n}{current}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{;}
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,7 @@
\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kt}{void}\PYG{+w}{ }\PYG{n+nf}{reverse\PYGZus{}traverse}\PYG{p}{(}\PYG{n}{listElement}\PYG{o}{*}\PYG{+w}{ }\PYG{n}{current}\PYG{p}{)\PYGZob{}}
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{current}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{n+nb}{NULL}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }\PYG{k}{return}\PYG{p}{;}\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
\PYG{+w}{ }\PYG{n}{reverse\PYGZus{}traverse}\PYG{p}{(}\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{next}\PYG{p}{);}
\PYG{+w}{ }\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{printFunction}\PYG{p}{(}\PYG{n}{current}\PYG{o}{\PYGZhy{}\PYGZgt{}}\PYG{n}{data}\PYG{p}{);}
\PYG{p}{\PYGZcb{}}
\end{Verbatim}

View File

@ -0,0 +1,102 @@
\makeatletter
\def\PYG@reset{\let\PYG@it=\relax \let\PYG@bf=\relax%
\let\PYG@ul=\relax \let\PYG@tc=\relax%
\let\PYG@bc=\relax \let\PYG@ff=\relax}
\def\PYG@tok#1{\csname PYG@tok@#1\endcsname}
\def\PYG@toks#1+{\ifx\relax#1\empty\else%
\PYG@tok{#1}\expandafter\PYG@toks\fi}
\def\PYG@do#1{\PYG@bc{\PYG@tc{\PYG@ul{%
\PYG@it{\PYG@bf{\PYG@ff{#1}}}}}}}
\def\PYG#1#2{\PYG@reset\PYG@toks#1+\relax+\PYG@do{#2}}
\@namedef{PYG@tok@w}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}}
\@namedef{PYG@tok@c}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@cp}{\def\PYG@tc##1{\textcolor[rgb]{0.61,0.40,0.00}{##1}}}
\@namedef{PYG@tok@k}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kp}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kt}{\def\PYG@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}}
\@namedef{PYG@tok@o}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@ow}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\@namedef{PYG@tok@nb}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@nf}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PYG@tok@nc}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PYG@tok@nn}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PYG@tok@ne}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.80,0.25,0.22}{##1}}}
\@namedef{PYG@tok@nv}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@no}{\def\PYG@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}}
\@namedef{PYG@tok@nl}{\def\PYG@tc##1{\textcolor[rgb]{0.46,0.46,0.00}{##1}}}
\@namedef{PYG@tok@ni}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.44,0.44,0.44}{##1}}}
\@namedef{PYG@tok@na}{\def\PYG@tc##1{\textcolor[rgb]{0.41,0.47,0.13}{##1}}}
\@namedef{PYG@tok@nt}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@nd}{\def\PYG@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}}
\@namedef{PYG@tok@s}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@sd}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@si}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.64,0.35,0.47}{##1}}}
\@namedef{PYG@tok@se}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.67,0.36,0.12}{##1}}}
\@namedef{PYG@tok@sr}{\def\PYG@tc##1{\textcolor[rgb]{0.64,0.35,0.47}{##1}}}
\@namedef{PYG@tok@ss}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@sx}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@m}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@gh}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\@namedef{PYG@tok@gu}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}}
\@namedef{PYG@tok@gd}{\def\PYG@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}}
\@namedef{PYG@tok@gi}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.52,0.00}{##1}}}
\@namedef{PYG@tok@gr}{\def\PYG@tc##1{\textcolor[rgb]{0.89,0.00,0.00}{##1}}}
\@namedef{PYG@tok@ge}{\let\PYG@it=\textit}
\@namedef{PYG@tok@gs}{\let\PYG@bf=\textbf}
\@namedef{PYG@tok@ges}{\let\PYG@bf=\textbf\let\PYG@it=\textit}
\@namedef{PYG@tok@gp}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\@namedef{PYG@tok@go}{\def\PYG@tc##1{\textcolor[rgb]{0.44,0.44,0.44}{##1}}}
\@namedef{PYG@tok@gt}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}}
\@namedef{PYG@tok@err}{\def\PYG@bc##1{{\setlength{\fboxsep}{\string -\fboxrule}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}}}
\@namedef{PYG@tok@kc}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kd}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kn}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@kr}{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@bp}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}}
\@namedef{PYG@tok@fm}{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}}
\@namedef{PYG@tok@vc}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@vg}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@vi}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@vm}{\def\PYG@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}}
\@namedef{PYG@tok@sa}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@sb}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@sc}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@dl}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@s2}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@sh}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@s1}{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}}
\@namedef{PYG@tok@mb}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@mf}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@mh}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@mi}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@il}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@mo}{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\@namedef{PYG@tok@ch}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@cm}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@cpf}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@c1}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\@namedef{PYG@tok@cs}{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.24,0.48,0.48}{##1}}}
\def\PYGZbs{\char`\\}
\def\PYGZus{\char`\_}
\def\PYGZob{\char`\{}
\def\PYGZcb{\char`\}}
\def\PYGZca{\char`\^}
\def\PYGZam{\char`\&}
\def\PYGZlt{\char`\<}
\def\PYGZgt{\char`\>}
\def\PYGZsh{\char`\#}
\def\PYGZpc{\char`\%}
\def\PYGZdl{\char`\$}
\def\PYGZhy{\char`\-}
\def\PYGZsq{\char`\'}
\def\PYGZdq{\char`\"}
\def\PYGZti{\char`\~}
% for compatibility with earlier versions
\def\PYGZat{@}
\def\PYGZlb{[}
\def\PYGZrb{]}
\makeatother

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 75 KiB

View File

@ -0,0 +1,13 @@
#include <stdio.h>
int main(int arg, char* argc[]){
printf("Hello assignment1.\n");
int my_integer;
int* my_integer_pointer;
long my_long;
double *my_double_pointer;
char **my_char_pointer_pointer;
return 0;
}

View File

@ -0,0 +1,8 @@
#include <stdio.h>
#include "linkedList.h"
#include "tests.h"
int main(int arg, char* argc[]){
runTests();
return 0;
}

View File

@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedList.h"
typedef struct listElementStruct{
char* data;
size_t size;
struct listElementStruct* next;
} listElement;
//Creates a new linked list element with given content of size
//Returns a pointer to the element
listElement* createEl(char* data, size_t size){
listElement* e = malloc(sizeof(listElement));
if(e == NULL){
//malloc has had an error
return NULL; //return NULL to indicate an error.
}
char* dataPointer = malloc(sizeof(char)*size);
if(dataPointer == NULL){
//malloc has had an error
free(e); //release the previously allocated memory
return NULL; //return NULL to indicate an error.
}
strcpy(dataPointer, data);
e->data = dataPointer;
e->size = size;
e->next = NULL;
return e;
}
//Prints out each element in the list
void traverse(listElement* start){
listElement* current = start;
while(current != NULL){
printf("%s\n", current->data);
current = current->next;
}
}
//Inserts a new element after the given el
//Returns the pointer to the new element
listElement* insertAfter(listElement* el, char* data, size_t size){
listElement* newEl = createEl(data, size);
listElement* next = el->next;
newEl->next = next;
el->next = newEl;
return newEl;
}
//Delete the element after the given el
void deleteAfter(listElement* after){
listElement* delete = after->next;
listElement* newNext = delete->next;
after->next = newNext;
//need to free the memory because we used malloc
free(delete->data);
free(delete);
}

View File

@ -0,0 +1,21 @@
#ifndef CT331_ASSIGNMENT_LINKED_LIST
#define CT331_ASSIGNMENT_LINKED_LIST
typedef struct listElementStruct listElement;
//Creates a new linked list element with given content of size
//Returns a pointer to the element
listElement* createEl(char* data, size_t size);
//Prints out each element in the list
void traverse(listElement* start);
//Inserts a new element after the given el
//Returns the pointer to the new element
listElement* insertAfter(listElement* after, char* data, size_t size);
//Delete the element after the given el
void deleteAfter(listElement* after);
#endif

View File

@ -0,0 +1,25 @@
#include <stdio.h>
#include "tests.h"
#include "linkedList.h"
void runTests(){
printf("Tests running...\n");
listElement* l = createEl("Test String (1).", 30);
//printf("%s\n%p\n", l->data, l->next);
//Test create and traverse
traverse(l);
printf("\n");
//Test insert after
listElement* l2 = insertAfter(l, "another string (2)", 30);
insertAfter(l2, "a final string (3)", 30);
traverse(l);
printf("\n");
// Test delete after
deleteAfter(l);
traverse(l);
printf("\n");
printf("\nTests complete.\n");
}

View File

@ -0,0 +1,6 @@
#ifndef CT331_ASSIGNMENT_TESTS
#define CT331_ASSIGNMENT_TESTS
void runTests();
#endif