19版考研数据结构王道课后习题代码-线性表【完】

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define maxSize 101

/*
//设计递归算法,删除不带头节点的单链表中所有值为x的节点 P37 1
typedef struct LNode
{
    int data;
    LNode *next;
}LNode,*List;
void Delete(List &t,int x)
{
    if(t)
    {
        if(t->data==x)
    {
        LNode *p=t;
        t=t->next;
        free(p);
        Delete(t,x);
    }
    Delete(t->next,x);
    }
}
int main()
{
    LNode *t;
    int n,t0;
    cin>>n;
    cin>>t0;
    t=(LNode*)malloc(sizeof(LNode));
    t->data=t0;
    t->next=NULL;
    LNode *q=t;
    for(int i=1;i>t0;
        LNode *s=(LNode*)malloc(sizeof(LNode));
        s->data=t0;
        q->next=s;
        q=s;
        q->next=NULL;
    }

    Delete(t,3);
    while(t)
    {
        cout<data<<" ";
        t=t->next;
    }
}
*/

/*
//删除带头节点的单链表中值为x的节点 P37 2
typedef struct LNode
{
    int data;
    LNode *next;
}LNode;
void Delete(LNode *&t,int x)
{
    LNode *pre=t;
    LNode *p=pre->next;
    while(p)
    {
        if(p->data==x)
        {
            pre->next=p->next;
            p=pre->next;
        }
        else
        {
            pre=pre->next;
            p=pre->next;
        }
    }
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    Delete(t,3);
    while(t->next)
    {
        cout<next->data<<" ";
        t=t->next;
    }
}

*/


/*
//从尾到头反向输出单链表的值 P37 3
typedef struct LNode
{
    int data;
    LNode *next;
}LNode;
void Reverse(LNode *t)
{
    int stack[maxSize];
    int top=-1;
    while(t->next)
    {
        stack[++top]=t->next->data;
        t=t->next;
    }
    while(top!=-1)
    {
        cout<next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    Reverse(t);
}
5
4 2 1 6 7
7 6 1 2 4
*/


/*
//删除单链表的最小值节点 P37 4
//用minpre和minp来删除节点!!
typedef struct LNode
{
    int data;
    LNode *next;
}LNode,*List;
void Delete(LNode *&t)
{
    LNode *minpre,*minp;
    int min;
    LNode *pre=t;
    LNode *p=t->next;
    minp=p;
    minpre=pre;
    min=p->data;
    while(p)
    {
        if(p->datadata;
            minp=p;
            minpre=pre;
        }
        p=p->next;
        pre=pre->next;
    }
    minpre->next=minp->next;
    free(minp);
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    Delete(t);
    while(t->next)
    {
        cout<next->data<<" ";
        t=t->next;
    }
}
 */

/*
//将带头节点的单链表就地逆置 P37 5
typedef struct LNode
{
    int data;
    LNode *next;
}LNode,*List;
void Reverse(LNode *&t)
{
    LNode *p=t;
    LNode *q=p->next;
    LNode *r=q->next;
    q->next=NULL;   //易错点!!!一点要让q后面为空!!!
    LNode *s;
    while(r)
    {
        s=r->next;
        r->next=q;
        p->next=r;
        q=r;
        r=s;
    }
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    Reverse(t);
    while(t->next)
    {
        cout<next->data<<" ";
        t=t->next;
    }
}
*/


/*
//使带头节点的单链表单增 P37 6  使用直接插入排序
typedef struct LNode
{
    int data;
    LNode *next;
}LNode,*List;
void SelectSort(LNode *&t)
{
    LNode *prer=t->next;
    LNode *r=t->next->next;
    while(r)
    {
        LNode *pre=t;
        LNode *p=t->next;
        while(p->data<=r->data&&p!=r)  //易错点!!要加上等号且p不能超过r!!
        {
            p=p->next;
            pre=pre->next;
        }
        if(p->data>r->data)
        {
            prer->next=r->next;
            r->next=p;
            pre->next=r;
            r=prer->next;
        }
        else
        {
            r=r->next;
            prer=prer->next;
        }
    }
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    SelectSort(t);
    while(t->next)
    {
        cout<next->data<<" ";
        t=t->next;
    }
}
//7
//3 1 4 1 2 9 1
//1 1 1 2 3 4 9
*/


/*
//删除介于给定两个值之间的元素 P37 7
typedef struct LNode
{
    int data;
    LNode *next;
}LNode,*List;
void Delete(LNode *t,int a,int b)
{
    LNode *pre=t;
    LNode *p=pre->next;
    while(p)
    {
        if(p->data>=a&&p->data<=b)
        {
            pre->next=p->next;
            p=pre->next;
        }
        else
        {
            pre=pre->next;
            p=p->next;
        }
    }
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    Delete(t,2,5);
    while(t->next)
    {
        cout<next->data<<" ";
        t=t->next;
    }
}


7
3 2 1 5 6 7 3
1 6 7
*/


/*
//找出两个链表的公共节点 P37 8  公共节点不只是值相同,而是同一个节点的意思。形状像Y,而不是X,从第一个公共节点到尾节点,这一部分是重合的
typedef struct LNode
{
    int data;
    LNode *next;
}LNode,*List;
int strlen(LNode *t)
{
    int cnt=0;
    while(t->next)
    {
        cnt++;
        t=t->next;
    }
    return cnt;
}
LNode *Find(LNode *l1,LNode *l2)
{
    int len1=strlen(l1);
    int len2=strlen(l2);
    LNode *longlist,*shortlist;
    int longlen,shortlen;
    if(len1>len2)
    {
        longlist=l1;
        longlen=len1;
        shortlist=l2;
        shortlen=len2;
    }
    else
    {
        longlist=l2;
        longlen=len2;
        shortlist=l1;
        shortlen=len1;
    }
    int t=longlen-shortlen;
    while(t)
    {
        longlist=longlist->next;
        t--;
    }
    while(longlist&&shortlist)
    {
        if(longlist==shortlist)
        {
            return longlist;
        }
        else
        {
            shortlist=shortlist->next;
            longlist=longlist->next;
           // cout<data<<" "<data<next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    
    LNode *t2=(LNode*)malloc(sizeof(LNode));
    t2->next=NULL;
    LNode *q2=t2;
    int n2;
    cin>>n2;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q2->next=p;
        q2=p;
        q2->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    
    LNode *t3=(LNode*)malloc(sizeof(LNode));
    t3->next=NULL;
    int n3;
    cin>>n3;
    cin>>t0;
    t3->data=t0;
    LNode *q3=t3;
    for(int i=1;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q3->next=p;
        q3=p;
        q3->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    q->next=t3;
    q2->next=t3;
    
//    while(t->next)
//    {
//        cout<next->data<<" ";
//        t=t->next;
//    }
//    cout<next)
//    {
//        cout<next->data<<" ";
//        t2=t2->next;
//    }
    
    LNode *s=Find(t,t2);
    cout<data;
}
*/


/*
//按递增顺序输出节点元素的数据,并释放节点所占的空间  P37 9  每次找出单链表中最小的元素,输出,删除
typedef struct LNode
{
    int data;
    LNode *next;
}LNode,*List;
void Sort(LNode *head)
{
    while(head->next)
    {
        LNode *p=head->next;
        int min=p->data;
        LNode *minp=p;
        LNode *minpre=head;
        LNode *pre=head;
        while(p)
        {
            if(p->datadata;
                minp=p;
                minpre=pre;
            }
        p=p->next;
        pre=pre->next;
        }
        cout<next=minp->next;
        free(minp);
    }
    free(head);
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    Sort(t);
}


6
3 6 4 1 2 8
1 2 3 4 6 8
*/

/*
//将单链表A分解为A和B,A含序号为奇数的元素,B含偶数 P37 10
typedef struct LNode
{
    int data;
    LNode *next;
}LNode;
void Split(LNode *a)
{
    int i=0;
    LNode *p=a;
    LNode *ra=(LNode*)malloc(sizeof(LNode));
    ra->next=NULL;
    LNode *b=(LNode*)malloc(sizeof(LNode));
    b->next=NULL;
    LNode *rb;
    rb=b;
    while(p->next)
    {
        i++;
        p=p->next;
        if(i%2==1)
        {
            ra->next=p;
            ra=p;
        }
        else
        {
            rb->next=p;
            rb=p;
        }
    }
    ra->next=NULL;
    rb->next=NULL;
    while(a->next)
    {
        cout<next->data<<" ";
        a=a->next;
    }
    cout<next)
    {
        cout<next->data<<" ";
        b=b->next;
    }
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    Split(t);
}
*/


