Taking baby-developer steps
자료구조 3 -> 새그먼트 오류 본문
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int data;
struct Node *prev;
struct Node *next;
} Node;
Node *head, *tail;
void insert(int data){
Node *node = (Node*)(malloc(sizeof(Node)));
node->data = data;
Node *cur;
cur = head->next;
while(cur->data < data && cur != tail){
cur = cur->next;
}
Node* front = cur->prev;
front->next = node;
node->prev = front;
cur->prev = node;
node->next = cur;
}
void removeFront(){
Node *front = head->next;
head->next = front->next;
Node *newFront = front->next;
newFront->prev = head;
free(front);
}
void showAll(){
Node *cur = head->next;
while(cur != tail){
printf("%d ", cur->data);
cur = cur->next;
}
}
int main(void){
head = (Node*)malloc(sizeof(Node));
tail = (Node*)malloc(sizeof(Node));
head-> next = tail;
head-> prev = head;
tail-> next = tail;
tail-> prev = head;
insert(7);
insert(1);
insert(9);
insert(2);
insert(0);
insert(14);
removeFront();
showAll();
system("pause");
return 0;
}
'Logs > 학습 log' 카테고리의 다른 글
2022.01.27. gnl (0) | 2022.01.27 |
---|---|
2022.01.26. gnl과제 하는 중... (0) | 2022.01.26 |
2022.01.25. 학습 log (0) | 2022.01.25 |
2022.01.24. 학습 log (0) | 2022.01.24 |
C언어_자료구조 - 2. 배열기반 리스트 - 특정한 위치에 원소를 추가하는 addAt()함수 (0) | 2021.04.04 |