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
1eed34dd
Commit
1eed34dd
authored
Sep 23, 2020
by
Liam E. Roeth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add search_before, delete_node; fix traverse_before, delete_node_at
main and test not updated yet
parent
81a41a5c
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
110 additions
and
49 deletions
+110
-49
llist.c
llist.c
+108
-47
llist.h
llist.h
+2
-2
No files found.
llist.c
View file @
1eed34dd
...
...
@@ -63,6 +63,39 @@ NODE_INT search(NODE *head, datatype data){
return
curr
;
}
NODE_INT
search_before
(
NODE
*
head
,
datatype
data
,
unsigned
before
){
//return:
// node non-null, num>=0 : normal; found data at index[num+before]
// node NULL, num 0 : head is NULL
// node NULL, num<0 : found data before enough items processed. num is index.
//--------------------e.g. search_before({1,2,3},2,3) -> (NULL, -2)
// node NULL, num>0 : did not find data; returns index of final NULL
//--------------------e.g. search_before({1,2,3},4,3) -> (NULL, 3)
NODE_INT
out
;
out
.
node
=
NULL
;
out
.
num
=
0
;
if
(
head
==
NULL
)
return
out
;
if
(
before
==
0
)
return
search
(
head
,
data
);
before
;
NODE
**
queue
=
calloc
(
before
,
sizeof
(
NODE
*
));
int
i
=
0
;
while
(
head
!=
NULL
&&
head
->
data
!=
data
){
queue
[
i
]
=
head
;
i
=
(
i
+
1
)
%
before
;
out
.
num
++
;
head
=
head
->
next
;
}
if
(
head
==
NULL
){
out
.
node
=
NULL
;
return
out
;
}
out
.
node
=
queue
[
i
];
out
.
num
-=
before
;
return
out
;
}
NODE
*
construct
(
datatype
data
,
NODE
*
next
){
//return:
// pointer to new node : normal
...
...
@@ -98,59 +131,39 @@ NODE_INT traverse(NODE *head, int pos){
return
out
;
}
NODE_INT
search_before
(
NODE
*
head
,
datatype
data
,
unsigned
before
){
//NODE_INT traverse_before(NODE *head, int pos, int offset){
// int newPos = pos-offset;
// NODE_INT returnCode = traverse(head, newPos);
// switch(returnCode.num){
// case 0: printf("Case 0-> here is the data: %d\n", returnCode.node->data);
// break;
// case 1: printf("Case 1-> here is the data: %d\n",returnCode.node->data);
// break;
// case 2: printf("Not in the list; returning final data point: %d\n",returnCode.node->data);
// break;
// }
//}
NODE_INT
traverse_before
(
NODE
*
head
,
int
pos
,
unsigned
before
){
//return:
//
node non-null, num>-1: normal; found data at index num+before
//
node NULL, num 0 : head
is NULL
//
node NULL, num<0 : completed before enough data processed. num is index.
//
e.g. search_before({1,2,3},2,3) -> (NULL, -2)
//
node non-null, num<0 : did not find data; returns node 'before' before last
// if n is index of node returned, num is -(n+1)
//
0 : normal
//
1 : head is null; NODE* return
is NULL
//
2 : reached end of list; NODE* return is final item
//
3 : reached end of list, but before put result back in list: NODE* is result
//
4 : before is larger than pos or pos is negative; NODE* return is NULL
int
effectivePos
=
pos
-
before
;
NODE_INT
out
;
out
.
node
=
NULL
;
out
.
num
=
0
;
if
(
head
==
NULL
)
return
out
;
if
(
before
==
0
)
return
search
(
head
,
data
);
before
;
NODE
**
queue
=
calloc
(
before
,
sizeof
(
NODE
*
));
int
i
=
0
;
while
(
head
!=
NULL
&&
head
->
data
!=
data
){
queue
[
i
]
=
head
;
i
=
(
i
+
1
)
%
before
;
out
.
num
++
;
head
=
head
->
next
;
out
.
node
=
NULL
;
out
.
num
=
4
;
if
(
effectivePos
>=
0
){
out
=
traverse
(
head
,
effectivePos
);
if
(
out
.
num
==
0
)
out
.
num
=
length
(
out
.
node
)
>
before
?
0
:
3
;
}
out
.
node
=
queue
[
i
];
out
.
num
-=
before
;
if
(
head
==
NULL
)
out
.
num
=
(
-
1
)
-
out
.
num
;
return
out
;
}
NODE_INT
traverseBefore
(
NODE
*
head
,
int
pos
,
int
offset
){
int
newPos
=
pos
-
offset
;
NODE_INT
returnCode
=
traverse
(
head
,
newPos
);
switch
(
returnCode
.
num
){
case
0
:
printf
(
"Case 0-> here is the data: %d
\n
"
,
returnCode
.
node
->
data
);
break
;
case
1
:
printf
(
"Case 1-> here is the data: %d
\n
"
,
returnCode
.
node
->
data
);
break
;
case
2
:
printf
(
"Not in the list; returning final data point: %d
\n
"
,
returnCode
.
node
->
data
);
break
;
}
}
int
delete_node_at
(
NODE
*
head
,
int
pos
){
NODE_INT
nodeToDelete
=
traverse
(
head
,
pos
);
NODE_INT
priorNode
=
traverse
(
head
,
pos
-
1
);
priorNode
.
node
->
next
=
nodeToDelete
.
node
->
next
;
//NODE *nextNode = nodeToDelete.node->next;
free
(
nodeToDelete
.
node
);
return
0
;
}
int
delete_node_after
(
NODE
*
head
){
//return:
// 0 : normal
...
...
@@ -171,6 +184,54 @@ void delete_list(NODE *head){
delete_list
(
tmp
);
}
int
delete_node_at
(
NODE
*
head
,
int
pos
){
//return:
// 0 : normal
// 1 : head is NULL
// 2 : head->next is NULL: this function will not free head
// 3 : pos is beyond end of list
if
(
head
==
NULL
)
return
1
;
if
(
head
->
next
==
NULL
)
return
2
;
if
(
pos
==
0
){
head
->
data
=
head
->
next
->
data
;
delete_node_after
(
head
);
return
0
;
}
NODE_INT
priorNode
=
traverse_before
(
head
,
pos
,
1
);
if
(
priorNode
.
num
==
2
||
priorNode
.
num
==
3
)
return
3
;
NODE
*
tmp
=
priorNode
.
node
->
next
;
priorNode
.
node
->
next
=
tmp
->
next
;
free
(
tmp
);
return
0
;
}
int
delete_node
(
NODE
*
head
,
datatype
data
){
//return:
// 0 : normal
// 1 : head is NULL
// 2 : data is sole item in list: this function will not free head
// -1 : data not found
NODE_INT
result
=
search_before
(
head
,
data
,
1
);
if
(
result
.
node
==
NULL
){
if
(
result
.
num
>
0
)
//not found
return
-
1
;
if
(
result
.
num
==
0
)
//head is null
return
1
;
if
(
head
->
next
==
NULL
)
//data is sole item in list
return
2
;
//data is first item: preserve address of head while deleting it
head
->
data
=
head
->
next
->
data
;
delete_node_after
(
head
);
return
0
;
}
//data is somewhere in the list
delete_node_after
(
result
.
node
);
return
0
;
}
NODE
*
construct_list
(
datatype
list
[],
int
size
){
//return:
// pointer to head : normal
...
...
llist.h
View file @
1eed34dd
...
...
@@ -20,11 +20,11 @@ NODE_INT search(NODE *head, datatype data);
NODE_INT
search_before
(
NODE
*
head
,
datatype
data
,
unsigned
before
);
NODE
*
construct
(
datatype
data
,
NODE
*
next
);
NODE_INT
traverse
(
NODE
*
head
,
int
pos
);
NODE_INT
traverse
Before
(
NODE
*
head
,
int
pos
,
int
offset
);
NODE_INT
traverse
_before
(
NODE
*
head
,
int
pos
,
unsigned
before
);
int
delete_node_after
(
NODE
*
head
);
void
delete_list
(
NODE
*
head
);
int
delete_node_at
(
NODE
*
head
,
int
pos
);
//
int delete_node(NODE *head, datatype data);
int
delete_node
(
NODE
*
head
,
datatype
data
);
NODE
*
construct_list
(
datatype
list
[],
int
size
);
int
add_to_end
(
NODE
*
head
,
datatype
new_data
);
int
insert_node
(
NODE
*
head
,
int
pos
,
datatype
new_data
);
...
...
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