/*
//就地算法,拆分单链表 P37 11
typedef struct LNode
{
    int data;
    LNode *next;
}LNode;
void Split(LNode *t)
{
    LNode *p=t->next;
    int i=0;
    LNode *a=(LNode*)malloc(sizeof(LNode));
    a->next=NULL;
    LNode *ra=a;
    LNode *b0=(LNode*)malloc(sizeof(LNode));
    b0->next=NULL;
    LNode *b=b0;
    while(p)
    {
        i++;
        if(i%2==1)
        {
            ra->next=p;
            ra=p;
            p=p->next;   //易错点
            ra->next=NULL;
        }
        else
        {
            LNode *q=p;
            p=p->next;   //注意上下这两个易错点处,改变q会把p也改变,因此p要后移才能保存p的后继节点!!
            q->next=b->next;
            b->next=q;
        }
    }
    
    while(a->next)
    {
        cout<next->data<<" ";
        a=a->next;
    }
    while(b0->next)
    {
        cout<next->data<<" ";
        b0=b0->next;
    }
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    Split(t);
}

5
3 2 1 2 4
3 1 4 2 2
*/

/*
//删除表中重复元素 P37 12
typedef struct LNode
{
    int data;
    LNode *next;
}LNode;
void Delete(LNode *t)
{
    LNode *p=t->next;
    LNode *q=p->next;
    while(q)
    {
       if(q->data==p->data)
       {
           p->next=q->next;
           q=p->next;
       }
        else
        {
            p=p->next;
            q=q->next;
        }
    }
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    Delete(t);
    while(t->next)
    {
        cout<next->data<<" ";
        t=t->next;
    }
}

7
1 2 2 2 2 3 3
1 2 3
*/


/*
//将两个单增链表合并为一个单减链表 P37 13
typedef struct LNode
{
    int data;
    LNode *next;
}LNode;
LNode *Merge(LNode *t1,LNode *t2)
{
    LNode *p=t1->next;
    LNode *q=t2->next;
    LNode *s0=(LNode*)malloc(sizeof(LNode));
    s0->next=NULL;
    LNode *s=s0;
    while(p&&q)
    {
        while(p&&q&&p->datadata)
        {
            LNode *x1=p;
            p=p->next;   //易错点!!注意这一句的顺序!!!p先移动,再改变x1的指针,否则p会被丢失!!
            x1->next=NULL;
            x1->next=s->next;
            s->next=x1;
        }
        while(p&&q&&p->data>=q->data)
        {
            LNode *x2=q;
            q=q->next;
            x2->next=NULL;
            x2->next=s->next;
            s->next=x2;
        }
    }
    while(p)
    {
        LNode *x1=p;
        p=p->next;
        x1->next=NULL;
        x1->next=s->next;
        s->next=x1;
    }
    while(q)
    {
        LNode *x2=q;
        q=q->next;
        x2->next=NULL;
        x2->next=s->next;
        s->next=x2;
    }
    return s;
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    
    LNode *t2=(LNode*)malloc(sizeof(LNode));
    t2->next=NULL;
    LNode *q2=t2;
    int n2;
    cin>>n2;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q2->next=p;
        q2=p;
        q2->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    
    LNode *s=Merge(t,t2);
    while(s->next)
    {
        cout<next->data<<" ";
        s=s->next;
    }
}

3
1 2 3
4
2 3 4 5
5 4 3 3 2 2 1
*/


/*
//从A,B公共元素中产生单链表C  P37 14
typedef struct LNode
{
    int data;
    LNode *next;
}LNode;
LNode *Common(LNode *a,LNode *b)
{
    LNode *c=(LNode*)malloc(sizeof(LNode));
    c->next=NULL;
    LNode *cc=c;
    LNode *p=a->next;
    LNode *q=b->next;
    while(p&&q)
    {
        while(p&&q&&p->datadata)
        {
            p=p->next;
        }
        while(p&&q&&p->data>q->data)
        {
            q=q->next;
        }
        if(p&&q&&p->data==q->data)  //这一句也要加上p&&q都存在!!!
        {
            LNode *x=(LNode*)malloc(sizeof(LNode));
            x->data=p->data;
            cc->next=x;
            cc=x;
            cc->next=NULL;
            p=p->next;
            q=q->next;
        }
    }
    return c;
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    
    LNode *t2=(LNode*)malloc(sizeof(LNode));
    t2->next=NULL;
    LNode *q2=t2;
    int n2;
    cin>>n2;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q2->next=p;
        q2=p;
        q2->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    LNode *s=Common(t,t2);
    while(s->next)
    {
        cout<next->data<<" ";
        s=s->next;
    }
}

3
1 3 5
6
1 2 3 4 7 8
1 3

*/


