-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoseph.h
More file actions
74 lines (67 loc) · 1.55 KB
/
Copy pathJoseph.h
File metadata and controls
74 lines (67 loc) · 1.55 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
typedef struct Nodea{
int id;
int secret;
struct Nodea* next;
struct Nodea* last;
}Node,*pNode;
pNode initNode(int num){
pNode node = (pNode)malloc(sizeof(Node));
pNode subNode = node;
for(int i = 0;i < num - 1;i++){
node->next = (pNode)malloc(sizeof(Node));
node->next->last = node;
node->id = i + 1;
node = node->next;
}
node->next = subNode;
subNode->last = node;
node->id = num;
node = node->next;
return node;
}
void deleteNode(pNode node){
node->next->last = node->last;
node->last->next = node->next;
memset(node,0,sizeof(Node));
free(node);
}
int listLength(pNode head){
pNode subNode = head;
int num = 1;
for(;head->next != subNode;head = head->next,num++);
return num;
}
bool insertNode(pNode head,pNode node,int loc){
if(loc == 0 || loc > listLength(head)){
return FALSE;
}
for(int i = 1;i < loc;i++){
head = head->next;
}
head->last->next = node;
node->last = head->last;
node->next = head;
head->last = node;
return TRUE;
}
void deleteAllNode(pNode head){
pNode subNode = head;
while(head->next != subNode){
pNode node = head;
head = head->next;
memset(node,0,sizeof(Node));
free(node);
}
memset(head,0,sizeof(Node));
free(head);
}
void showNode(pNode head,int num){
for(int i = 0;i < num;i++){
head = head->next;
}
printf("%d",head->secret);
}