Structure handling Complete the code below that (a) creates a node struct, (b) stores a number, (c) appends to the list pointed by the head pointer passed as a parameter, and (d) returns the new head pointer. struct node { }; /* yp is the head pointers of a linked list that may be empty. z is an integer that needs to be stored in a struct. */ struct node *append(struct node *yp, int z) { if int x; struct node *next; else { } while 6 10 ; ; /* declare a new pointer */ ; /* create a new struct */ ;/* store z in the newaly created struct */ /* set the new struct pointer to NULL "/ ; /* if the list is not empty */ ; /* set head pointer to new pointer */ / if the list is not empty "/ _(_7 )______________8____; /* go the last node */ ____;?* attach the bew pointer to list */ /* return the head pointer */

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Concept explainers
Question

please complate code in  fill in the blanks 

 

### Structure Handling: Linked Lists

Complete the code below that (a) creates a node struct, (b) stores a number, (c) appends to the list pointed by the head pointer passed as a parameter, and (d) returns the new head pointer.

```c
struct node {
    int x;
    struct node *next;
};
/* yp is the head pointer of a linked list that may be empty.
   z is an integer that needs to be stored in a struct. */
struct node *append(struct node *yp, int z) {
    __________1__________; /* declare a new pointer */
    __________2__________; /* create a new struct */
    __________3__________; /* store z in the newly created struct */
    __________4__________; /* set the new struct pointer to NULL */

    if (_________5_______) { /* if the list is not empty */
        __________6__________; /* set head pointer to new pointer */
    } else { /* if the list is not empty */
        while (___7___ != ____8_____) { /* go to the last node */
            __________9__________; /* attach the new pointer to the list */
        }
    }

    __________10__________; /* return the head pointer */
}
```

This snippet outlines the steps needed to manipulate a singly linked list by appending a new node to it. Each numbered underscore comments indicate where you should fill in the blanks with the correct C code to complete the function. Below are clues for each blank to guide you in filling out the code:

1. Declare a new pointer for the structure node.
2. Use `malloc` to allocate memory for this new node.
3. Store the integer `z` in the freshly allocated node structure.
4. Initialize the `next` pointer of this node to `NULL`, preparing it for the end of the list.
5. Check if the list is empty by verifying if `yp` is `NULL`.
6. Assign the head pointer to this new node if the list was empty.
7. Iterate through the list using a temporary pointer until you reach the last node.
8. Proceed with traversal until you reach a node where `next` is `NULL`.
9. Link the new node to the end of the list by updating the `next` of the last node.
10. Finally, return the head pointer of the list, whether it is the newly added node or the original head if the list was not
Transcribed Image Text:### Structure Handling: Linked Lists Complete the code below that (a) creates a node struct, (b) stores a number, (c) appends to the list pointed by the head pointer passed as a parameter, and (d) returns the new head pointer. ```c struct node { int x; struct node *next; }; /* yp is the head pointer of a linked list that may be empty. z is an integer that needs to be stored in a struct. */ struct node *append(struct node *yp, int z) { __________1__________; /* declare a new pointer */ __________2__________; /* create a new struct */ __________3__________; /* store z in the newly created struct */ __________4__________; /* set the new struct pointer to NULL */ if (_________5_______) { /* if the list is not empty */ __________6__________; /* set head pointer to new pointer */ } else { /* if the list is not empty */ while (___7___ != ____8_____) { /* go to the last node */ __________9__________; /* attach the new pointer to the list */ } } __________10__________; /* return the head pointer */ } ``` This snippet outlines the steps needed to manipulate a singly linked list by appending a new node to it. Each numbered underscore comments indicate where you should fill in the blanks with the correct C code to complete the function. Below are clues for each blank to guide you in filling out the code: 1. Declare a new pointer for the structure node. 2. Use `malloc` to allocate memory for this new node. 3. Store the integer `z` in the freshly allocated node structure. 4. Initialize the `next` pointer of this node to `NULL`, preparing it for the end of the list. 5. Check if the list is empty by verifying if `yp` is `NULL`. 6. Assign the head pointer to this new node if the list was empty. 7. Iterate through the list using a temporary pointer until you reach the last node. 8. Proceed with traversal until you reach a node where `next` is `NULL`. 9. Link the new node to the end of the list by updating the `next` of the last node. 10. Finally, return the head pointer of the list, whether it is the newly added node or the original head if the list was not
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Types of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education