关闭

3.3

44人阅读 评论(0) 收藏 举报
分类:

——————————a————————

#include <iostream>
#include <stdlib.h>
#define MAX 8
using namespace std;
typedef struct node
{
    int num;
    struct node* pnext;
}qnode,*pnode;
pnode init()
{
    int i=MAX,num_;
    pnode head,p,q;
    head=(pnode)malloc(sizeof(qnode));
    head->pnext=NULL;
    q=head;
    while(i--)
    {
        p=(pnode)malloc(sizeof(qnode));
        cin>>num_;
        p->num=num_;
        q->pnext=p;
        q=p;
    }
    q->pnext=NULL;
    return head;
}
void traverse(pnode head)
{
    pnode q=head->pnext;
    while(q!=NULL)
    {
        cout<<q->num;
        q=q->pnext;
    }
}
pnode find(pnode head,int pos)
{
    pnode q=head;
    int i;
    for(i=0;i<(pos-1);i++)
    {
        q=q->pnext;
    }
    return q;
}
pnode find_(pnode head,int pos)
{
    pnode q=head;
    int i;
    for(i=0;i<pos;i++)
    {
        q=q->pnext;
    }
    return q;
}
void exchange(pnode head,int pos,int pos_)
{
    pnode p,q,p_,q_;
    p=find(head,pos);
    p_=p->pnext;
    q=find_(head,pos_);
    q_=q;
    q=q->pnext;
    p->pnext=q_;
    q_->pnext=p_;
    p_->pnext=q;
}
int main()
{
    pnode head;
    head=init();
    exchange(head,4,5);
    traverse(head);
}

——————————b————————————————

#include <iostream>
#include <stdlib.h>
#define MAX 8
using namespace std;
typedef struct node
{
    int num;
    struct node* prior;
    struct node* pnext;
}qnode,*pnode;
pnode init()
{
    int i=MAX,num_;
    pnode head,p,q;
    head=(pnode)malloc(sizeof(qnode));
    head->pnext=NULL;
    q=head;
    while(i--)
    {
        p=(pnode)malloc(sizeof(qnode));
        cin>>num_;
        p->num=num_;
        q->pnext=p;
        p->prior=q;
        q=p;
    }
    q->pnext=NULL;
    return head;
}
void traverse(pnode head)
{
    pnode q=head->pnext;
    while(q!=NULL)
    {
        cout<<q->num<<' ';
        q=q->pnext;
    }
}
pnode find(pnode head,int pos)
{
    pnode q=head;
    int i;
    for(i=0;i<(pos-1);i++)
    {
        q=q->pnext;
    }
    return q;
}
pnode find_(pnode head,int pos)
{
    pnode q=head;
    int i;
    for(i=0;i<pos;i++)
    {
        q=q->pnext;
    }
    return q;
}
void exchange(pnode head,int pos,int pos_)
{
    pnode p,q,p_,q_;
    p=find(head,pos);
    p_=p->pnext;
    q=find_(head,pos_);
    q_=q;
    q=q->pnext;
    p->pnext=q_;
    q_->prior=p;
    q_->pnext=p_;
    p_->prior=q_;
    p_->pnext=q;
    q->prior=p_;
}
int main()
{
    pnode head;
    head=init();
    exchange(head,4,5);
    traverse(head);
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8274次
    • 积分:937
    • 等级:
    • 排名:千里之外
    • 原创:90篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条