(list)+list.cpp file
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.
This commit is contained in:
40
list.cpp
Normal file
40
list.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
/* 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;
|
||||
}
|
||||
Reference in New Issue
Block a user