/*给单链表建环,让尾指针指向第num个结点*/
#include<iostream>
#include<cstdlib>
const int MaxSize = 101;
using namespace std;
typedef struct ListNode{
int data;
struct ListNode *next;
}ListNode;
void BuiltLoopList(ListNode *L,int num)
{
ListNode *p = L;
ListNode *q = NULL;
if(L == NULL||num < 0) return ;
for(int i = 1;i < num;i++)
{
if(p == NULL)
{
return ;
}
p = p->next;
}
q = p;
while(1)
{
q = q->next;
if(q->next == NULL) break;
}
q->next = p;
}
void Print(ListNode *L)
{
ListNode *p;
p = L->next;
while(p != NULL)
{
cout<<p->data<<endl;
p = p->next;
}
}
int main()
{
int num;
ListNode *A;
ListNode *r;
ListNode *s;
A = (ListNode*)malloc(sizeof(ListNode));
r = A;
while(cin>>num)
{
s = (ListNode*)malloc(sizeof(ListNode));
s->data = num;
r->next= s;
r = r->next;
}
r->next = NULL;
BuiltLoopList(A,3);
Print(A);
return 0;
}
#include<iostream>
#include<cstdlib>
const int MaxSize = 101;
using namespace std;
typedef struct ListNode{
int data;
struct ListNode *next;
}ListNode;
void BuiltLoopList(ListNode *L,int num)
{
ListNode *p = L;
ListNode *q = NULL;
if(L == NULL||num < 0) return ;
for(int i = 1;i < num;i++)
{
if(p == NULL)
{
return ;
}
p = p->next;
}
q = p;
while(1)
{
q = q->next;
if(q->next == NULL) break;
}
q->next = p;
}
void Print(ListNode *L)
{
ListNode *p;
p = L->next;
while(p != NULL)
{
cout<<p->data<<endl;
p = p->next;
}
}
int main()
{
int num;
ListNode *A;
ListNode *r;
ListNode *s;
A = (ListNode*)malloc(sizeof(ListNode));
r = A;
while(cin>>num)
{
s = (ListNode*)malloc(sizeof(ListNode));
s->data = num;
r->next= s;
r = r->next;
}
r->next = NULL;
BuiltLoopList(A,3);
Print(A);
return 0;
}