/*
//求交集 P37 15
typedef struct LNode
{
    int data;
    LNode *next;
}LNode;
LNode *Common(LNode *a,LNode *b)
{
    LNode *ra=a->next;
    LNode *rb=b->next;
    LNode *r=a;
    LNode *u;
    while(ra&&rb)
    {
        while(ra&&rb&&ra->datadata)
        {
            u=ra;
            ra=ra->next;
            free(u);
        }
        while(ra&&rb&&ra->data>rb->data)
        {
            u=rb;
            rb=rb->next;
            free(u);
        }
        if(ra&&rb&&ra->data==rb->data)
        {
            r->next=ra;
            r=ra;
            ra=ra->next;
            u=rb;
            rb=rb->next;
            free(u);
        }
    }
    while(ra)
    {
        u=ra;
        ra=ra->next;
        free(u);
    }
    while(rb)
    {
        u=rb;
        rb=rb->next;
        free(u);
    }
    r->next=NULL;
    free(b);
    return a;
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    
    LNode *t2=(LNode*)malloc(sizeof(LNode));
    t2->next=NULL;
    LNode *q2=t2;
    int n2;
    cin>>n2;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q2->next=p;
        q2=p;
        q2->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    LNode *s=Common(t,t2);
    while(s->next)
    {
        cout<next->data<<" ";
        s=s->next;
    }
}

3
2 4 7
5
1 2 3 4 8
2 4
*/


/*
//判断连续子序列  P38  16
typedef struct LNode
{
    int data;
    LNode *next;
}LNode;
int Sequence(LNode *a,LNode *b)
{
    LNode *ra=a->next;
    LNode *rb=b->next;
    while(ra&&rb)
    {
        LNode *s=ra;
        while(ra&&rb&&ra->data==rb->data)
        {
            ra=ra->next;
            rb=rb->next;
        }
        if(rb==NULL)
        {
            return 1;
        }
        else
        {
            ra=s->next;
            rb=b->next;
        }
    }
    return 0;
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    
    LNode *t2=(LNode*)malloc(sizeof(LNode));
    t2->next=NULL;
    LNode *q2=t2;
    int n2;
    cin>>n2;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q2->next=p;
        q2=p;
        q2->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    int d=Sequence(t,t2);
    cout<next;
    LNode *b=t->prior;
    while(b!=t&&a!=t)
    {
        if(a->data!=b->data)
        {
            return 0;
        }
        a=a->next;
        b=b->prior;
    }
    return 1;
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        p->prior=q;
        q=p;
    }
    q->next=t;
    t->prior=q;
    int d=Judge(t);
    cout<next!=h1)
    {
        a=a->next;
    }
    a->next=h2;
    a=h2;
    while(a->next!=h2)
    {
        a=a->next;
    }
    a->next=h1;
    return h1;
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    int n,t0;
    cin>>n;
    cin>>t0;
    t->data=t0;
    LNode *q=t;
    for(int i=1;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
    }
    q->next=t;
    
    LNode *t2=(LNode*)malloc(sizeof(LNode));
    t2->next=NULL;
    int n2;
    cin>>n2;
    cin>>t0;
    t2->data=t0;
    LNode *q2=t2;
    for(int i=1;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q2->next=p;
        q2=p;
    }
    q2->next=t2;
    LNode *s=Merge(t,t2);
    LNode *d=s;   //易错点:记得保留头节点!!
    while(s->next!=d)
    {
        cout<data<<" ";
        s=s->next;
    }
    cout<data;
}

3
4 2 1
4
2 6 3 1
4 2 1 2 6 3 1
*/


