티스토리 뷰
연결리스트-2(LinkedList)
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | #include <stdio.h> #include <malloc.h> typedef int element; typedef struct node { element data; node *link; }node; node *head, *temp, *tail; int N, M, x, y; char ins; void initList() { head = (node *)malloc(sizeof(node)); head->link = NULL; tail = (node *)malloc(sizeof(node)); tail->link = NULL; } void insertList(element data) //가장 끝에 추가 { node *newnode; newnode = (node *)malloc(sizeof(node)); newnode->data = data; if (head->link == NULL) { head->link = newnode; newnode->link = NULL; tail = newnode; } else { newnode->link = NULL; tail->link = newnode; tail = newnode; } } void insertPos(int pos, int num) //pos 위치부터 num개 추가 { temp = head; for (int i = 0; i < pos; i++) temp = temp->link; for (int i = 0; i < num; i++) { node *newnode; newnode = (node *)malloc(sizeof(node)); scanf(" %d", &(newnode->data)); newnode->link = temp->link; temp->link = newnode; temp = temp->link; } } void deleteList(int pos, int num) //pos 위치부터 num개 삭제 { temp = head; for (int i = 0; i < pos; i++) temp = temp->link; for (int i = 0; i < num; i++) { if (temp->link == NULL) return; temp->link = temp->link->link; } } void printList() { temp = head; for (int i = 0; i < 10; i++) { printf("%d ", temp->link->data); temp = temp->link; } } void clearList() { free(head); free(tail); free(temp); } bool isOver() { int len = 0; temp = head; while (temp->link != NULL) { len++; temp = temp->link; if (len > 10) return true; } return false; } int main() { for (int tc = 1; tc <= 1; tc++) { initList(); N = 0, M = 0, x = 0, y = 0, ins = 0; scanf("%d\n", &N); for (int i = 0; i < N; i++) { element data = 0; scanf("%d ", &data); insertList(data); } scanf("%d\n", &M); for (int i = 0; i < M; i++) { if (i == 0) scanf("%c", &ins); else scanf(" %c", &ins); if (ins == 'I') { scanf(" %d %d", &x, &y); insertPos(x,y); } else if (ins == 'D') { scanf(" %d %d", &x, &y); deleteList(x,y); } else if (ins == 'A') { scanf(" %d", &y); for (int j = 0; j < y; j++) { element data = 0; scanf(" %d", &data); insertList(data); } } } printf("#%d ", tc); printList(); printf("\n"); clearList(); } return 0; } | cs |
'STUDY > 자료구조(Data Structures)' 카테고리의 다른 글
자료구조 •String 숫자를 Int로 변환(스트링->정수 변환)• (0) | 2017.12.30 |
---|---|
자료구조 •트리(Tree) -구조체 배열• (0) | 2017.12.29 |
자료구조 •트리(Tree) 개념• (0) | 2017.12.27 |
자료구조 •연결리스트(LinkedList)• (0) | 2017.12.27 |
자료구조 •배열리스트(ArrayList)• (0) | 2017.12.27 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 해싱 충돌
- 겨울방학
- Algorithms
- 자료구조
- string변환
- c언어
- 시험준비
- 알고리즘
- 다이어리
- 트리
- 네트워크
- 컴퓨터네트워크
- 계획
- 패턴 매칭
- int변환
- string숫자
- LinkedList
- datastructures
- Tree
- 보이어-무어
- string개념
- DataStructure
- stringint
- string
- 알고리즘개념
- string int로 변환
- kmp 알고리즘
- open address
- 연결리스트
- atoi()
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함