数据结构—02-线性结构3 Reversing Linked List (25 分)

不是一道很难的题,要求是根据输入的链表,以及K,每K个结点转置一次。

主要实现下面几个操作即可:

1.根据输入,创建链表(静态链表)

2.实现链表转置

3.输出

代码:

#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;

struct Node
{
    int address;
    int value;
    int next;
};
Node *Data;
void Print(Node *Data);
void Swap(Node &A,Node &B)
{
    Node Temp;
    Temp.address = A.address;
    Temp.next = A.next;
    Temp.value = A.value;

    A.address = B.address;
    A.next = B.next;
    A.value = B.value;

    B.address = Temp.address;
    B.next = Temp.next;
    B.value = Temp.value;
}

void Reverse(int i,int j)//类似冒泡排序、将第一个移动到尾部,第二个移动到倒数第二个。。。
{
    for(int Q = 1; Q<=j-i; Q++)
    {
        for(int P = i; P<=j-Q; P++)
        {
            Swap(Data[P],Data[P+1]);
            int Temp;
            Temp = Data[P].next;
            Data[P].next = Data[P+1].address;
            Data[P+1].next = Temp;
        }
    }
    if(i-1>0)
    {
         Data[i-1].next = Data[i].address;
    }
}

void Print(Node *Data)
{
    int Next = Data[1].address;
    for(int i=1; Next!=-1; i++)
    {
        printf("%05d %d ",Data[i].address,Data[i].value);
        if(Data[i].next<0)
            cout<<Data[i].next;
        else
            printf("%05d",Data[i].next);
        cout<<endl;
        Next = Data[i].next;
    }
}

int main()
{
    int rootAddress,amount,K,rear;
    cin>>rootAddress>>amount>>K;
    Data = new Node[amount+1];
    for(int i=1; i<=amount; i++)
    {
        int address,value,next;
        cin>>address>>value>>next;
        if(next == -1)
        {
            rear = i;
        }
        Data[i].address = address;
        Data[i].value = value;
        Data[i].next = next;
    }
    //Swap(Data[rear],Data[amount]);
    int Next = rootAddress;
    int start = 1;
    for(int i=1; i<=amount&&Next!=-1; i++)
    {
        for(int j=i; j<=amount; j++)
        {
            if(Data[j].address == Next)
            {
                Swap(Data[i],Data[j]);//创建链表
                Next = Data[i].next;
                break;
            }
        }
        if(i%K == 0){
            Reverse(start,i);
            start = i+1;
        }
    }
    Print(Data);
    return 0;
}

过程中要注意:可能有数据时假的(非该链表内地,没有结点的next指向它,所以输出的时候不能把所有数据都输出)

其他一些细节的操作就不赘述了。这里我们回顾一下链表的转置把=-=,这个静态链表的实现不适用于结构体链表,且效率低。

 链表转置:

#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;

struct Node
{
    int value;
    Node *Next;
};
Node *Data;

Node* Reverse(Node *Head)
{
    if(Head == NULL)
        return NULL;
    Node *L,*M;//L、M分别保存下一个结点的next位置和下一个结点
    L = Head;
    M = Head->Next;
    Head->Next = NULL;
    while(M!=NULL){
        Node *R;
        R = M->Next;
        M->Next = L;
        L = M;
        M = R;
    }
    return L;
}

void Print(Node *Head)
{
    while(Head){
        cout<<Head->value<<" ";
        Head = Head->Next;
    }
    cout<<endl;
}

int main()
{
    Node *Head,*Temp;
    Head = new Node;
    Head->value = 1;
    Head->Next = NULL;

    Temp = new Node;
    Temp->value = 2;
    Temp->Next = NULL;
    Head->Next = Temp;

    Print(Head);//输出1 2
    Head = Reverse(Head);
    Print(Head);//输出2 1
    return 0;
}

 

posted @ 2018-09-21 00:57  巴特曼  阅读(163)  评论(0)    收藏  举报