Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
EECS373Homework3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
jattisha
EECS373Homework3
Commits
bc778685
Commit
bc778685
authored
8 years ago
by
jattisha
Browse files
Options
Downloads
Patches
Plain Diff
add list.c
parents
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
list.c
+60
-0
60 additions, 0 deletions
list.c
with
60 additions
and
0 deletions
list.c
0 → 100644
+
60
−
0
View file @
bc778685
#include
<assert.h>
#include
<stdlib.h>
#include
"list.h"
// Takes a valid, sorted list starting at `head` and adds the element
// `new_element` to the list. The list is sorted based on the value of the
// `index` member in ascending order. Returns a pointer to the head of the list
// after the insertion of the new element.
list_t
*
insert_sorted
(
list_t
*
head
,
list_t
*
new_element
)
{
assert
(
head
!=
NULL
);
assert
(
new_element
!=
NULL
);
list_t
*
before
=
head
,
*
after
=
head
->
next
;
if
(
before
->
index
>
new_element
->
index
)
{
new_element
->
next
=
head
;
head
=
new_element
;
return
head
;
}
while
(
after
!=
NULL
&&
after
->
index
<
new_element
->
index
)
{
before
=
after
;
after
=
after
->
next
;
if
(
after
==
NULL
)
break
;
}
before
->
next
=
new_element
;
new_element
->
next
=
after
;
return
head
;
}
// Reverses the order of the list starting at `head` and returns a pointer to
// the resulting list. You do not need to preserve the original list.
list_t
*
reverse
(
list_t
*
head
)
{
assert
(
head
!=
NULL
);
list_t
*
before
=
head
,
*
middle
=
head
->
next
,
*
after
;
if
(
middle
==
NULL
)
{
return
head
;
}
after
=
middle
->
next
;
head
->
next
=
NULL
;
while
(
after
!=
NULL
)
{
middle
->
next
=
before
;
before
=
middle
;
middle
=
after
;
after
=
after
->
next
;
}
middle
->
next
=
before
;
head
=
middle
;
return
head
;
}
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