如何制作一个充满随机数的链表并显示它们?

问题描述 投票:0回答:1

我正在学习如何使用链表。我需要制作一个链表,其中填充 25-75 个随机整数 0-100,然后显示它们。由于某种原因,当我运行程序时,根本没有显示输出,但也没有显示错误。这是我到目前为止的代码:

#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;
  
}

我正在使用类中的代码块示例来实现插入和显示功能,但我无法使其正常工作,而且我不知道该怎么做。输出应该是这样的:

例如。列表为: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、

但是当我运行程序时,控制台上没有输出,我必须退出程序,因为它也不会自行结束。请帮忙!谢谢!

c linked-list
1个回答
0
投票

main 中的 for 循环应该是:

  for(i = 0; i != listcount-1; i++){   /* != instead of = */

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

显示功能正常,但可以删除尾随逗号:

void display(NodePointer current){
  //for empty list
  if(NULL == current){
    printf("The linked list is empty!\n\n");
    return;
  }
  printf("The list is: ");
  printf("%i", current->data);
  //loop through list
  for(current = current->next; current != NULL; current = current->next){
    //display each node
    printf(", %i", current->data);
    //go to next node
  }
  printf("\n\n");
}
© www.soinside.com 2019 - 2024. All rights reserved.