Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swen90006-a2-2019
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Li Zixuan
swen90006-a2-2019
Commits
63176d77
Commit
63176d77
authored
Sep 5, 2019
by
Toby Murray
Browse files
Options
Downloads
Patches
Plain Diff
list_pop
parent
531eb385
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/passbook.c
+33
-12
33 additions, 12 deletions
src/passbook.c
with
33 additions
and
12 deletions
src/passbook.c
+
33
−
12
View file @
63176d77
...
...
@@ -192,12 +192,14 @@ char inst[INSTRUCTION_LENGTH];
/* password mapping for each url: initially empty */
node_t
*
map
=
NULL
;
/* a list of node pointers -- the idea is to use these to implement a node
stack that we can then use to traverse the tree iteratively, avoiding
recursion. We need tree traversal e.g. when saving the passbook to a file */
/* a doubly-linked list of node pointers
is used to implement stacks/queues of nodes so we can implement various
tree traversal algorithms without using recursion (to avoid stack overflow
for very large trees). */
typedef
struct
nodeptr_list
{
const
node_t
*
p
;
struct
nodeptr_list
*
next
;
struct
nodeptr_list
*
prev
;
}
nodeptr_list_t
;
...
...
@@ -206,26 +208,47 @@ nodeptr_list_t * list_push(nodeptr_list_t *lst, const node_t *p){
assert
(
n
!=
NULL
&&
"push: malloc failed"
);
n
->
p
=
p
;
n
->
next
=
lst
;
if
(
lst
!=
NULL
){
lst
->
prev
=
n
;
}
n
->
prev
=
NULL
;
return
n
;
}
/* when out is non-NULL we place a pointer to the first node into it */
nodeptr_list_t
*
list_pop
(
nodeptr_list_t
*
lst
,
const
node_t
**
out
){
if
(
lst
!=
NULL
){
if
(
out
!=
NULL
){
*
out
=
lst
->
p
;
}
nodeptr_list_t
*
ret
=
lst
->
next
;
free
(
lst
);
return
ret
;
}
else
{
if
(
out
!=
NULL
){
*
out
=
(
const
node_t
*
)
NULL
;
}
return
lst
;
}
}
void
list_destroy
(
nodeptr_list_t
*
lst
){
while
(
lst
!=
NULL
){
nodeptr_list_t
*
next
=
lst
->
next
;
free
(
lst
);
lst
=
next
;
lst
=
list_pop
(
lst
,
NULL
);
}
}
void
list_print
(
nodeptr_list_t
*
lst
){
printf
(
"
\n
Begin list print:
\n
"
);
while
(
lst
!=
NULL
){
node_print
(
lst
->
p
);
lst
=
lst
->
next
;
const
node_t
*
p
;
lst
=
list_pop
(
lst
,
&
p
);
node_print
(
p
);
}
printf
(
"End list print.
\n\n
"
);
}
void
print_all
(
const
node_t
*
p
){
nodeptr_list_t
*
lst
=
NULL
;
if
(
p
!=
NULL
){
...
...
@@ -239,10 +262,8 @@ void print_all(const node_t *p){
}
// pop from the stack to simulate the return
const
node_t
*
q
=
lst
->
p
;
nodeptr_list_t
*
temp
=
lst
;
lst
=
lst
->
next
;
free
(
temp
);
const
node_t
*
q
;
lst
=
list_pop
(
lst
,
&
q
);
// print the node following the return
node_print
(
q
);
...
...
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