Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
labb0-single-linked list
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mattias Erlingsson
labb0-single-linked list
Commits
f3e52831
Commit
f3e52831
authored
5 years ago
by
Mattias Erlingsson
Browse files
Options
Downloads
Patches
Plain Diff
changed checking for null, and new test in main
parent
9fc9a384
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
list.c
+22
-40
22 additions, 40 deletions
list.c
main.c
+3
-12
3 additions, 12 deletions
main.c
with
25 additions
and
52 deletions
list.c
+
22
−
40
View file @
f3e52831
...
...
@@ -8,22 +8,15 @@ void append(struct list_item *first, int x){
struct
list_item
*
temp
;
temp
=
(
struct
list_item
*
)
malloc
(
sizeof
(
struct
list_item
));
/*if list is empty, let root point to new item, and item tu NULL*/
if
(
first
->
next
==
NULL
){
first
->
next
=
temp
;
temp
->
value
=
x
;
temp
->
next
=
NULL
;
}
else
{
struct
list_item
*
temp1
=
first
;
//for iteration
/*iterate list until next pointer i NULL.*/
while
(
temp1
->
next
!=
NULL
){
temp1
=
temp1
->
next
;
}
/*temp1->next == NULL, insert and rearrange pointers*/
temp1
->
next
=
temp
;
temp
->
value
=
x
;
temp
->
next
=
NULL
;
struct
list_item
*
temp1
=
first
;
//for iteration
/*iterate list until next pointer i NULL.*/
while
(
temp1
->
next
!=
NULL
){
temp1
=
temp1
->
next
;
}
/*temp1->next == NULL, insert and rearrange pointers*/
temp1
->
next
=
temp
;
temp
->
value
=
x
;
temp
->
next
=
NULL
;
}
void
prepend
(
struct
list_item
*
first
,
int
x
){
...
...
@@ -53,34 +46,24 @@ void input_sorted(struct list_item *first,int x){
struct
list_item
*
temp
;
temp
=
(
struct
list_item
*
)
malloc
(
sizeof
(
struct
list_item
));
/*See if list is empty*/
if
(
first
->
next
==
NULL
){
first
->
next
=
temp
;
temp
->
value
=
x
;
temp
->
next
=
NULL
;
}
else
{
/*iterate until x < y or until last node*/
struct
list_item
*
temp1
=
first
;
while
(
temp1
->
next
!=
NULL
){
/*insert before value higher then x*/
if
(
temp1
->
next
->
value
>
x
){
temp
->
next
=
temp1
->
next
;
temp1
->
next
=
temp
;
temp
->
value
=
x
;
return
;
}
else
{
/*increment pointer*/
temp1
=
temp1
->
next
;
}
/*iterate until x < y or until last node*/
struct
list_item
*
temp1
=
first
;
while
(
temp1
->
next
!=
NULL
){
/*insert before value higher then x*/
if
(
temp1
->
next
->
value
>
x
){
temp
->
next
=
temp1
->
next
;
temp1
->
next
=
temp
;
temp
->
value
=
x
;
return
;
}
else
{
/*increment pointer*/
temp1
=
temp1
->
next
;
}
}
/*nothing bigger so we put it last*/
temp1
->
next
=
temp
;
temp
->
value
=
x
;
temp
->
next
=
NULL
;
}
}
void
clear
(
struct
list_item
*
first
){
...
...
@@ -93,12 +76,11 @@ void clear(struct list_item *first){
return
;
}
else
{
/*iterate until last node*/
while
(
temp
->
next
!=
NULL
)
{
while
(
temp
!=
NULL
)
{
temp1
=
temp
->
next
;
free
(
temp
);
temp
=
temp1
;
}
free
(
temp1
);
//delete last node
first
->
next
=
NULL
;
//make root point to null to restore original property
}
}
This diff is collapsed.
Click to expand it.
main.c
+
3
−
12
View file @
f3e52831
...
...
@@ -7,19 +7,10 @@ void main(int argc, char ** argv) {
root
.
value
=
-
1
;
root
.
next
=
NULL
;
/*append(&root, 5);
append(&root, 1);
append(&root, 4);
append(&root, 16);
prepend(&root, 3);
append(&root, 20);
prepend(&root, 6);
input_sorted
(
&
root
,
1
);
input_sorted
(
&
root
,
15
);
prepend(&root, 10);
append(&root, 20);*/
prepend
(
&
root
,
24
);
input_sorted
(
&
root
,
30
);
//clear(&root);
print
(
&
root
);
clear
(
&
root
);
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment