-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSDUT 2118.cpp
68 lines (64 loc) · 1.32 KB
/
SDUT 2118.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct node
{
int data;
struct node *next;
} Node;
int main()
{
int num;
Node *cur,*prev,*head;
prev = (Node*)malloc(sizeof(Node));
head = prev;
while(scanf("%d",&num),num!=-1)
{
cur = (Node*)malloc(sizeof(Node));
cur->data = num;
prev ->next = cur;
prev = cur;
}
cur->next = NULL;
Node *temp;
prev = head->next;
cur = prev ->next;
temp = cur ->next;
for(;prev->next!=NULL;prev = prev ->next)
{
cur->next = prev;
prev = cur;
if(temp !=NULL)cur = temp;
if(temp !=NULL)temp = temp ->next;
}
temp = head->next;
temp->next = NULL;
head = cur;
/*
Node *temp;
prev = head;
for(cur = head->next;cur->next!=NULL;cur = cur->next)
{
if(prev == head)
{
temp = cur->next;
prev = prev->next;
prev->next = NULL;
temp = temp->next;
continue;
}
cur->next = prev;
prev = cur;
cur = temp;
temp = temp->next;
}
cur->next = prev;
head = cur;
*/
for(cur = head;cur!=NULL;cur=cur->next)
{
if(cur->next==NULL) printf("%d\n",cur->data);
else printf("%d ",cur->data);
}
return 0;
}