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
79a374e7
Commit
79a374e7
authored
Sep 23, 2020
by
Liam E. Roeth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix search, add simple makefile and readme
parent
a423667b
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
45 additions
and
11 deletions
+45
-11
README.txt
README.txt
+5
-0
llist.c
llist.c
+12
-6
makefile
makefile
+15
-0
test.c
test.c
+13
-5
No files found.
README.txt
0 → 100644
View file @
79a374e7
interactive UI:
$ make
noninteractive test of each function:
$ make test
llist.c
View file @
79a374e7
...
@@ -41,6 +41,7 @@ void print_list(NODE *head){
...
@@ -41,6 +41,7 @@ void print_list(NODE *head){
}
}
NODE_INT
psearch
(
NODE
*
head
,
datatype
data
){
NODE_INT
psearch
(
NODE
*
head
,
datatype
data
){
//search for a value. prints updates
//return:
//return:
// positive : index where data was found; NODE* return is pointer to where
// positive : index where data was found; NODE* return is pointer to where
// -1 : not found; NODE* return is NULL
// -1 : not found; NODE* return is NULL
...
@@ -54,9 +55,11 @@ NODE_INT psearch(NODE *head, datatype data){
...
@@ -54,9 +55,11 @@ NODE_INT psearch(NODE *head, datatype data){
}
}
NODE_INT
search
(
NODE
*
head
,
datatype
data
){
NODE_INT
search
(
NODE
*
head
,
datatype
data
){
//searches for a value.
//return:
//return:
// positive : index where data was found; NODE* return is pointer to where
// NODE* is NULL : not found; int is index of last null
// -1 : not found; NODE* return is NULL
// NODE* is non-null : int is index where data was found;
//---------------------NODE* return is pointer to where
NODE_INT
curr
;
NODE_INT
curr
;
curr
.
node
=
head
;
curr
.
node
=
head
;
int
i
;
int
i
;
...
@@ -68,11 +71,13 @@ NODE_INT search(NODE *head, datatype data){
...
@@ -68,11 +71,13 @@ NODE_INT search(NODE *head, datatype data){
curr
.
node
=
curr
.
node
->
next
;
curr
.
node
=
curr
.
node
->
next
;
}
}
curr
.
node
=
NULL
;
curr
.
node
=
NULL
;
curr
.
num
=
-
1
;
curr
.
num
=
i
;
return
curr
;
return
curr
;
}
}
NODE_INT
search_before
(
NODE
*
head
,
datatype
data
,
unsigned
before
){
NODE_INT
search_before
(
NODE
*
head
,
datatype
data
,
unsigned
before
){
//searches for a value, then returns the item 'before' before it.
//only traverses the list once.
//return:
//return:
// node non-null, num>=0 : normal; found data at index[num+before]
// node non-null, num>=0 : normal; found data at index[num+before]
// node NULL, num 0 : head is NULL
// node NULL, num 0 : head is NULL
...
@@ -85,8 +90,9 @@ NODE_INT search_before(NODE *head, datatype data, unsigned before){
...
@@ -85,8 +90,9 @@ NODE_INT search_before(NODE *head, datatype data, unsigned before){
out
.
num
=
0
;
out
.
num
=
0
;
if
(
head
==
NULL
)
if
(
head
==
NULL
)
return
out
;
return
out
;
if
(
before
==
0
)
if
(
before
==
0
)
{
return
search
(
head
,
data
);
return
search
(
head
,
data
);
}
before
;
before
;
NODE
**
queue
=
calloc
(
before
,
sizeof
(
NODE
*
));
NODE
**
queue
=
calloc
(
before
,
sizeof
(
NODE
*
));
int
i
=
0
;
int
i
=
0
;
...
@@ -208,11 +214,11 @@ int delete_node(NODE *head, datatype data){
...
@@ -208,11 +214,11 @@ int delete_node(NODE *head, datatype data){
// 0 : normal
// 0 : normal
// 1 : head is NULL
// 1 : head is NULL
// 2 : data is sole item in list: this function will not free head
// 2 : data is sole item in list: this function will not free head
//
-1
: data not found
//
4
: data not found
NODE_INT
result
=
search_before
(
head
,
data
,
1
);
NODE_INT
result
=
search_before
(
head
,
data
,
1
);
if
(
result
.
node
==
NULL
){
if
(
result
.
node
==
NULL
){
if
(
result
.
num
>
0
)
//not found
if
(
result
.
num
>
0
)
//not found
return
-
1
;
return
4
;
if
(
result
.
num
==
0
)
//head is null
if
(
result
.
num
==
0
)
//head is null
return
1
;
return
1
;
if
(
head
->
next
==
NULL
)
//data is sole item in list
if
(
head
->
next
==
NULL
)
//data is sole item in list
...
...
makefile
0 → 100644
View file @
79a374e7
run
:
main.out
./main.out
.PHONY
:
run all test
test.out
:
llist.c llist.h test.c
gcc
-o
test.out llist.c test.c
main.out
:
llist.c llist.h main.c
gcc
-o
main.out llist.c main.c
all
:
test.out main.out
test
:
test.out
./test.out
test.c
View file @
79a374e7
...
@@ -23,20 +23,28 @@ int main(){
...
@@ -23,20 +23,28 @@ int main(){
printf
(
"I found it at index %d.
\n
"
,
target
.
num
);
printf
(
"I found it at index %d.
\n
"
,
target
.
num
);
fputs
(
"The number I found there was: "
,
stdout
);
fputs
(
"The number I found there was: "
,
stdout
);
print_node
(
target
.
node
);
print_node
(
target
.
node
);
puts
(
"Deleting the element at index 3..."
);
target
=
traverse
(
root
,
3
);
target
=
traverse
(
root
,
3
);
fputs
(
"At index 3 there is: "
,
stdout
);
print_node
(
target
.
node
);
puts
(
"Deleting the element at index 3..."
);
delete_node_at
(
root
,
3
);
delete_node_at
(
root
,
3
);
puts
(
"Now it's garbage:"
);
fputs
(
"Now it's garbage: "
,
stdout
);
print_node
(
target
.
node
);
print_node
(
target
.
node
);
puts
(
"Deleting the first occurrence of
1
..."
);
puts
(
"Deleting the first occurrence of
5
..."
);
target
=
search
(
root
,
5
);
target
=
search
(
root
,
5
);
delete_node
(
root
,
5
);
delete_node
(
root
,
5
);
puts
(
"Now it's garbage:"
);
fputs
(
"Now it's garbage: "
,
stdout
);
print_node
(
target
.
node
);
print_node
(
target
.
node
);
print_list
(
root
);
print_list
(
root
);
puts
(
"Deleting the first item by index,"
);
delete_node_at
(
root
,
0
);
puts
(
"and the new first item by value..."
);
delete_node
(
root
,
3
);
printf
(
"The head is still at %p.
\n
"
,
root
);
print_list
(
root
);
puts
(
"Deleting list..."
);
puts
(
"Deleting list..."
);
delete_list
(
root
);
delete_list
(
root
);
puts
(
"Now it's garbage:"
);
puts
(
"Now it's garbage
(or at least probably not data)
:"
);
print_list
(
root
);
print_list
(
root
);
return
0
;
return
0
;
...
...
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