-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path원형연결리스트.c
83 lines (71 loc) · 1.23 KB
/
원형연결리스트.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *link;
}Node;
void insert_first(Node *head, int item)
{
Node *new_node = (Node *)malloc(sizeof(Node));
Node *p = head->link;
new_node->data = item;
if(head->link == NULL)
{
head->link = new_node;
new_node->link = head->link;
}
else
{
while(p->link != head->link)
{
p = p->link;
}
new_node->link = head->link;
p->link = new_node;
head->link = new_node;
}
}
void insert_last(Node *head, int item)
{
Node *new_node = (Node *)malloc(sizeof(Node));
Node *p = head->link;
new_node->data = item;
if(head->link == NULL)
{
head->link = new_node;
new_node->link = head->link;
}
else
{
while(p->link != head->link)
{
p = p->link;
}
new_node->link = p->link;
p->link = new_node;
}
}
void print_node(Node *head)
{
Node *p;
p = head->link;
//while(p != head->link)
do{
printf("%d -> ", p->data);
p = p->link;
}while(p != head->link);
}
int main(void)
{
Node *head = (Node *)malloc(sizeof(Node));
head->link = NULL;
insert_first(head, 1);
insert_first(head, 2);
insert_first(head, 3);
insert_first(head, 4);
insert_first(head, 5);
insert_last(head, 50);
print_node(head);
free(head);
return 0;
}