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...
C# Tip Calculator. I can't figure the code out for this assignment can I get some...
C# Tip Calculator. I can't figure the code out for this assignment can I get some help For this assignment, you'll create a simple tip calculator. Expected program flow: Ask the user for the bill total. Ask the user for the tip percent. Print the final output in the following format when the user enters 10 and 15 for the first two prompts: Total for bill $10.00 with a 15% tip is $11.50 Note that the money values should be...
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?
Method in C language for inserting in descending order in a single linked list. It has...
Method in C language for inserting in descending order in a single linked list. It has a head and tail variable.
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...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT