알고리즘/알고리즘+자료구조

    [자료구조] 연결리스트 구현하기 (C++)

    C++을 이용해 연결리스트를 구현합니다. #include struct node { int data; node *next; }; struct linked_list { node *head = new node; node *current = new node; linked_list() { current = head; } // 맨 뒤에 노드를 추가 void append(int data) { node *new_node = new node; new_node->data = data; current->next = new_node; current = new_node; } // n번째 노드 뒤에 노드 삽입 void insert(int n, int data) { node *new_node = new node; node *pre..

    [자료구조] 스택 구현하기 (C++)

    문제: https://www.acmicpc.net/problem/10828 10828번: 스택 첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 www.acmicpc.net 스택은 데이터의 입력과 출력이 한쪽 방향으로만 이루어지기 때문에 배열을 이용해서 구현할 수 있습니다. #include #include struct stack { int arr[10000]; int top_index = -1; // arr[top]을 리턴하고 top을 1 감소시킨다. int pop() { if (isEmpty()) return -1; return arr[to..