Question #69607

I have an assignment that I need to send urgently but I can not. I do not understand linked lists and how to apply them to code. Can you help me?

Write “C” function and draw the flowchart of your algorithm, which constructs the linked list representation of your school number as shown below. This function inputs your school number as an argument and returns the address of the first node of the linked list.
Sample school number: 705102005

Expert's answer

#include <stdio.h>
#include <stdlib.h>
struct Node {
    int value;
    struct Node* next;
};
struct Node* create_list(int number) {
    int digit;
    int rest_digits = number;
    struct Node* head = 0;
    struct Node* new = 0;
    while (rest_digits) {
        digit = rest_digits % 10;
        rest_digits = rest_digits / 10;
        new = malloc(sizeof(struct Node) * 1);
        new->value = digit;
        if (head == 0) new->next = 0;
        else new->next = head;
        head = new;
    }
    return head;
}
void print_list(struct Node* head) {
    struct Node* node = head;
    while (node) {
        printf("[%d]-->", node->value);
        node = node->next;
    }
    printf("XXX");
}
void destroy_list(struct Node* head) {
    struct Node* node = head;
    struct Node* next = 0;
    while (node) {
        next = node->next;
        free(node);
        node = next;
    }
}
int main(void) {
    struct Node* list;
    list = create_list(123456789);
    print_list(list);
    destroy_list(list);
    return 0;
}

Answer:

The flowchart of the code:



Answer provided by AssignmentExpert.com

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS