简体   繁体   English

以升序将元素插入单链列表中。

[英]To insert the elements in a singly linked list in ascending order.

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

Here I am trying to insert the elements in a ascending order. 在这里,我试图按升序插入元素。 To my surprise, the ouput I got is 5 78 23 8 17 10, which is incorrect, could anyone please have a look on this? 令我惊讶的是,我的输出是5 78 23 8 17 10,这是不正确的,任何人都可以看看这个吗? Any help would be appreciated.. 任何帮助,将不胜感激..

In order to insert nodes in ascending order in a linked list, you'll have to address following scenarios - 为了按升序将节点插入链表中,您必须解决以下情况-
1. List is empty: When there are no elements in the list 1.列表为空:列表中没有元素时
2. Insert at begin: When r->data < header->data , it needs to be inserted at the very beginning of the list. 2.在开始处插入:当r->data < header->data ,需要在列表的最开始处插入它。
3. Insert at end: When r->data is greater than the node with largest value in the list, it needs to be inserted at the very end (assuming we're doing sorted insert here), as user @keltar has already pointed out. 3.在末尾插入:当r->data大于列表中具有最大值的节点时,需要在末尾插入(假设我们在此处进行排序插入),因为用户@keltar已经指出出。
4. Insert in the middle: When r->data > header->data but smaller than the largest element in the list, it needs to be inserted somewhere in between header and last node. 4.插入中间:当r->data > header->data但小于列表中的最大元素时,需要将其插入到header和最后一个节点之间的某个位置。

Simple implementation of sorted insert is as below - 排序插入的简单实现如下-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM