Question

In: Computer Science

This is my C language code. I have some problems with the linked list. I can't...

This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL.

#include

#include

#include

#include

struct node{int data; struct node *next;};

int main()

{

    struct node *head, *current, *temp, *trash;

    srand(time(0));

    int randNumber = rand()%51;

    if(randNumber != 49)

    {

        temp = (struct node*)malloc(sizeof(struct node));

        current = (struct node*)malloc(sizeof(struct node));

        temp->data = randNumber;

        temp->next = NULL;

        head = temp;

        head->next = current = NULL;

        randNumber = rand()%51;

        while(randNumber != 49)

        {

            temp = (struct node*)malloc(sizeof(struct node));

            temp->data = randNumber;

            temp->next = NULL;

            current= temp;

            current = current->next;

            randNumber = rand()%51;

        }

        temp = head;

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

        printf("%d", temp->next->data);

        while (temp != NULL)

        {

            printf("%d\n", temp->data);

            trash = temp;

            temp = temp->next;

            free(trash);

        }

        

        

        

    }

    

    printf("EXIT\n");

    return 0;

}

Solutions

Expert Solution

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

#include <stdio.h>
#include <stdlib.h>
struct node{int data; struct node *next;};

int main()

{

   struct node *head, *current, *temp, *trash;

   srand(time(0));

   int randNumber = rand()%51;

   if(randNumber != 49)

   {
       temp = (struct node*)malloc(sizeof(struct node));

       temp->data = randNumber;

       temp->next = NULL;

       head = temp;

       head->next = NULL;

       current = head;

       randNumber = rand()%51;

       while(randNumber != 49)

       {
           temp = (struct node*)malloc(sizeof(struct node));

           temp->data = randNumber;

           temp->next = NULL;

           current->next= temp;

           current = temp;

           randNumber = rand()%51;
       }

       temp = head;


       while (temp != NULL)

       {

           printf("%d\n", temp->data);

           trash = temp;

           temp = temp->next;

           free(trash);

       }

   }

   printf("EXIT\n");

   return 0;
}


Related Solutions

It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void remove_node(struct linked_list* list, int rm_node_value) (the function to make) This function removes a node with specified value. If there is only one node in the list, remove the node and remove the list also since there is nothing left. While removing a node, the node should be perfectly freed. If the type of list is...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...
It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.) void pop_Stack (struct linked_list* list, int number) //*This is the function to make and below is the explanation that should be written in given code. This function removes some nodes in stack manner; the tail of the list will be removed, repeatedly. The parameter (variable number_of_nodes) means the number of nodes that will be removed. When...
This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
The programming language that is being used here is JAVA, below I have my code that...
The programming language that is being used here is JAVA, below I have my code that is supposed to fulfill the TO-DO's of each segment. This code in particular has not passed 3 specific tests. Below the code, the tests that failed will be written in bold with what was expected and what was outputted. Please correct the mistakes that I seem to be making if you can. Thank you kindly. OverView: For this project, you will develop a game...
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
Practice problems in my statistics textbook I can't find answers for to verify mine: A regular...
Practice problems in my statistics textbook I can't find answers for to verify mine: A regular six-faced fair die will be rolled. Let Y be the number obtained. a.) What is the expected value of Y? b.) What is the variance of Y?
I know this code takes in a set of numbers into a doubly linked list and...
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working? main.c Source Code: #include #include #include "node.h" int main() { struct mynode *head=NULL; int value; printf("Give first value: \n"); scanf("%d",&value); printf("Give next values, and input 0 to end list: \n"); do{ if(value>0){    head = pushNode(head, value);    scanf("%d",&value); } }while (value>0); printf("Before insertion sort: "); printlist(head); head=insertsort(head); printf("After insertion...
Please use C language to code all of the problems below. Please submit a .c file...
Please use C language to code all of the problems below. Please submit a .c file for each of the solutions, that includes the required functions, tests you wrote to check your code and a main function to run the code. Q2. Implement the quick-sort algorithm.
This is the code I have. My problem is my output includes ", 0" at the...
This is the code I have. My problem is my output includes ", 0" at the end and I want to exclude that. // File: main.cpp /*---------- BEGIN - DO NOT EDIT CODE ----------*/ #include <iostream> #include <fstream> #include <sstream> #include <iomanip> using namespace std; using index_t = int; using num_count_t = int; using isConnected_t = bool; using sum_t = int; const int MAX_SIZE = 100; // Global variable to be used to count the recursive calls. int recursiveCount =...
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT