sean

厚积薄发,坚持到最后一分钟。

构造有序的单链表

描述

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

并实现单链表的逆置

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

 

输入输入链表中的数据。(用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("\n");
    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;
}

 

posted on 2019-03-16 15:22  Sean521  阅读(1499)  评论(0)    收藏  举报

导航