按升序在单链表中插入元素。 #包括 #包括 类型定义结构节点 { int数据; 结构节点*链接; }名单; 列表*头=空; 无效插入(整数) { printf(“num:%d”,num); 结构节点*temp,*r; 温度=收割台; r=malloc(sizeof(struct node)); r->data=num; if(temp==NULL | | temp->data>num) { r->link=temp; 标题=r; } 其他的 { while(temp!=NULL) { 如果(临时->数据链接->数据->数量)); { r->link=temp->link; 温度->链接=r; 返回; } 温度=温度->链接; } } } 无效显示() { 结构节点*temp; 温度=收割台; while(temp!=NULL) { printf(“%d”,临时->数据); 温度=温度->链接; } } 空干管() { 插入(10); 插入(5); 插入(17); 插入(8); 插入(23); 插入(78); 显示(); }

按升序在单链表中插入元素。 #包括 #包括 类型定义结构节点 { int数据; 结构节点*链接; }名单; 列表*头=空; 无效插入(整数) { printf(“num:%d”,num); 结构节点*temp,*r; 温度=收割台; r=malloc(sizeof(struct node)); r->data=num; if(temp==NULL | | temp->data>num) { r->link=temp; 标题=r; } 其他的 { while(temp!=NULL) { 如果(临时->数据链接->数据->数量)); { r->link=temp->link; 温度->链接=r; 返回; } 温度=温度->链接; } } } 无效显示() { 结构节点*temp; 温度=收割台; while(temp!=NULL) { printf(“%d”,临时->数据); 温度=温度->链接; } } 空干管() { 插入(10); 插入(5); 插入(17); 插入(8); 插入(23); 插入(78); 显示(); },c,linked-list,C,Linked List,在这里,我尝试以升序插入元素。令我惊讶的是,我得到的输出是5 78 23 8 17 10,这是不正确的,有人能看看这个吗?非常感谢您的帮助。为了在链表中按升序插入节点,您必须解决以下问题- 1.列表为空:当列表中没有元素时 2.开始时插入:当r->datadata时,需要将其插入列表的最开始处。 3.在末尾插入:当r->data大于列表中具有最大值的节点时,需要将其插入到最末尾(假设我们在这里进行排序插入),正如用户@keltar所指出的那样。 4.中间插入:当r->data>header->d

在这里,我尝试以升序插入元素。令我惊讶的是,我得到的输出是5 78 23 8 17 10,这是不正确的,有人能看看这个吗?非常感谢您的帮助。

为了在链表中按升序插入节点,您必须解决以下问题-
1.列表为空:当列表中没有元素时
2.开始时插入:当
r->datadata
时,需要将其插入列表的最开始处。
3.在末尾插入:当
r->data
大于列表中具有最大值的节点时,需要将其插入到最末尾(假设我们在这里进行排序插入),正如用户
@keltar
所指出的那样。
4.中间插入:当
r->data>header->data
小于列表中最大的元素时,需要将其插入到header和最后一个节点之间的某个位置

排序插入的简单实现如下所示-

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

typedef struct node
{
    int data;
    struct node *link;
}list;

list *header =NULL;

void insert(int num)
{
    printf("num:%d ", num);
    struct node *temp, *r;
    temp = header;

    r = malloc(sizeof(struct node));
    r -> data = num;

    if(temp == NULL ||temp-> data > num )
    {
        r-> link = temp ;
        header = r;
    }
    else 
    {
        while(temp !=NULL)
        {
            if(temp -> data <= num && (temp->link->data > num));
            {
                r -> link = temp -> link;
                temp->link=r;
                return;
            }
            temp = temp -> link;
        }           
    }
}

void display()
{
    struct node *temp;
    temp = header;

    while(temp != NULL)
    {
        printf("%d ", temp->data);
        temp = temp->link;
    }
}

void main( )
{
    insert(10);
    insert(5);
    insert(17);
    insert(8);
    insert(23);
    insert(78);

    display();
}
#包括
#包括
#定义第13节
结构节点{
int数据;
结构节点*下一步;
};
/*
*print_list()-打印列表的状态
*/
无效打印列表(结构节点*头)
{
结构节点*tmp=head;
while(tmp){
printf(“%d”,tmp->data);
tmp=tmp->next;
}
printf(“\n\n”);
}
/*
*insert_node_sorted()-将节点插入列表
*按顺序
*/
结构节点*插入节点\排序(结构节点*头,整数数据)
{
结构节点*tmp;
结构节点*e_ptr;
结构节点*新节点=malloc(sizeof(结构节点));
如果(!新建_节点)
出口(0);
新建_节点->数据=数据;
新建节点->下一步=空;
/*当列表为空时*/
如果(!头)
返回新的_节点;
否则{
/*初始化tmp&e\u ptr*/
tmp=头部;
e_ptr=头部;
/*使e_ptr指向列表中最大的元素,即最后一个元素*/
while(e_ptr->next)
e_ptr=e_ptr->next;
/*如果要插入的元素小于头部节点*/
if(头部->数据>新建节点->数据){
新建节点->下一步=头部;
head=新的_节点;
}否则,如果(新建节点->数据>e_ptr->数据){/*要插入的数据大于列表中最大的元素*/
e_ptr->next=新建_节点;
}否则{/*新节点应放置在head和e_ptr之间的某个位置*/
同时(tmp->next&&tmp->next->data<新建节点->数据)
tmp=tmp->next;
新建_节点->下一步=tmp->下一步;
tmp->next=新建_节点;
}
}
回流头;
}
/*
*驱动函数
*/
int main(int argc,字符**argv)
{
结构节点*head=NULL;
int i;
/*填充列表*/
对于(i=0;i
如果
错误,则在
后面加分号。但您还有许多其他逻辑错误。谢谢您的及时回复。请告诉我逻辑错误,好吗?如果最后一个节点小于新节点-您仍然希望插入新节点,但没有相应的代码。0)main()应返回int 1)您忘记在malloc之后将->下一个指针设置为NULL。2) 通过使用指向指针的指针,可以将insert函数减少到大约五行。
#include <stdio.h>
#include <stdlib.h>

#define LEN 13

struct node {
    int data;
    struct node *next;
};
/*
 * print_list() - To print the state of the list
 */
void print_list(struct node *head)
{
    struct node *tmp = head;
    while (tmp) {
        printf(" %d ", tmp->data);
        tmp = tmp->next;
    }
    printf("\n\n");
}
/*
 * insert_node_sorted() - To insert nodes into the list
 * in sorted order
 */
struct node *insert_node_sorted(struct node *head, int data)
{
    struct node *tmp;
    struct node *e_ptr;
    struct node *new_node = malloc(sizeof(struct node));
    if (!new_node)
        exit(0);
    new_node->data = data;
    new_node->next = NULL;
    /* When the list is empty */
    if (!head)
        return new_node;
    else {
        /* Initialize tmp & e_ptr */
        tmp = head;
        e_ptr = head;
        /* Make e_ptr point to the largest element of the list, i.e. last element */
        while (e_ptr->next)
            e_ptr = e_ptr->next;
        /* If the element to be inserted is smaller than the head node */
        if (head->data > new_node->data) {
            new_node->next = head;
            head = new_node;
        } else if (new_node->data > e_ptr->data){ /* Data to be inserted is larger than the largest element in the list */
            e_ptr->next = new_node;
        } else { /* New node should be placed somewhere in between head and e_ptr */
            while (tmp->next && tmp->next->data < new_node->data) 
                tmp = tmp->next;
            new_node->next = tmp->next;
            tmp->next = new_node;
        }
    }
    return head;
}
/*
 * Driver function
 */
int main(int argc, char **argv)
{
    struct node *head = NULL;
    int i;
    /* Populate the list */
    for (i = 0; i < LEN; i++)
        head = insert_node_sorted(head, rand() % 1000);
    /* Print the state of the list */
    printf("State of the list -\n\n");
    print_list(head);
    return 0;
}