题目1181:遍历链表

#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef struct node node;
struct node
{
    int data;
    struct node * next;
};

node* creat(int n)
{
    node *head=new node;
    head->next=NULL;
    node *p=head;
    while(n-->0)
    {
        node *t=new node;
        scanf("%d",&t->data);
        t->next=p->next;
        p->next=t;
        p=t;
    }
    return head;
}
void look(node * head)
{
    node * p=head->next;
    while(p->next)
    {
        printf("%d ",p->data);
        p=p->next;
    }
    printf("%d
",p->data);
}
void destory(node *head)
{
    node * p=head;
    while(p)
    {
        node *t=p;
        p=p->next;
        delete t;
    }
}

void Sort(node *head)
{
    node *p,*q;
    for(p=head->next; p; p=p->next)
    {
        for(q=head->next; q; q=q->next)
        {
            if(p->data<q->data)
            {
                int t=p->data;
                p->data=q->data;
                q->data=t;
            }
        }
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        node* head=creat(n);
        Sort(head);
        look(head);
        destory(head);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/jasonhaven/p/7355033.html