判断单链表是否递增

来源:互联网 发布:java if else语句 编辑:程序博客网 时间:2024/06/06 01:58
#include <iostream>#include<list>using namespace std;template <class T>struct Node{    T data;    Node<T>* next;};template <class T>//尾插建立循环单链表Node<T>* creat_back( Node<T> * first,int len){    Node<T>* r=first;    for( int i=0; i<len; i++)    {        int data;        cin>>data;        Node<T>* pnew=new Node<T>;        pnew->data=data;        pnew->next=r->next;        r->next=pnew;        r=pnew;    }    r->next=NULL;    return first;}template <class T>//输出链表循环单链表void show(Node<T>* first){    Node<T>* p=first->next;    while(p!=NULL)    {        cout<<p->data<<' ';        p=p->next;    }    cout<<endl<<endl;}template<class T>int IsIncrease(Node<T>* first){    Node<T>* p=first->next;    while(p->next)    {        if(p->data<p->next->data)            p=p->next;        else            return 0;    }    return 1;}int main(){    Node<int >* first=new Node<int>;    first=creat_back(first,5);    show(first);    if(IsIncrease(first))        cout<<"Increase"<<endl;    else        cout<<"Not Increase"<<endl;    return 0;}

阅读全文
0 0
原创粉丝点击