/*
//删除循环单链表中的最小值。P38 19
typedef struct LNode
{
    int data;
    LNode *next,*prior;
}LNode;
void Delete(LNode *t)
{
    LNode *p,*pre;
    while(t->next!=t)
    {
        p=t->next;
        pre=t;
        int min=p->data;
        LNode *minp=p;
        LNode *minpre=t;
        while(p!=t)   //易错点!!!注意是p而不是p->next
        {
            if(p->datadata;
                minp=p;
                minpre=pre;
            }
            p=p->next;
            pre=pre->next;
        }
        cout<next=minp->next;
        free(minp);
    }
    free(t);
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
    }
    q->next=t;
    Delete(t);
}


6
1 3 4 3 2 1
1 1 2 3 3 4
*/

/*
//对双向链表进行排序,先按访问频度,再按最近访问时间 P38 20
typedef struct LNode
{
    int data,freq;
    LNode *next,*prior;
}LNode;
LNode *Locate(LNode *t,int x)
{
    LNode *p=t->next;
    while(p!=NULL&&p->data!=x)
    {
        p=p->next;
    }
    p->prior->next=p->next;
    p->next->prior=p->prior;
    p->freq++;
    LNode *q=t->next;
    while(q!=NULL&&q->freq>p->freq)
    {
        q=q->next;
    }
    p->next=q;
    q->prior->next=p;
    p->prior=q->prior;
    q->prior=p;
    return p;
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        p->prior=q;
        q=p;
        q->next=NULL;
    }
    LNode *s=Locate(t,2);
    
}
*/

/*
//查找链表中倒数第k个位置的节点的值 P38 21
typedef struct LNode
{
    int data,freq;
    LNode *next,*prior;
}LNode;
int Search(LNode *list,int k)
{
    LNode *p=list->next;
    LNode *pre=list->next;
    while(k)
    {
        p=p->next;
        if(p==NULL)
            return 0;
        k--;
    }
    while(p)
    {
        p=p->next;
        pre=pre->next;
    }
    return pre->data;
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        p->prior=q;
        q=p;
        q->next=NULL;
    }
    int d=Search(t,3);
    cout<next)
    {
        cnt++;
        t=t->next;
    }
    return cnt;
}
LNode *Find(LNode *a,LNode *b)
{
    int len1=strlen(a);
    int len2=strlen(b);
    LNode *longlist,*shortlist;
    int min1,max2;
    if(len1next;
        t--;   //注意循环控制条件!!!
    }
    while(longlist&&shortlist)
    {
        if(longlist->data==shortlist->data)
        {
            return longlist;
        }
        else
        {
            longlist=longlist->next;
            shortlist=shortlist->next;
        }
    }
    return NULL;
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    
    LNode *t2=(LNode*)malloc(sizeof(LNode));
    t2->next=NULL;
    LNode *q2=t2;
    int n2;
    cin>>n2;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q2->next=p;
        q2=p;
        q2->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    
    LNode *t3=(LNode*)malloc(sizeof(LNode));
    t3->next=NULL;
    int n3;
    cin>>n3;
    cin>>t0;
    t3->data=t0;
    LNode *q3=t3;
    for(int i=1;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q3->next=p;
        q3=p;
        q3->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    q->next=t3;
    q2->next=t3;
    
    LNode *s=Find(t,t2);
    while(s)
    {
        cout<data<<" ";
        s=s->next;
    }
}


3
2 4 5
2
6 8
4
2 6 4 3
2 6 4 3
*/


/*
//删除绝对值相同的元素 P38 23
typedef struct LNode
{
    int data,freq;
    LNode *next,*prior;
}LNode;
void Delete(LNode *t)
{
    int map[maxSize];
    LNode *p=t->next;
    LNode *pre=t;
    while(p)
    {
        if(map[abs(p->data)]==0)
        {
            map[abs(p->data)]++;
            p=p->next;
            pre=pre->next;
        }
        else if(map[abs(p->data)]==1)
        {
            pre->next=p->next;
            p=pre->next;
        }
    }
}
int main()
{
    LNode *t=(LNode*)malloc(sizeof(LNode));
    t->next=NULL;
    LNode *q=t;
    int n,t0;
    cin>>n;
    for(int i=0;i>t0;
        LNode *p=(LNode*)malloc(sizeof(LNode));
        p->data=t0;
        q->next=p;
        q=p;
        q->next=NULL;  //!!!易错点,一定要记得置NULL
    }
    Delete(t);
    while(t->next)
    {
        cout<next->data<<" ";
        t=t->next;
    }
}

5
21 -15 -15 -7 15
21 -15 -7
*/














 

你可能感兴趣的:(考研)