-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskipList.cpp
79 lines (73 loc) · 1.08 KB
/
skipList.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
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <stdlib.h>
void insert(int in);
struct node
{
int data;
struct node* next;
struct node* skip5;
};
struct node* head;
int size=0;
int main()
{
head = (struct node*)malloc(sizeof(struct node));
insert(1);
insert(2);
insert(3);
insert(4);
insert(5);
insert(6);
struct node* cur = head;
while(cur!=NULL)
{
printf("%d",cur->data);
cur = cur->next;
}
printf("\n\n\n");
struct node* pre = head;
while(pre!=NULL)
{
printf("%d",pre->data);
pre=pre->skip5;
}
return 0;
}
void insert(int in)
{
if(size == 0)
{
head->data = in;
size++;
}
else
{
struct node* cur = head;
struct node* pre = head;
int preCount = 0;
while(cur->next!=NULL)
{
if(preCount>=5)
pre = pre->next;
cur=cur->next;
preCount++;
}
cur->next = (struct node*)malloc(sizeof(struct node));
pre->skip5 = cur->next;
cur->next->data = in;
size++;
}
}
/*
struct node* pre5 = NULL;
int preCount =0;
while(cur->next!=NULL)
{
if(preCount==5)
pre5 = head;
if(preCount>=5)
pre5 = pre5->next;
preCount++;
cur = cur->next;
}
*/