构造有序的单链表

描述
构造有序(升序)的单链表
并实现单链表的逆置
(可以采用结构化的程序设计方法实现,即不必定义类)
输入
输入链表中的数据。(用0表示输入的结束,0不能添加到链表中)
输出
按顺序输出有序链表中的数据

//这题在数组里排序,再利用头插尾插的方式插入,方法有点low,操作起来容易一点
#include
#include
#define N 110
using namespace std;

struct Node
{
    int data;
    Node *next;
};

class Link
{
    Node *first;
public:
    Link();
    Link(int a[],int n);//头插
    Link(int a[],int n,int m);//尾插
};
//头插
Link::Link(int a[],int n)
{
    first=new Node;
    first->next=NULL;
    for(int j=0;jdata=a[j];
        s->next=first->next;
        first->next=s;
    }
    Node *p=first->next;
    while(p!=NULL)
    {
        cout<data<<" ";
        p=p->next;
    }
    cout<data=a[j];
        r->next=s;
        r=s;
    }
    r->next=NULL;
    Node *p=first->next;
    while(p!=NULL)
    {
        cout<data<<" ";
        p=p->next;
    }
    cout<>x&&x)
    {
        a[n]=x;
        n++;
    }
    sort(a,a+n);
    Link l(a,n,m);
    Link L(a,n);
    return 0;
}

你可能感兴趣的:(数据结构练习题)