Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ cmake_minimum_required (VERSION 2.8)
project (Examples)

set (CMAKE_CXX_FLAGS "--std=c++11 ${CMAKE_CXX_FLAGS}")

add_subdirectory (DynStructures)
add_subdirectory (Hello)
add_subdirectory (oop)
Expand Down
2 changes: 2 additions & 0 deletions DynStructures/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ add_executable (DynStructures
Data.h
DoubleLinkedList.cpp
DoubleLinkedList.h
List.cpp
List.h
main.cpp
Queue.cpp
Queue.h
Expand Down
143 changes: 143 additions & 0 deletions DynStructures/List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include "List.h"

#include <cstdlib>

struct ListNode* Create(struct Data* listData)
{
struct ListNode* result = (struct ListNode*)malloc(sizeof(struct ListNode));
result->ListData = listData;
result->Next = NULL;
return result;
}

void Destroy(struct ListNode* node)
{
while (node)
{
struct ListNode* p = node;
node = node->Next;
free(p->ListData);
free(p);
}
}

struct ListNode* GetLastNode(struct ListNode* first)
{
if (!first)
{
return NULL;
}
struct ListNode* p = first;
while (p->Next)
{
p = p->Next;
}
return p;
}

struct ListNode* PushBack(struct ListNode* first, struct ListNode* newNode)
{
newNode->Next = NULL;
struct ListNode* lastNode = GetLastNode(first);
if (lastNode)
{
lastNode->Next = newNode;
return first;
}
return newNode;
}

struct ListNode* Insert(struct ListNode* first, struct ListNode* newNode)
{
newNode->Next = NULL;
if (!first)
{
return newNode;
}

struct ListNode* prev = NULL;
struct ListNode* p = first;
while (p)
{
if (CompareByName(newNode->ListData, p->ListData) < 0)
{
if (prev)
{
prev->Next = newNode;
newNode->Next = p;
return first;
}
else
{
newNode->Next = p;
return newNode;
}
}
prev = p;
p = p->Next;
}
prev->Next = newNode;
return first;
}

struct ListNode* Remove(struct ListNode* first, struct ListNode* toDelete)
{
struct ListNode* prev = NULL;
struct ListNode* p = first;
struct ListNode* newFirst = first;
while (p)
{
if (p == toDelete)
{
if (prev)
{
prev->Next = p->Next;
}
else
{
newFirst = p->Next;
}
p->Next = NULL;
break;
}
prev = p;
p = p->Next;
}
return newFirst;
}

struct ListNode* Find(struct ListNode* first, DataCompareFunc compare)
{
struct ListNode* p = first;
while (p)
{
if ((*compare)(p->ListData))
{
return p;
}
p = p->Next;
}
return NULL;
}

int GetCount(struct ListNode* first)
{
int result = 0;
struct ListNode* p = first;
while (p)
{
++result;
p = p->Next;
}
return result;
}

void PrintList(struct ListNode* first)
{
struct ListNode* p = first;
while (p)
{
PrintData(p->ListData);
p = p->Next;
}
}
19 changes: 19 additions & 0 deletions DynStructures/List.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "Data.h"

struct ListNode
{
struct Data* ListData;
struct ListNode* Next;
};

struct ListNode* Create(struct Data* listData);
void Destroy(struct ListNode* node);
struct ListNode* GetLastNode(struct ListNode* first);
struct ListNode* PushBack(struct ListNode* first, struct ListNode* newNode);
struct ListNode* Insert(struct ListNode* first, struct ListNode* newNode);
struct ListNode* Remove(struct ListNode* first, struct ListNode* toDelete);
struct ListNode* Find(struct ListNode* first, DataCompareFunc predicate);
int GetCount(struct ListNode* first);
void PrintList(struct ListNode* first);
5 changes: 4 additions & 1 deletion DynStructures/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include "Data.h"
//#include "List.h"
//#include "Stack.h"
//#include "Queue.h"
#include "DoubleLinkedList.h"
#include "SingleLinkedList.h"
//#include "Stack.h"
//#include "Queue.h"
Expand Down Expand Up @@ -173,6 +177,5 @@ int main()
// TestDoubleLinkedList();

TestSingleLinkedListSort();

return 0;
}