zoukankan      html  css  js  c++  java
  • 7-1 有序链表的插入

    已知一个递增有序链表L(带头结点,元素为整数),编写程序将一个新整数插入到L中,并保持L的有序性。 其中单链表的类型定义参考如下:

    typedef int elementType;

    typedef struct lnode

    { elementType data;

    struct lnode *next;

    }Lnode,* LinkList;

    输入格式:

    输入分三行

    第一行 元素个数

    第二行 元素的值,元素间用空格分隔。

    第三行 待插入的元素值

    输出格式:

    在一行中输出有序链表元素值,每个元素前输出一个空格以便与相邻元素分隔。

    输入样例:

    5
    1 3 5 7 9
    4

    输出样例:

     1 3 4 5 7 9

     1 #include <stdio.h>
     2 #include <malloc.h>
     3 typedef int elementType;
     4 typedef struct lnode
     5 {
     6     elementType data;
     7     struct lnode *next;
     8 
     9 }Lnode,* LinkList;
    10 int main()
    11 {
    12     int num;
    13     LinkList List;
    14     Lnode *head, *tail, *temp;
    15     head = tail = (Lnode *)malloc(sizeof(Lnode));
    16     scanf("%d", &num);
    17     while(num--){
    18         temp = (Lnode *)malloc(sizeof(Lnode));
    19         scanf("%d", &temp->data);
    20         tail->next = temp;
    21         tail = temp;
    22     }
    23     tail->next = NULL;
    24     List = head;
    25 
    26     temp = (Lnode *)malloc(sizeof(Lnode));
    27     tail = List->next;
    28     scanf("%d", &temp->data);
    29     while(tail->data <= temp->data && tail->next != NULL){
    30         tail = tail->next;
    31         head = head->next;
    32     }
    33     if(tail->next == NULL){
    34         tail->next = temp;
    35         temp->next = NULL;
    36     }
    37     else{
    38             temp->next = tail;
    39             head->next = temp;
    40     }
    41 
    42 
    43 
    44     head = List->next;
    45     while(head != NULL)
    46     {
    47         printf("%d ", head->data);
    48         head = head->next;
    49     }
    50 }
     1 #include <stdio.h>
     2 #include <malloc.h>
     3 typedef int elementType;
     4 typedef struct lnode
     5 {
     6     elementType data;
     7     struct lnode *next;
     8 
     9 }Lnode,* LinkList;
    10 int main()
    11 {
    12     int num;
    13     LinkList List;
    14     Lnode *head, *tail, *temp;
    15     head = tail = (Lnode *)malloc(sizeof(Lnode));
    16     scanf("%d", &num);
    17     while(num--){
    18         temp = (Lnode *)malloc(sizeof(Lnode));
    19         scanf("%d", &temp->data);
    20         tail->next = temp;
    21         tail = temp;
    22     }
    23     tail->next = NULL;
    24     List = head;
    25 
    26     temp = (Lnode *)malloc(sizeof(Lnode));
    27     tail = List;
    28     scanf("%d", &temp->data);
    29     while(tail->next != NULL){
    30         if(tail->next->data >temp->data){
    31             temp->next = tail->next;
    32             tail->next = temp;
    33             break;
    34         }
    35         else
    36             tail = tail->next;
    37     }
    38     if(tail->next == NULL){
    39         tail->next = temp;
    40         temp->next = NULL;
    41     }
    42 
    43     head = List->next;
    44     while(head != NULL)
    45     {
    46         printf("%d ", head->data);
    47         head = head->next;
    48     }
    49 }

    作者:7oDo

    仅供参考,请勿抄袭。

    Hang Hang Hang !!!

  • 相关阅读:
    Codeforces Round #592 (Div. 2)C. The Football Season(暴力,循环节)
    Educational Codeforces Round 72 (Rated for Div. 2)D. Coloring Edges(想法)
    扩展KMP
    poj 1699 Best Sequence(dfs)
    KMP(思路分析)
    poj 1950 Dessert(dfs)
    poj 3278 Catch That Cow(BFS)
    素数环(回溯)
    sort与qsort
    poj 1952 buy low buy lower(DP)
  • 原文地址:https://www.cnblogs.com/Jie-Fei/p/9637051.html
Copyright © 2011-2022 走看看