Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bb89058

Browse files
Create CircularList.c
1 parent f9c89d9 commit bb89058

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

‎DataStructure/CircularList.c‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
typedef int element;
5+
typedef struct Node {
6+
element data;
7+
struct Node* link;
8+
} Node;
9+
10+
Node* insertFirst(Node* head, element data) {
11+
Node* node = (Node*)malloc(sizeof(Node));
12+
node->data = data;
13+
14+
if (head == NULL) {
15+
head = node;
16+
node->link = head;
17+
}
18+
else {
19+
node->link = head->link;
20+
head->link = node;
21+
}
22+
23+
return head;
24+
}
25+
26+
Node* insertLast(Node* head, element data) {
27+
Node* node = (Node*)malloc(sizeof(Node));
28+
node->data = data;
29+
30+
if (head == NULL) {
31+
head = node;
32+
node->link = head;
33+
}
34+
else {
35+
node->link = head->link;
36+
head->link = node;
37+
head = node;
38+
}
39+
40+
return head;
41+
}
42+
43+
void print(Node* head) {
44+
if (head == NULL) {
45+
return printf("[ 리스트가 비었습니다. ]\n");
46+
}
47+
48+
Node* p = head->link;
49+
do {
50+
printf("| %d | -> ", p->data);
51+
p = p->link;
52+
} while (p != head->link);
53+
54+
printf("\n");
55+
}
56+
57+
int main(void) {
58+
Node* head = NULL;
59+
60+
for (int i = 5; i >= 1; i--) {
61+
head = insertFirst(head, i);
62+
print(head);
63+
}
64+
65+
for (int i = 6; i <= 10; i++) {
66+
head = insertLast(head, i);
67+
print(head);
68+
}
69+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /