zoukankan      html  css  js  c++  java
  • Reversing Linked List(根据输入序列对其做部分或全部反转)

    题目:Given a constant KK and a singly linked list LL, you are supposed to reverse the links of every KK elements on LL.

    For example, given LL being 1→2→3→4→5→6, if K = 3 K=3, then you must output 3→2→1→6→5→4;

    if K = 4, you must output 4→3→2→1→5→6.

    //样本测试:

    输入(输入乱序):

    00100 6 4
    00000 4 99999
    00100 1 12309
    68237 6 -1
    33218 3 00000
    99999 5 68237
    12309 2 33218

    输出(根据首行地址输入排序,在排序后进行反转):
    00000 4 33218
    33218 3 12309
    12309 2 00100
    00100 1 99999
    99999 5 68237
    68237 6 -1
    //代码如下:经过调试最后Rear中数据存贮如输出所示
    #include<cstdio>
    #include<iostream>
    #include<vector>
    #include<malloc.h>
    #include<string>
    #include<queue>
    #include<stack>
    using namespace std;
    //创建链表
    typedef struct PNode
    {
        int address;
        int Data;
        int  Next;
        
        PNode *next;
        struct PNode():address(NULL),Data(NULL),next(NULL),Next(NULL){}
    }*Node;
    
    //-------------------------------输入地址为int型数据(我不知输入数据类型,现在只是当其为整数型)--------------
    Node readinput()
    {                
                    int D=0,N=0,K=0;      int D1=0,N1=0,P1=0;int n1;
    
                    Node P,Rear,Head,t;
                    P=(Node)malloc(sizeof(Node));P->next=NULL;
                    Rear=P;t=P;
                    queue<int>a,c;stack<int>b;
                
                    scanf("%d%d%d",&D1,&N1,&P1);
                    n1=N1;
    //-------------------------开始将数据输入到链表中----------------------
                    while(n1--)
                    {
                        scanf("%d%d%d",&D,&N,&K);
                        P->address=D;
                        P->Data=N;
                        P->Next=K;
                        Head=(Node)malloc(sizeof(Node));
                        
                        P->next=Head;
                        P=P->next;
                        Head->next=NULL;
                    
                    }
                    P=NULL;
    //------------开始通过最初的地址进行排序-------------------------------
    //------------查找排序,引入队列---------------------------------------
        
                
                    for(int i=0;i<N1;i++)
                    {        t=Rear;
                        while(t->next!=NULL&&t->Data!=NULL)
                            {
                                if(t->address==D1)
                                    {  
                                    a.push(t->Data);//先压数据,在压地址
                                    a.push(t->address);//将序列的地址压入到了队列中,用链表做有点吃亏
                                                                    
                                    b.push(t->Next);
                               }
                                t=t->next;
                    }
                        D1=b.top();
                        b.pop();
                    }
                    
    
                    for(int i=0;i<a.size();i++)
                    {
                        if(a.front()==P1)
                        {        
                                break;
                        }
                        else
                        {
                        b.push(a.front());
                            a.pop();
                        b.push(a.front());
                            a.pop();
                        }
                    }
                
                        b.push(a.front());
                            a.pop();
                        b.push(a.front());
                            a.pop();
                            
                
                    while(!b.empty())
                        {
                            c.push(b.top());
                            b.pop();
                        }
                
                    while (!a.empty())
                    {        int d=a.front();
                                a.pop();
                            c.push(a.front());
                            a.pop();
                            c.push(d);
    //---------------为了保持顺序一致性---------------------
                    }
                    t=Rear;int tmp=N1;
                    while(tmp--)
                    {
                        if(t->next!=NULL)
                        {
                            t->address=c.front();
                            c.pop();
                            t->Data=c.front();
                            c.pop();
                            if(!c.empty())
                            t->Next=c.front();
                            else
                            {
                                t->Next=-1;
                            }
                            t=t->next;
                        }
                    
                    }
    
    return Rear;
    }
    
    int main()
    {        
                Node p1;
                p1=readinput();
    
    return 0;
    }
    
    
    


  • 相关阅读:
    安卓机-华为安装charles证书
    sed替换文件内容
    升级php5.3.10到php5.6.30
    js 判断设备
    element-ui框架富文本编辑器
    git从主分支上拉取新分支以及提交代码、合并到主分支
    前端项目初始化
    vue路由点击第二次时报错
    js 数组sort方法根据数组中对象的某一个属性值进行排序
    去除一个数组中与另一个数组中的相同元素
  • 原文地址:https://www.cnblogs.com/woainifanfan/p/5415869.html
Copyright © 2011-2022 走看看