上后谈爱情

导航

 

题目: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;
}

 



posted on 2016-04-21 10:13  上后谈爱情  阅读(196)  评论(0编辑  收藏  举报