题目1181:遍历链表

来源:互联网 发布:淘宝盗版商品是啥意思 编辑:程序博客网 时间:2024/06/02 19:28
#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\n",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;}

0 0