forked from zhuxinquan/Data-Structure-And-Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.8.c
105 lines (98 loc) · 2.21 KB
/
2.8.c
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*************************************************************************
> File Name: 2.8.c
> Author: zhuxinquan
> Mail: [email protected]
> Created Time: 2015年09月21日 星期一 17时11分23秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node * next;
}Node, * LinkList;
LinkList CreatLink(int * count)
{
LinkList head, tail, s;
int x;
tail = head = (LinkList)malloc(sizeof(Node));
scanf("%d", &x);
while(x != -1)
{
s = (Node *)malloc(sizeof(Node));
s->data = x;
s->next = NULL;
(*count)++;
tail->next = s;
tail = s;
scanf("%d", &x);
}
return head;
}
void la_insert_lb(LinkList head_a, LinkList head_b, int i, int len, int j)
{
int k;
Node * pa, * pb, * prea, * preb, * pdel;
prea = head_a;
preb = head_b;
pa = head_a->next;
pb = head_b->next;
for(k = 1; k < i; k++)
{
prea = prea->next;
}
for(k = 1; k < j + 1; k++)
{
preb = preb->next;
}
for(k = 0; k < len; k++)
{
pdel = prea->next;
prea->next = pdel->next;
pdel->next = preb->next;
preb->next = pdel;
preb = pdel;
}
}
void print_link(LinkList head)
{
Node * p = head->next;
while(p)
{
printf("%4d", p->data);
p = p->next;
}
printf("\n");
}
int main(void)
{
int count_a, count_b, i, len, j;
LinkList head_a, head_b;
count_a = 0;
count_b = 0;
printf("input la and -1 is over:\n");
head_a = CreatLink(&count_a);
printf("input lb and -1 is over:\n");
head_b = CreatLink(&count_b);
printf("la:");
print_link(head_a);
printf("lb:");
print_link(head_b);
while(1)
{
printf("input i, len, j(eg: 3,4,2):");
scanf("%d,%d,%d", &i, &len, &j);
if( (i + len > count_b) || (j > count_b) || (i < 1) || (j < 0) )
{
printf("input error!\n");
continue;
}
else{
break;
}
}
la_insert_lb(head_a, head_b, i, len, j);
printf("la:");
print_link(head_a);
printf("lb:");
print_link(head_b);
}