-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreport3-1.c
More file actions
111 lines (89 loc) · 1.9 KB
/
Copy pathreport3-1.c
File metadata and controls
111 lines (89 loc) · 1.9 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
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
// 원형 연결리스트 - 리포트 3-1 (deleteLast 추가)
#include <stdio.h>
#include <stdlib.h>
typedef int element;
typedef struct ListNode
{
element data;
struct ListNode *link;
}ListNode;
typedef struct
{
ListNode *tail; // head : tail->link
}ListType;
void init(ListType *L) {
L->tail = NULL;
}
void insertFirst(ListType *L, element e) {
ListNode *node = (ListNode*)malloc(sizeof(ListNode));
node->data = e;
if(L->tail==NULL) {
L->tail = node;
node->link = L->tail;
}
else {
node->link = L->tail->link;
L->tail->link = node;
}
}
void insertLast(ListType *L, element e) {
ListNode *node = (ListNode*)malloc(sizeof(ListNode));
node->data = e;
if(L->tail==NULL) {
L->tail = node;
node->link = node;
}
else {
node->link = L->tail->link;
L->tail->link = node;
L->tail = node;
}
}
element deleteLast(ListType *L) { //구현 하는 것 과제
ListNode *p = L->tail;
ListNode *q = p->link;
element e = p->data;
while (q->link!=p)
{
q = q->link;
}
q->link = p->link;
L->tail = q;
free(p);
return e;
}
element deleteFirst(ListType *L) {
ListNode *p = L->tail;
ListNode *q = p->link;
element e = q->data;
if(p==q) {
L->tail = NULL;
free(p);
return e;
}
else {
p->link = q->link;
free(q);
return e;
}
}
void print(ListType *L) {
ListNode *p = L->tail->link;
while (L->tail != p)
{
printf("[%d] => ", p->data);
p = p->link;
}
printf("[%d] => ...", p->data);
printf("\n\n");
}
int main() {
ListType L;
init(&L);
insertFirst(&L, 10); insertFirst(&L, 20); insertFirst(&L, 30);
insertLast(&L, 40); insertLast(&L, 50); insertLast(&L, 60);
print(&L);
deleteLast(&L);
print(&L);
return 0;
}