admin管理员组

文章数量:1516870

#include <iostream>
using namespace std;
template <class t>
struct node
{
    t data;
    node <t> *next;
};
template <class t>
class list
{
public:
    list();
    list(t a[],int n);
    list(t a[],int n,int weicha);
    void print();
    t weizhi(int x);
    t zhi(int x);
    void JudgeZeng();
    node <t> *first;
    int l;
};
template <class t>
list<t>::list()
{
    first =new node<t>;
    first->next=NULL;
}
template <class t>
list<t>::list(t a[],int n)
{
    first =new node<t>;
    first->next=NULL;
    node <t> *s;
    for(int i=0; i<n; i++)
    {
        s=new node<t> ;
        s->data=a[i];
        s->next =first->next;
        first->next=s;
    }
}
template <class t>
list<t>::list(t a[],int n,int weicha)
{
    node<t> *s,*r;
    first =new node<t>;
    r=first;
    for (int i=0; i<n; i++)
    {
        s=new node<t>;
        s->data=a[i];
        r->next =s;
        r=s;
    }
    r->next=NULL;
    l=n;
}
template <class t>
void list<t> ::print ()
{
    node <t> *p;
    p=first->next;
    while (p!=NULL)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
}
template <class t>
t list <t>::zhi(int x)
{
    node <t> *p,*q;
    p=first->next;
    int k=1;
    while (p!=NULL&&x!=p->data)
    {
        p=p->next;
        k++;
    }
    if(p==NULL||p->next==NULL)
        cout<<"Error";
    else
    {
        int j=0;
        p=first;
        while (p!=NULL&&j<k-1)
        {
            p=p->next;
            j++;
        }
        q=p->next;
        p->next=q->next;
        delete q;
        print();
    }
}
template <class t>
t list <t>::weizhi(int x)
{
    node <t> *p,*q;
    p=first;
    int j=0;
    while (p!=NULL&&j<x-1)
    {
        p=p->next;
        j++;
    }
    if(x<=0||p==NULL)
        cout<<"Error";
    else
    {
        q=p->next;
        p->next=q->next;
        delete q;
        print();
    }
}
template <class t>
void list<t>::JudgeZeng()
{
    node<t>* p = first->next, * q;
    if (p)
    {
        while (p->next)
        {
            q = p->next;
            if (q->data > p->data)
                p = q;
            else
                cout << "No";
        }
    }
    cout << "Yes";
}
int main()
{
    int a1[100];
    int a2[100];
    int t,x;
    int i=0;
    while (cin>>t&&t)
    {
        a1[i]=t;
        i++;
    }
    list <int> a(a1,i,1);
    i=0;
    while (cin>>t&&t)
    {
        a2[i]=t;
        i++;
    }
    list <int> b(a2,i);
    cin>>x;
    int y;
    cin>>y;
    a.print();
    cout<<endl;
     b.print();
     cout<<endl;
     a.zhi(x);
     cout<<endl;
    a.weizhi(y);
    cout<<endl;
    a.JudgeZeng();
}

本文标签: 编程的背后理解