1. [15 pts] Please draw a figure to show the structure of nodes following the example in the slides. [Answer] 5-> -> 2. [15 pts] Please fill the following blanks with reasonable values. We assume that the address of nodes [0] is 0x200, and the size for both integer and pointer is 4 bytes, for example, the address for nodes [1] is 0x204, nodes [2] is 0x208. [Answer] (1) nodes [2].value =2 (2) nodes [3]. next->value = (3) &nodes [4] = Exercise 1 Function and Structure [30 pts] Please debug the following program and answer the following questions. There is a cycle in a linked list if some node in the list can be reached again by continuously following the next pointer. #include typedef struct node { int value; struct node *next; } node; int ll_has_cycle (node *first) if (first NULL) return 0; node *head = first%;B while (head->next != NULL) { } if (head first) { return 1; } head = head->next;B return 0; void test_11_has_cycle() { int i; node nodes [6] ; for (i = 0; i < 6; i++) { nodes [i] .next = NULL; nodes [i].value i; nodes [0] .next = &nodes [1]; nodes [1] .next = &nodes [2]; nodes [2] .next &nodes [3]; nodes [3] .next nodes [4] .next & nodes [4]; NULL; nodes [5] .next &nodes [0]; printf("1. Checking first list for cycles. \n Function 11_has_cycle says it has %s cycle\n\n", 11_has_cycle (&nodes[0])?"a":"no"); printf("2. Checking length-zero list for cycles. \n Function 11_has_cycle says it has %s cycle\n\n", 11_has_cycle (NULL)?"a": "no"); } printf("3. nodes [%d] value is: %d\n", 2, nodes [0].value); int main(void) } test_11_has_cycle (); return 0;

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 18PE
icon
Related questions
Question
1. [15 pts] Please draw a figure to show the structure of nodes following the example in the slides.
[Answer]
5-> ->
2. [15 pts] Please fill the following blanks with reasonable values. We assume that the address of
nodes [0] is 0x200, and the size for both integer and pointer is 4 bytes, for example, the address for
nodes [1] is 0x204, nodes [2] is 0x208.
[Answer]
(1) nodes [2].value =2
(2) nodes [3]. next->value =
(3) &nodes [4]
=
Transcribed Image Text:1. [15 pts] Please draw a figure to show the structure of nodes following the example in the slides. [Answer] 5-> -> 2. [15 pts] Please fill the following blanks with reasonable values. We assume that the address of nodes [0] is 0x200, and the size for both integer and pointer is 4 bytes, for example, the address for nodes [1] is 0x204, nodes [2] is 0x208. [Answer] (1) nodes [2].value =2 (2) nodes [3]. next->value = (3) &nodes [4] =
Exercise 1 Function and Structure [30 pts]
Please debug the following program and answer the following questions. There is a cycle in a linked
list if some node in the list can be reached again by continuously following the next pointer.
#include <stdio.h>
typedef struct node {
int value;
struct node *next;
} node;
int ll_has_cycle (node *first)
if (first
NULL) return 0;
node *head = first%;B
while (head->next != NULL) {
}
if (head
first) {
return 1; }
head =
head->next;B
return 0;
void test_11_has_cycle() {
int i;
node nodes [6] ;
for (i =
0; i < 6; i++) {
nodes [i] .next = NULL;
nodes [i].value
i;
nodes [0] .next
=
&nodes [1];
nodes [1] .next
=
&nodes [2];
nodes [2] .next
&nodes [3];
nodes [3] .next
nodes [4] .next
& nodes [4];
NULL;
nodes [5] .next
&nodes [0];
printf("1. Checking first list for cycles. \n Function 11_has_cycle says it
has %s cycle\n\n", 11_has_cycle (&nodes[0])?"a":"no");
printf("2. Checking length-zero list for cycles. \n Function 11_has_cycle
says it has %s cycle\n\n", 11_has_cycle (NULL)?"a": "no");
}
printf("3. nodes [%d] value is: %d\n", 2, nodes [0].value);
int main(void)
}
test_11_has_cycle ();
return 0;
Transcribed Image Text:Exercise 1 Function and Structure [30 pts] Please debug the following program and answer the following questions. There is a cycle in a linked list if some node in the list can be reached again by continuously following the next pointer. #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle (node *first) if (first NULL) return 0; node *head = first%;B while (head->next != NULL) { } if (head first) { return 1; } head = head->next;B return 0; void test_11_has_cycle() { int i; node nodes [6] ; for (i = 0; i < 6; i++) { nodes [i] .next = NULL; nodes [i].value i; nodes [0] .next = &nodes [1]; nodes [1] .next = &nodes [2]; nodes [2] .next &nodes [3]; nodes [3] .next nodes [4] .next & nodes [4]; NULL; nodes [5] .next &nodes [0]; printf("1. Checking first list for cycles. \n Function 11_has_cycle says it has %s cycle\n\n", 11_has_cycle (&nodes[0])?"a":"no"); printf("2. Checking length-zero list for cycles. \n Function 11_has_cycle says it has %s cycle\n\n", 11_has_cycle (NULL)?"a": "no"); } printf("3. nodes [%d] value is: %d\n", 2, nodes [0].value); int main(void) } test_11_has_cycle (); return 0;
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT