-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01-linkedlist-create.c
129 lines (105 loc) · 2.09 KB
/
01-linkedlist-create.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "stdafx.h"
#include "LinkedList.h"
/*
create from console input
*/
LinkList CreateByTailConsole() {
LinkList head = (Node *)malloc(sizeof(Node));
Node *p;
Node *rear; // rear pointer
char ch;
rear = head;
while ((ch = getchar()) != '\n') {
p = (Node *)malloc(sizeof(Node));
p->data = ch;
rear->next = p; // point prev to p;
rear = p; // poin rear to p;
}
rear->next = NULL;
return head;
}
LinkList CreateByHead(int data[], int len) {
LinkList head = (Node *)malloc(sizeof(Node));
Node *p, *r;
p = head;
r = head;
int i = 0;
for (i = 0;i < len;i++) {
p = (Node *)malloc(sizeof(Node));
p->data = data[i];
r->next = p;
r = p;
}
r->next = NULL;
Node *dummy = head;
head = head->next;
dummy->next = NULL;
free(dummy);
return head;
}
/*
create by tail
*/
LinkList CreateByTailWithHead(int input[], int len) {
Node *head, *p, *r;
head = (Node *)malloc(sizeof(Node));
r = head;
int i = 0;
for (i = 0;i < len;i++) {
p = (Node *)malloc(sizeof(Node));
p->data = input[i];
r->next = p;
r = p;
}
r->next = NULL;
return head;
}
LinkList CreateByTail(int input[], int len) {
Node *head, *p, *r;
head = (Node *)malloc(sizeof(Node));
head->data = input[0];
head->next = NULL;
r = head;
int i = 0;
for (i = 1;i < len;i++) {
p = (Node *)malloc(sizeof(Node));
p->data = input[i];
r->next = p;
r = p;
}
r->next = NULL;
return head;
}
LinkList CreateCycleListByTail(int input[], int len) {
Node *head, *p, *r;
head = (Node *)malloc(sizeof(Node));
head->data = input[0];
head->next = NULL;
r = head;
int i = 0;
for (i = 1;i < len;i++) {
p = (Node *)malloc(sizeof(Node));
p->data = input[i];
r->next = p;
r = p;
}
r->next = head;
return head;
}
Dnode* CreateDoublyList(int input[], int len){
Dnode *head, *fast, *slow;
head = (Dnode *)malloc(sizeof(Dnode));
slow = head;
slow->data = input[0];
slow->next = NULL;
slow->prev = NULL;
int i;
for (i = 1;i < len;i++) {
slow->next = fast = (Dnode *)malloc(sizeof(Dnode));
fast->data = input[i];
fast->prev = slow;
slow = slow->next;
}
fast->next = NULL;
return head;
}