본문 바로가기

C Language

bottom을 push하기 | pushing bottom

bottom을 push하기 ❘ pushing bottom

`push_bottom` 함수는 새로운 노드를 스택의 맨 아래에 추가하는 연산을 수행합니다. 이 함수를 상세히 설명하겠습니다:

1. 함수 파라미터:
   - `t_stack *stack`: 노드를 추가할 스택
   - `t_node *new_node`: 스택에 추가할 새 노드

2. 지역 변수:
   - `t_node *bottom`: 스택의 최하위 노드(더미 노드)를 가리키는 포인터
   - `t_node *temp`: 현재 스택의 최하위 데이터 노드를 임시로 저장하는 포인터

3. 연산 과정:
   bottom = stack->bottom;
   - 스택의 최하위 노드(더미 노드)를 `bottom`에 저장합니다.

   temp = bottom->left;
   - 현재 스택의 최하위 데이터 노드(또는 비어있다면 top 노드)를 `temp`에 저장합니다.

   bottom->left = new_node;
   - 최하위 더미 노드의 왼쪽 링크를 새 노드로 설정합니다.

   temp->right = new_node;
   - 기존 최하위 데이터 노드(또는 top 노드)의 오른쪽 링크를 새 노드로 설정합니다.

   new_node->left = temp;
   - 새 노드의 왼쪽 링크를 기존 최하위 데이터 노드(또는 top 노드)로 설정합니다.

   new_node->right = bottom;
   - 새 노드의 오른쪽 링크를 최하위 더미 노드로 설정합니다.

4. 결과:
   - 새 노드가 스택의 최하위에 성공적으로 삽입됩니다.
   - 기존 노드들은 한 단계씩 위로 밀립니다.

Bottom을 push하는 의미:
1. 데이터 구조의 유연성: 이 구현은 스택을 양방향으로 조작할 수 있게 합니다. 일반적인 스택 연산(top에서의 push/pop)뿐만 아니라, 필요한 경우 bottom에서도 연산을 수행할 수 있습니다.

2. 특수 알고리즘 지원: 일부 알고리즘(예: 특정 정렬 알고리즘)에서는 스택의 양 끝에서 조작이 필요할 수 있습니다.

3. 큐 에뮬레이션: 스택의 한쪽 끝에서 push하고 다른 쪽에서 pop하면 큐처럼 동작할 수 있습니다.

4. 효율성: 양방향 연결 리스트를 사용하므로, bottom에서의 연산도 O(1) 시간 복잡도로 수행할 수 있습니다.

이 구현은 스택을 더 유연하게 만들어 다양한 상황에서 효율적으로 사용할 수 있게 합니다.

 

그리고 위에서 `bottom->left`는 `stack->bottom->left`와 동일합니다. 


1. `bottom = stack->bottom;`
   - 이 줄은 스택의 최하위 노드(더미 노드)의 주소를 `bottom` 변수에 저장합니다.
   - `stack`은 `t_stack` 구조체 포인터이고, 그 안에 `bottom`이라는 멤버가 있습니다.
   - `stack->bottom`은 스택의 최하위 더미 노드를 가리킵니다.
   - 이 할당 후, `bottom`은 `stack->bottom`과 동일한 주소를 가리키게 됩니다.

2. `temp = bottom->left;`
   - 이 줄은 최하위 더미 노드의 바로 위 노드(실제 데이터를 가진 최하위 노드 또는 빈 스택의 경우 top 노드)의 주소를 `temp` 변수에 저장합니다.
   - 이 노드는 스택의 실제 최하위 데이터 노드이거나, 스택이 비어있는 경우 top 노드입니다.

이 두 줄의 코드는 다음과 같은 목적을 가집니다:
- 스택의 최하위 구조에 쉽게 접근할 수 있게 합니다.
- 새 노드를 삽입할 때 필요한 두 개의 주요 참조점(bottom 더미 노드와 현재 최하위 데이터 노드)을 설정합니다.
- 코드의 가독성을 높이고 후속 연산을 단순화합니다.

이렇게 함으로써, 새 노드는 `temp`와 `bottom` 사이에 정확히 삽입될 수 있습니다.
   - `bottom->left`는 `stack->bottom->left`와 정확히 같습니다. 왜냐하면 `bottom`은 이미 `stack->bottom`의 값을 가지고 있기 때문입니다.
   - 이 노드는 스택의 실제 최하위 데이터 노드이거나, 스택이 비어있는 경우 top 노드입니다.

이 두 줄의 코드는 다음과 같은 목적을 가집니다:
- 스택의 최하위 구조에 쉽게 접근할 수 있게 합니다.
- 새 노드를 삽입할 때 필요한 두 개의 주요 참조점(bottom 더미 노드와 현재 최하위 데이터 노드)을 설정합니다.
- 코드의 가독성을 높이고 후속 연산을 단순화합니다.

이렇게 함으로써, 새 노드는 `temp`와 `bottom` 사이에 정확히 삽입될 수 있습니다.

 

 

The `push_bottom` function performs the operation of adding a new node to the bottom of the stack. Here's a detailed explanation of this function:

1. Function parameters:
   - `t_stack *stack`: The stack to which the node will be added
   - `t_node *new_node`: The new node to be added to the stack

2. Local variables:
   - `t_node *bottom`: Pointer to the bottom node of the stack (dummy node)
   - `t_node *temp`: Pointer to temporarily store the current bottommost data node of the stack

3. Operation process:
   bottom = stack->bottom;
   - Stores the bottom node of the stack (dummy node) in `bottom`.

   temp = bottom->left;
   - Stores the current bottommost data node (or top node if empty) in `temp`.

   bottom->left = new_node;
   - Sets the left link of the bottom dummy node to the new node.

   temp->right = new_node;
   - Sets the right link of the existing bottommost data node (or top node) to the new node.

   new_node->left = temp;
   - Sets the left link of the new node to the existing bottommost data node (or top node).

   new_node->right = bottom;
   - Sets the right link of the new node to the bottom dummy node.

4. Result:
   - The new node is successfully inserted at the bottom of the stack.
   - Existing nodes are pushed up one level.

The meaning of pushing to the bottom:
1. Data structure flexibility: This implementation allows the stack to be manipulated bidirectionally. In addition to regular stack operations (push/pop at the top), operations can also be performed at the bottom if needed.

2. Support for special algorithms: Some algorithms (e.g., certain sorting algorithms) may require manipulation at both ends of the stack.

3. Queue emulation: By pushing at one end of the stack and popping from the other, it can behave like a queue.

4. Efficiency: Using a doubly-linked list allows operations at the bottom to be performed with O(1) time complexity.

This implementation makes the stack more flexible, allowing it to be used efficiently in various situations.

Furthermore, `bottom->left` is equivalent to `stack->bottom->left`. 

1. `bottom = stack->bottom;`
   - This line stores the address of the stack's bottommost node (dummy node) in the `bottom` variable.
   - `stack` is a `t_stack` structure pointer, which has a member called `bottom`.
   - `stack->bottom` points to the bottommost dummy node of the stack.
   - After this assignment, `bottom` points to the same address as `stack->bottom`.

2. `temp = bottom->left;`
-This line stores the address of the node just above the bottommost dummy node (the actual bottommost data node or the top node if the stack is empty) in the `temp` variable.
   - This node is either the actual bottommost data node of the stack or the top node if the stack is empty.

These two lines of code serve the following purposes:
- They allow easy access to the bottom structure of the stack.
-They set up two key reference points (the bottom dummy node and the current bottommost data node) needed for inserting a new node.
- They improve code readability and simplify subsequent operations.

By doing this, the new node can be precisely inserted between `temp` and `bottom`.
   - `bottom->left` is exactly the same as `stack->bottom->left` because `bottom` already holds the value of `stack->bottom`.
   - This node is either the actual bottommost data node of the stack or the top node if the stack is empty.

These two lines of code serve the following purposes:
- They allow easy access to the bottom structure of the stack.
- They set up two key reference points (the bottom dummy node and the current bottommost data node) needed for inserting a new node.
- They improve code readability and simplify subsequent operations.

By doing this, the new node can be precisely inserted between `temp` and `bottom`.