본문 바로가기

C/자료구조

(17)
문과생이 이해한 선형큐 ▲출처 : + 학교 수업 #include #include #define MAX 100 typedef struct { int front; int rear; int data[MAX]; }QueueType; void init(QueueType* q) { q->front = -1; q->rear = -1; } int is_empty(QueueType* q) { if (q->front == q->rear) return 1; else return 0; //false } int is_full(QueueType* q) { if (q->rear == MAX - 1) return 1; else return 0; } void enqueue(QueueType* q, int item) { if (is_full(q)) { fpr..
문과생이 이해한 스택(동적, 구조체) 출처 : + 학교 수업 #include #include typedef int element; typedef struct node { element data; struct node* link; }node; node* top = NULL; void push(element value) { node* new_node = (node*)malloc(sizeof(node)); if (new_node == NULL) { fprintf(stderr, "메모리 할당 에러\n"); return; } else { new_node->data = value; new_node->link = top; top = new_node; } } element pop() { if (top == NULL) { fprintf(stderr, "스택..
문과생이 이해한 스택(정적, 구조체) 출처 : + 학교 수업 #include #include #define MAX 100 typedef struct { int student_no; char name[MAX]; char address[MAX]; }element; element stack[MAX]; int top = -1; int is_empty() { return top == -1; } int is_full() { return top == MAX - 1; } void push(element item) { if (is_full()) { fprintf(stderr, "스택 포화 에러\n"); return; } else stack[++top] = item; } element pop() { if (is_empty()) { fprintf(stderr,..
문과생이 이해한 이중 연결리스트 출처 : 깃 허브 github.com/Shinsungjun/VScodecommit/blob/master/DataStructure/LinkedList/DoublyLinkedList1.c Shinsungjun/VScodecommit first VS code commit. Contribute to Shinsungjun/VScodecommit development by creating an account on GitHub. github.com 이 코드를 가지고 공부햇다. 본 코드는 뒤에서만 추가가 가능하길래 앞에서도 추가할 수 있는 코드를 추가했다. #include #include typedef struct NODE { //여기에 NODE안 써줘면 에러 struct NODE* llink; int data; st..
문과생이 이해한 원형 연결리스트 출처 : + 학교 수업 #include #include typedef struct { int data; struct node* link; }node; void print_node(node* head) { node* p; if (head == NULL) return; p = head->link; do { printf("%d -> ", p->data); p = p->link; } while (p != head); printf("%d->", p->data); } node* insert_first(node* head, int data) { node* p = (node*)malloc(sizeof(node)); p->data = data; if (head == NULL) { head = p; p->link = he..
문과생이 이해한 단일 연결리스트(이중 포인터 사용) #include typedef struct { int data; struct node* link; }node; node* insert_first(node** head, int x) { node* p = (node*)malloc(sizeof(node)); p->data = x; p->link = head; head= p; } int main() { node* head = NULL; //데이터 삽입 및 출력 for (int i = 0; i < 5; i++) { head = insert_first(&head,i); //반환된 head 포인터를 저장. 아니면 null값만 출력됨 print_node(&head); } return 0; } 이중 포인터는 포인터의 포인터를 저장한다. 그래서 head값을 굳이 retu..
문과생이 이해한 단일 연결 리스트(기타 연산- 합병, 역순, 출력/순회, 탐색) 출처 : + 학교 수업 #전체 코드 #include #include typedef struct { int data; struct node* link; }node; node* insert_first(node* head, int x) { node* p = (node*)malloc(sizeof(node)); p->data = x; p->link = head; //head는 노드 생성을 안했으니까 head->link 연산이 안 됨 head= p; return head; } node* insert(node* head, node* pre, int s) { node* p = (node*)malloc(sizeof(node)); p->data = s; p->link = pre->link; pre->link = p; re..
문과생이 이해한 단일 연결 리스트(삭제 연산) 출처 : + 학교 수업 #include #include typedef struct { int data; struct node* link; }node; node* insert_first(node* head, int x) { node* p = (node*)malloc(sizeof(node)); p->data = x; p->link = head; //head는 노드 생성을 안했으니까 head->link 연산이 안 됨 head= p; return head; } node* insert(node* head, node* pre, int s) { node* p = (node*)malloc(sizeof(node)); p->data = s; p->link = pre->link; pre->link = p; return he..