Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
L
linked_list2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Liam E. Roeth
linked_list2
Commits
749998c9
Commit
749998c9
authored
Sep 17, 2020
by
Liam E. Roeth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update llist.c
parent
b730c2ff
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
131 additions
and
0 deletions
+131
-0
llist.c
llist.c
+131
-0
No files found.
llist.c
View file @
749998c9
#include<stdio.h>
#include<stdlib.h>
#include "llist.h"
void
print_node
(
NODE
*
head
,
int
pos
){
NODE
*
currentNode
;
currentNode
=
head
;
for
(
int
i
=
0
;
i
<
pos
;
i
++
){
currentNode
=
currentNode
->
next
;
}
printf
(
"The data at index %d is: %d"
,
pos
,
currentNode
->
data
);
}
void
print_list
(
NODE
*
head
){
printf
(
"Printing list...
\n
"
);
NODE
*
currentNode
;
currentNode
=
head
;
int
i
=
0
;
while
(
currentNode
->
data
!=
NULL
){
printf
(
"Index: %d; Data: %d
\n
"
,
i
,
currentNode
->
data
);
currentNode
=
currentNode
->
next
;
i
++
;
}
}
NODE
*
search
(
int
n
,
NODE
*
head
){
printf
(
"Searching list for: %d"
,
n
);
NODE
*
currentNode
;
currentNode
=
head
;
while
(
currentNode
->
data
!=
NULL
){
if
(
currentNode
->
data
==
n
){
printf
(
"Location found, index: %d."
,
currentNode
);
return
currentNode
;
}
currentNode
=
currentNode
->
next
;
}
printf
(
"Not found in this list."
);
}
NODE
*
construct
(
datatype
data
,
NODE
*
next
){
NODE
*
out
=
malloc
(
sizeof
(
NODE
));
if
(
out
!=
NULL
){
out
->
data
=
data
;
out
->
next
=
next
;
}
return
out
;
}
NODE
*
traverse
(
NODE
*
head
,
int
pos
){
if
(
head
==
NULL
)
return
NULL
;
int
i
;
for
(
i
=
0
;
i
<
pos
||
pos
<
0
;
i
++
){
if
(
head
->
next
!=
NULL
)
head
=
head
->
next
;
else
break
;
}
return
head
;
}
int
delete_node_after
(
NODE
*
head
){
if
(
head
==
NULL
||
head
->
next
==
NULL
)
return
1
;
NODE
*
tmp
=
head
->
next
;
head
->
next
=
head
->
next
->
next
;
free
(
tmp
);
return
0
;
}
NODE
*
construct_list
(
datatype
list
[],
int
size
){
if
(
size
<=
0
)
return
NULL
;
NODE
*
next
=
construct
(
list
[
size
-
1
],
NULL
);
NODE
*
head
;
int
i
;
for
(
i
=
size
-
2
;
i
>=
0
;
i
--
){
head
=
next
;
next
=
construct
(
list
[
i
],
head
);
if
(
next
==
NULL
)
Delete_List
(
head
);
}
return
next
;
}
int
add_to_end
(
NODE
*
head
,
datatype
new_data
){
return
Insert_Node
(
head
,
-
1
,
new_data
);
}
int
Insert_Node
(
NODE
*
head
,
int
pos
,
datatype
new_data
){
if
(
head
==
NULL
)
return
1
;
if
(
pos
!=
0
)
return
Insert_Node_After
(
traverse
(
head
,
pos
-
1
),
new_data
);
else
{
int
out
=
Insert_Node_After
(
head
,
head
->
data
);
if
(
out
>
0
)
return
out
;
head
->
data
=
new_data
;
}
return
0
;
}
int
Insert_Node_After
(
NODE
*
head
,
datatype
data
){
if
(
head
==
NULL
)
return
1
;
NODE
*
new
=
construct
(
data
,
head
->
next
);
if
(
new
==
NULL
)
return
2
;
head
->
next
=
new
;
return
0
;
}
int
Length
(
NODE
*
head
){
if
(
head
==
NULL
)
return
0
;
int
i
=
1
;
while
((
head
=
head
->
next
)
!=
NULL
)
i
++
;
return
i
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment