Search code examples
clinked-list

How do I make a linked list filled with random numbers and display them?


I am learning how to use linked lists. I need to make a linked list that is filled with 25-75 random integers 0-100 and then display them. For some reason when I run my program no output shows at all but no error is shown either. This is my code so far:

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

//Self Referential Structure                                                 
struct node{
  int data;
  struct node *next;
};

//Type Definitions                                                             
typedef struct node Node;
typedef struct node* NodePointer;

//Function Prototypes
void insert(int, NodePointer *);
void display(NodePointer);

//Insert Function
void insert(int num2, NodePointer *head2){
  //pointer to a new node to be inserted into linked list
  NodePointer newNode = NULL;
  //pointer to the previous node in the linked list
  NodePointer previous = NULL;
  //pointer to the current node in the linked list
  NodePointer current = *head2;

  //create a node on the heap
  newNode = malloc(sizeof(Node));
  /*
check to see if space is available
if no space on heap, malloc() will return NULL
  */
  if(NULL != newNode){
    //assign num to the node
    newNode->data = num2;
    //figure out where to insert in linked list
    while(NULL != current && num2 <= current->data){
      //move previous to current
      previous = current;
      //move current to next node
      current = current->next;
    }//end of while
    //insert at beginning of linked list
    if(NULL == previous){
      newNode->next = current;
      //change the address stored in head
      *head2 = newNode; 
    }//end of if
    else{
      //insert between previous and current
      previous->next = newNode;
      newNode->next = current;
    }//end of else
  }//end of if
}//end of function

//Display Function
void display(NodePointer current){
  //for empty list
  if(NULL == current){
    printf("The linked list is empty!\n\n");
    return;
  }
  printf("The list is: ");
  //loop through list
  while(NULL != current){
    //display each node
    printf("%i, ", current->data);
    //go to next node
    current = current->next;
  }
  printf("\n\n");
}

//MAIN FUNCTION
int main(){
  int i = 0; //Looping variable
  int listcount = 0;
  int num = 0;

  NodePointer head = NULL; //Stores pointer to 1st node

  srand(time(NULL)); //Seed
  listcount = rand() % (75 + 1 - 25) + 25;
  
  //Loop to fill linked list
  for(i = 0; i = listcount-1; i++){
    num = rand() % 100;
    insert(num, &head);
  }

  //Display List
  display(head);

  return 0;
  
}

I am using the code block examples from my class for the insert and display functions but it I cannot get it to work right and I am not sure what to do. This is what the output should look like:

Ex. The list is: 0, 0, 1, 4, 5, 6, 8, 9, 12, 13, 14, 17, 19, 20, 20, 23, 24, 25, 25, 26, 28, 30, 30, 32, 34, 36, 37, 38, 43, 44, 46, 50, 53, 54, 54, 62, 62, 65, 66, 66, 68, 70, 70, 74, 79, 79, 80, 82, 83, 85, 85, 89, 96, 97,

But when I run my program there is no output on the consol and I have to exit the program as it will not end by itself either. Please help! Thank you!


Solution

  • Jabberwocky's comment shows one of the errors.

    The for loop in main could also be:

      for(i = 0; i < listcount; i++){
    

    The other error is in insert(), which is inserting in reverse order. To fix it:

        while(NULL != current && num2 >= current->data){   /* not <= */
    

    The display function is working, but the trailing comma can be removed:

    void display(NodePointer current){
      //for empty list
      if(NULL == current){
        printf("The linked list is empty!\n\n");
        return;
      }
      printf("The list is: ");
      //display first node
      printf("%i", current->data);
      //loop through rest of list
      for(current = current->next; current != NULL; current = current->next){
        //display each node
        printf(", %i", current->data);
      }
      printf("\n\n");
    }
    

    Eventually you'll want a function to delete the list that frees every node in the list and then sets the head to NULL. You may want a function that removes and frees every node equal to some specified data value.