Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a C code for linked list which asks for input, print it and exit, i got

ID: 3737619 • Letter: T

Question

This is a C code for linked list which asks for input, print it and exit, i got this code from this web, but it crashes when asked to print, can you please help me fix it. I know the problem is in the insert node and it is suppose to link next cell to the previous. thank you

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct int_node {
    int data;
    struct int_node *next;
} int_node;

//creates a new node and put the data
int_node* addNode(int m) {
    int_node* newNode = (int_node*)malloc(sizeof(int_node));
    newNode->data = m;
    newNode->next = NULL;
    return newNode;
}

// this method insert the data and returns the head of the new list
int_node* insert(int m, int_node* head) {
    int_node* temp = head;
    int_node* newNode = addNode(m);
    if(head == NULL) {
        return newNode;
    }
    while(temp->next != NULL)
        temp = temp->next;
     temp->next = newNode;
    return head;
}

//list the problem is here
void print(int_node* head) {
    int_node* temp = head;

    while(temp != NULL) {
    printf("trial2, " );
        printf("%d ",temp->data);
        temp = temp->next;
    }
    printf(" ");
}

int main(void) {
        // users choice
        char choice[50] = "";

        int_node *head;
        int n;

        // while loop for users input
        while(strcmp(choice, "exit") != 0) {
            printf("Operation to be performed (Insert, Print or Exit): ");
            scanf("%s", choice);

            if(strcmp(choice, "insert") == 0) {
                printf("Enter a number:");
                scanf("%d", &n);
                head = n; //insert(n, head); // something going on here
            } else if(strcmp(choice, "print") == 0) {
            printf("trial");
                printf(head);
            }
        }

        // clear list
        int_node *a = head;
        int_node *b;
        while(a != NULL) {
                b = a;
                a = a->next;
                free(b);
        }
}

Explanation / Answer

/* C Program to remove duplicates from a sorted linked list */

#include <stdio.h>

#include <stdlib.h>

/* Link list node */

typedef struct int_node {

int data;

struct int_node *next;

} int_node;

/* UTILITY FUNCTIONS */

/* Function to insert a node at the beginging of the linked list */

void push(struct int_node **head_ref, int new_data) {

/* allocate node */

struct int_node *new_node =

(struct int_node *)malloc(sizeof(struct int_node));

/* put in the data */

new_node->data = new_data;

/* link the old list off the new node */

new_node->next = (*head_ref);

/* move the head to point to the new node */

(*head_ref) = new_node;

}

/* Function to print nodes in a given linked list */

void printList(struct int_node *node) {

while (node != NULL) {

printf(" %d ", node->data);

node = node->next;

}

}

/* Drier program to test above functions*/

int main() {

/* Start with the empty list */

struct int_node *head = NULL;

int n, ch;

// while loop for users input

do {

printf("Operation to be performed (Insert 0, Print 1 or Exit 2 or "

"greater ): ");

scanf("%d", &ch);

if (ch == 0) {

printf("Enter a number:");

scanf("%d", &n);

push(&head, n);

} else if (ch == 1) {

printf("trial ");

printList(head);

}

} while (ch < 2);

// clear list

int_node *a = head;

int_node *b;

while (a != NULL) {

b = a;

a = a->next;

free(b);

}

}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote