Will have all the singly linked list algorithms I can think of, using the structure I have defined (template based singly linked list) and the simple append function I have added that also acts as the main method of creating an instance.
41 lines
616 B
C++
41 lines
616 B
C++
/* list.cpp
|
|
* Date: 2021-11-20
|
|
* Author: Aryadev Chavali
|
|
*/
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
|
|
template <typename T>
|
|
struct List
|
|
{
|
|
T value;
|
|
struct List<T> *next;
|
|
};
|
|
|
|
template <typename T>
|
|
struct List<T> *append(struct List<T> *lst, T value)
|
|
{
|
|
struct List<T> *node;
|
|
if (lst == NULL)
|
|
{
|
|
node = new List<T>;
|
|
node->value = value;
|
|
node->next = NULL;
|
|
return node;
|
|
}
|
|
|
|
for (node = lst; node->next != NULL; node = node->next)
|
|
continue;
|
|
|
|
node->next = new List<T>;
|
|
node->next->value = value;
|
|
node->next->next = NULL;
|
|
return lst;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
return 0;
|
|
}
|