-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoseph_link.c
85 lines (75 loc) · 1.37 KB
/
joseph_link.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
#include "mydecl.h"
#define N 2
typedef struct node{
int no;
struct node* next;
}Node;
//---
Node* creatNode();
//---
int main(void){
Node* head=NULL;
Node *p,*pre,*del;
int i=1,m;
//没有头结点
head=creatNode();
printf("Enter a numebr:\n");
scanf("%d",&m);
pre=NULL;
p=head;
printf("\n----------");
while(p->no){//我就是想这么判断怎么办!!!???
//当被释放后里面有垃圾值也不为空!p指向它后会执行导致bug!!!!!
if(i%m){
pre=p;
p=p->next;
++i;
}
else{//delete this node and print and restore the value of i
//有逻辑错误因为是循环链表,最后一个节点会陷入死循环
del=p;
printf("%3d",del->no);
// del->no=0;
if(pre==NULL){//依次删除
p=p->next;
}
else{
if(p->next==p)//just one node,bug!
//p->no=0;
break;
else{
p=p->next;
pre->next=p;
}
}
free(del);//SIGABRT, double free or corruption (fasttop):
i=1;
}
// printf("\np ====%d\n",p);
}
free(p);
putchar('\n');
return 0;
}
Node* creatNode(){//构造没有头结点的
int i;
Node *pHead=NULL;
Node *pRear=NULL;
Node *pNew=NULL;
for(i=1;i<=N;++i){
pNew=(Node*)malloc(sizeof(Node));
pNew->no=i;
pNew->next=NULL;
if(i==1){
pHead=pNew;
pRear=pNew;
}
else{
pRear->next=pNew;
pRear=pNew;
}
}
//fixed!
pRear->next=pHead;
return pHead;
}