• 构造有序的单链表


    描述

    构造有序(升序)的单链表

    并实现单链表的逆置

    (可以采用结构化的程序设计方法实现,即不必定义类)

    输入输入链表中的数据。(用0表示输入的结束,0不能添加到链表中)输出按顺序输出有序链表中的数据样例输入

    4 1 6 8 2 0

    样例输出

    1 2 4 6 8
    8 6 4 2 1
    代码:方法略显无耻
    #include<bits/stdc++.h>
    using namespace std;
    int a[1000];
    struct Node
    {
        int data;
        Node *next;
    };
    int main()
    {
        int x=0,j=0;
        while(scanf("%d",&x))
        {
            if(x==0) break;
            a[++j]=x;
        }
        sort(a+1,a+1+j);
        for(int i=1;i<=j;i++)
        {
            printf("%d ",a[i]);
        }
        printf("
    ");
        Node *head,*s,*p,*q;
        s=new Node;
        s->data=a[j];
        head=s;
        for(int i=1;i<=j-1;i++)
        {
           p=s;
           s=new Node;
           s->data=a[j-i];
           p->next=s;
        }
        s->next=NULL;
        p=head;
        while(p)
        {
            printf("%d ",p->data);
            p=p->next;
        }
        return 0;
    }
  • 相关阅读:
    适配器
    装饰器
    getOwnPropertyDescriptor
    发布订阅
    策略模式
    window.requestAnimationFrame() 和 window.cancelAnimationFrame()
    L1-056 猜数字
    L1-055 谁是赢家
    L1-054 福到了
    L1-053 电子汪
  • 原文地址:https://www.cnblogs.com/dean-SunPeishuai/p/10542453.html
Copyright © 2020-2023  润新知