0
Follow
0
View

How to solve the problem of linked list?

panchaoyu1 注册会员
2023-02-26 17:19

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > in your code, insertnode function creates a new node, but did not set a new node data, every time to insert a new node, The data field of the new node is random, so the program will always output an infinite number of random numbers. Also, the list insertion logic in the insertnode function is problematic.

Here is the modified code:

#include
#include

typedef struct node
{
    int data;
    struct node* next;   //指向下一个节点的指针
}NODE;

NODE* creatNode(int data)
{
    NODE* newnode = (NODE*)malloc(sizeof(NODE));
    newnode->data = data;
    newnode->next = NULL;
    return newnode;
}

NODE* creatList()    //创建新结点或说创建链表
{
    NODE* head = creatNode(0);
    return head;
}

void insertNode(NODE* headnode, int data)  //插入结点,参数:所要插入的链表,所要插入的结点的数据
{
    NODE* newnode = creatNode(data);    //创建插入的结点
    newnode->next = headnode->next;    //让newnode指向headnode的下一个结点
    headnode->next = newnode;          //headnode指向newnode
}

void printList(NODE * headnode)    //打印链表
{
    NODE * pmove= headnode->next;
    while (pmove)
    {
        printf("%d ", pmove->data);
        pmove = pmove->next;
    }
    printf("\n");
}

int main() 
{
    NODE* head = creatList();
    insertNode(head, 1);
    insertNode(head, 2);
    insertNode(head, 3);
    printList(head);
    return 0;
}


The main contents of

modification include:

  1. changed the data domain word of the node from date to data.
  2. Modifies the function creatNode that creates the node and sets the data field of the node.
  3. modified the function creatList that creates the linked list to make the data field of the header node 0, making the data field of the header node meaningless.
  4. modifies the insertNode function by setting the next pointer to the new node, ensuring that the inserted node is placed first in the list.
  5. modified the function printList, which prints a linked list, to correctly print the node's data field instead of printing the address.

This will print the elements of the linked list correctly.

fyb3526642 注册会员
2023-02-26 17:19

The following answers are quoted from GPT-3 model, please use them wisely:

#include
#include
typedef struct node
{
    int date;
    struct node* next;   //指向下一个节点的指针
}NODE;
NODE* head = NULL;  //定义头指针
NODE* creatlist();
NODE* creatNode();
void printflist(NODE* headnode);
void insertnode(NODE* headnode, int date);

#include"sti.h"
int main() 
{
    NODE* list = creatlist();
    insertnode(list, 1);
    insertnode(list, 2);
    insertnode(list, 3);
    printflist(list);
    return 0;
}
NODE* creatNode(int date)
{
    NODE* newnode = (NODE*)malloc(sizeof(NODE));
    newnode->date = date;
    newnode->next = NULL;
    return newnode;
};
NODE* creatlist()    //创建新结点或说创建链表
{
    NODE* newnode = (NODE*)malloc(sizeof(NODE));   //创建新结点
    newnode->next = NULL;     //让newnode这个结点指向空
    return newnode;
}
void insertnode(NODE* headnode, int date)  //插入结点,参数:所要插入的链表,所要插入的结点的数据
{
  NODE * curr = NULL;
  NODE* newnode = creatNode(date);    //创建插入的结点
  curr = headnode;
  while(curr->next != NULL)  // 找到链表最后一个结点
    {
        curr = curr->next;
    }
  curr->next = newnode; //将newnode链接到链表的尾部
  newnode->next = NULL;
}
void printflist(NODE * headnode)    //打印结点
{
    NODE * pmove= headnode->next;
    while (pmove !=NULL)
        {
        printf("%d", pmove->date);
        pmove = pmove->next;
        }
    printf("\n");
}

Please accept my answer if it answers your question

About the Author

Question Info

Publish Time
2023-02-26 17:19
Update Time
2023-02-26 17:19