把一个新数插入到一个有序的单链表中

 //把一个新数插入到一个有序的单链表中
//xwlee 2006/12/16
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define FALSE 0
#define TRUE  1
#define MY_MAX 100

typedef struct NODE{
    struct NODE *link;
    int value;
}node;

int sll_insert(register node **rootp, int new_value);

int main()
{
    int i;
    node *p, *p_origin;

    srand( (unsigned)time( NULL ) );//随机产生数据

    if( !( p = (node *)malloc(MY_MAX*sizeof(struct NODE)) ) )
        exit(1);
    p_origin=p;
    
    //初始化有序数组
    for(i=1; i<MY_MAX; ++i)
    {
        p->value = i*10;
        p->link=p+1;
        p++;
    }
    p->value=i*10;
    p->link=NULL;//注意这里,结束链表
    
    
    for(p=p_origin; p!=NULL; p=p->link)
        printf("%-8d", p->value);
    
    
    p=p_origin;
    for(i=0; i<200; i++)
         sll_insert( &p, rand()%1000);
    printf("/n=======================after insert======================/n");      
    for(;  p;  p= p->link  )
          printf("%-8d", p->value);
    

    printf("/n");
    return 0;
}

//插入函数,效率不错
int sll_insert(register node **linkp, int new_value)
{
    register node *current;
    register node *newp;
    
    while( (current=*linkp)!=NULL &&
            (current->value) < new_value)
        linkp=&current->link;

    if( !(newp=(node *)malloc( sizeof(node) ) ) )
        return FALSE;
    newp->value=new_value;

    //在链表中插入新结点,返回TRUE
    newp->link=current;
    *linkp=newp;
    
    return TRUE;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Java实现链表有序插入,以下是一个示例代码: ```java public class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } public class SortedLinkedList { Node head; // 插入节点 public void insert(int data) { Node newNode = new Node(data); if (head == null || data < head.data) { newNode.next = head; head = newNode; } else { Node current = head; while (current.next != null && current.next.data < data) { current = current.next; } newNode.next = current.next; current.next = newNode; } } // 打印链表 public void display() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); } public static void main(String[] args) { SortedLinkedList list = new SortedLinkedList(); // 从键盘输入一组有序的数 Scanner scanner = new Scanner(System.in); System.out.println("请输入一组有序的数(以逗号分隔):"); String input = scanner.nextLine(); String[] nums = input.split(","); // 依次插入节点 for (String num : nums) { int data = Integer.parseInt(num.trim()); list.insert(data); } // 从键盘输入一个元素值key,并进行有序插入 System.out.println("请输入要插入的元素值:"); int key = scanner.nextInt(); list.insert(key); // 打印链表 System.out.println("插入后的有序链表:"); list.display(); } } ``` 以上代码,我们首先定义了一个`Node`类表示链表的节点,然后定义了一个`SortedLinkedList`类表示有序链表。在`SortedLinkedList`类,我们实现了`insert`方法用于有序插入节点,以及`display`方法用于打印链表。 在`main`方法,我们首先从键盘输入一组有序的数,并将其插入链表。然后从键盘输入一个元素值key,并将其有序插入链表。最后,打印插入后的有序链表。 请注意,以上代码只是示例,可能需要根据实际情况进行适当修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值