Skip to content

Commit 900fc07

Browse files
committed
Uploaded source file for linear::single linked list.
1 parent e18bc2c commit 900fc07

File tree

4 files changed

+383
-0
lines changed

4 files changed

+383
-0
lines changed

SingleLinkedList/SingleLinkedList.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31624.102
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SingleLinkedList", "SingleLinkedList\SingleLinkedList.vcxproj", "{6605471D-48ED-489E-995D-EB171A5B6186}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{6605471D-48ED-489E-995D-EB171A5B6186}.Debug|x64.ActiveCfg = Debug|x64
17+
{6605471D-48ED-489E-995D-EB171A5B6186}.Debug|x64.Build.0 = Debug|x64
18+
{6605471D-48ED-489E-995D-EB171A5B6186}.Debug|x86.ActiveCfg = Debug|Win32
19+
{6605471D-48ED-489E-995D-EB171A5B6186}.Debug|x86.Build.0 = Debug|Win32
20+
{6605471D-48ED-489E-995D-EB171A5B6186}.Release|x64.ActiveCfg = Release|x64
21+
{6605471D-48ED-489E-995D-EB171A5B6186}.Release|x64.Build.0 = Release|x64
22+
{6605471D-48ED-489E-995D-EB171A5B6186}.Release|x86.ActiveCfg = Release|Win32
23+
{6605471D-48ED-489E-995D-EB171A5B6186}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {6F20498B-7905-4212-9CB0-BDECB24A44B9}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>16.0</VCProjectVersion>
23+
<Keyword>Win32Proj</Keyword>
24+
<ProjectGuid>{6605471d-48ed-489e-995d-eb171a5b6186}</ProjectGuid>
25+
<RootNamespace>SingleLinkedList</RootNamespace>
26+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>Application</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v142</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>Application</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v142</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>Application</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v142</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>Application</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v142</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
74+
<LinkIncremental>true</LinkIncremental>
75+
</PropertyGroup>
76+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
77+
<LinkIncremental>false</LinkIncremental>
78+
</PropertyGroup>
79+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
80+
<LinkIncremental>true</LinkIncremental>
81+
</PropertyGroup>
82+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
83+
<LinkIncremental>false</LinkIncremental>
84+
</PropertyGroup>
85+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
86+
<ClCompile>
87+
<WarningLevel>Level3</WarningLevel>
88+
<SDLCheck>true</SDLCheck>
89+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
90+
<ConformanceMode>true</ConformanceMode>
91+
</ClCompile>
92+
<Link>
93+
<SubSystem>Console</SubSystem>
94+
<GenerateDebugInformation>true</GenerateDebugInformation>
95+
</Link>
96+
</ItemDefinitionGroup>
97+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
98+
<ClCompile>
99+
<WarningLevel>Level3</WarningLevel>
100+
<FunctionLevelLinking>true</FunctionLevelLinking>
101+
<IntrinsicFunctions>true</IntrinsicFunctions>
102+
<SDLCheck>true</SDLCheck>
103+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
104+
<ConformanceMode>true</ConformanceMode>
105+
</ClCompile>
106+
<Link>
107+
<SubSystem>Console</SubSystem>
108+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
109+
<OptimizeReferences>true</OptimizeReferences>
110+
<GenerateDebugInformation>true</GenerateDebugInformation>
111+
</Link>
112+
</ItemDefinitionGroup>
113+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
114+
<ClCompile>
115+
<WarningLevel>Level3</WarningLevel>
116+
<SDLCheck>true</SDLCheck>
117+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
118+
<ConformanceMode>true</ConformanceMode>
119+
</ClCompile>
120+
<Link>
121+
<SubSystem>Console</SubSystem>
122+
<GenerateDebugInformation>true</GenerateDebugInformation>
123+
</Link>
124+
</ItemDefinitionGroup>
125+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
126+
<ClCompile>
127+
<WarningLevel>Level3</WarningLevel>
128+
<FunctionLevelLinking>true</FunctionLevelLinking>
129+
<IntrinsicFunctions>true</IntrinsicFunctions>
130+
<SDLCheck>true</SDLCheck>
131+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
132+
<ConformanceMode>true</ConformanceMode>
133+
</ClCompile>
134+
<Link>
135+
<SubSystem>Console</SubSystem>
136+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
137+
<OptimizeReferences>true</OptimizeReferences>
138+
<GenerateDebugInformation>true</GenerateDebugInformation>
139+
</Link>
140+
</ItemDefinitionGroup>
141+
<ItemGroup>
142+
<ClInclude Include="src\LinkList.h" />
143+
</ItemGroup>
144+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
145+
<ImportGroup Label="ExtensionTargets">
146+
</ImportGroup>
147+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClInclude Include="src\LinkList.h">
19+
<Filter>Header Files</Filter>
20+
</ClInclude>
21+
</ItemGroup>
22+
</Project>
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*****************************************************************//**
2+
* \file LinkList.h
3+
* \brief Contains definition on (Single) Linked List in C.
4+
*
5+
* \author Xuhua Huang
6+
* \date September 2021
7+
*********************************************************************/
8+
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
12+
/**
13+
* In the following implementation, ListNode refers to the type of a Node;
14+
* LinkList refers to the pointer type pointing ti a linked list.
15+
* LinkList L; ==> ListNode* L;
16+
*/
17+
typedef struct Node
18+
{
19+
public:
20+
int data;
21+
struct Node* next;
22+
}ListNode, *LinkList;
23+
24+
/* Providing basic operation of defined LinkList struct. */
25+
/* Function to initialize linked list. */
26+
void InitLinkList(LinkList* head) {
27+
if ((*head=(LinkList)malloc(sizeof(ListNode))) == NULL)
28+
exit(-1);
29+
(*head)->next = nullptr;
30+
}
31+
32+
/* Function to determine whether the linked list is empty. */
33+
int isLinkListEmpty(const LinkList& head) {
34+
if (head->next == nullptr)
35+
return 1;
36+
else
37+
return 0;
38+
}
39+
40+
/* Function to get element by index. */
41+
ListNode* GetElement(const LinkList& head, int index) {
42+
if (isLinkListEmpty(head))
43+
return nullptr;
44+
else if (index < 1)
45+
return nullptr;
46+
47+
ListNode* p = head;
48+
int j = 0;
49+
// loop through the list and locate the pointer to index position
50+
while (p->next != nullptr && j < index)
51+
{
52+
p = p->next;
53+
j++;
54+
}
55+
// now p has reached the proper position
56+
if (j == index)
57+
return p;
58+
else
59+
return nullptr;
60+
}
61+
62+
/* Function to get element by content, O(n). */
63+
ListNode* LocElement(const LinkList& head, int e) {
64+
ListNode* p = head->next;
65+
while (p)
66+
{
67+
if (p->data != e)
68+
p = p->next;
69+
else
70+
return p;
71+
}
72+
}
73+
74+
/* Function to locate the element by content */
75+
int GetElementPos(LinkList head, int e) {
76+
if (isLinkListEmpty(head))
77+
return 0;
78+
79+
ListNode* p = head->next;
80+
int index = 1;
81+
while (p)
82+
{
83+
if (p->data == e)
84+
return index;
85+
else {
86+
p = p->next;
87+
index++;
88+
}
89+
}
90+
if (!p)
91+
return 0;
92+
}
93+
94+
/* Function to insert an element to the list. */
95+
int InsertList(LinkList head, int index, int e) {
96+
ListNode* previous = head;
97+
ListNode* newNode;
98+
99+
// loop through the list and locate the pointer to index position
100+
int j = 0;
101+
while (previous->next != nullptr && j < index-1)
102+
{
103+
previous = previous->next;
104+
j++;
105+
}
106+
// now pointer `previous` has reached the proper position
107+
108+
// determine the position of j
109+
if (j != index - 1) {
110+
printf("ERROR!Illegal insertion position!\n");
111+
return 0;
112+
}
113+
114+
if ((newNode = (ListNode*)malloc(sizeof(ListNode))) == NULL)
115+
exit(-1);
116+
117+
newNode->data = e;
118+
newNode->next = previous->next;
119+
previous->next = newNode;
120+
return 1;
121+
}
122+
123+
/* Function to delete an element from the list. */
124+
int DeleteFromList(LinkList head, int index, int* e) {
125+
/**
126+
* Delete an element from the linked list.
127+
*
128+
* \param head operable linked list pointer
129+
* \param i index of the element to be deleted
130+
* \param e memory to store the deleted element
131+
* \return 0 for failure, 1 for success, exit(-1) for error
132+
*/
133+
ListNode* previous = head;
134+
ListNode* delNode = nullptr;
135+
136+
// loop through the list and locate the pointer to index position
137+
int j = 0;
138+
while (previous->next
139+
&& previous->next->next
140+
&& j < index - 1)
141+
{
142+
previous = previous->next;
143+
j++;
144+
}
145+
// now pointer `previous` has reached the proper position
146+
147+
// determine the position of j
148+
if (j != index - 1) {
149+
printf("ERROR!Illegal insertion position!\n");
150+
return 0;
151+
}
152+
153+
delNode = previous->next;
154+
*e = (int)(delNode->data);
155+
// disconnect the newNode from the previous node
156+
previous->next = delNode->next;
157+
free(delNode);
158+
return 1;
159+
}
160+
161+
/* Function to determine the length of a list. */
162+
int ListLength(LinkList head) {
163+
ListNode* p = head;
164+
int count = 0;
165+
while (p->next != nullptr)
166+
{
167+
p = p->next;
168+
count++;
169+
}
170+
return count;
171+
}
172+
173+
/* Function to clear a linked list. */
174+
void DestroyLinkList(LinkList head) {
175+
ListNode* p = head;
176+
ListNode* q = nullptr;
177+
while (p->next)
178+
{
179+
q = p;
180+
p = p->next;
181+
free(q);
182+
}
183+
}

0 commit comments

Comments
 (0)