본문 바로가기

C Language

stack을 push 하는 함수 | Function to push for stack

stack을 push 하는 함수 ❘ Function to push for stack

위의 push_top 함수는 새로운 노드를 스택의 맨 위에 추가하는 연산을 수행합니다. 


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

 

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

 

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

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

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

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

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

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

 

결과:
새 노드가 스택의 최상위에 성공적으로 삽입됩니다.
기존 노드들은 한 단계씩 아래로 밀립니다.
스택의 LIFO(Last In, First Out) 특성이 유지됩니다.
이 함수는 스택이 비어있는 경우와 노드가 있는 경우 모두에 대해 올바르게 작동합니다. 비어있는 경우, temp는 bottom 노드를 가리키게 되어 새 노드가 top과 bottom 사이에 정확히 삽입됩니다.

 

 

Push 연산에서 새 노드는 실제로 top->right에 직접 삽입됩니다. 이 과정을 더 정확하게 설명하면 다음과 같습니다:
새 노드 new_node가 생성됩니다.


new_node->right는 현재 top->right가 가리키는 노드를 가리키게 됩니다.

new_node->right = top->right;
현재 top->right가 가리키는 노드의 left를 new_node로 설정합니다 (첫 번째 삽입이 아닌 경우).

if (top->right != bottom)
    top->right->left = new_node;
new_node->left는 top을 가리킵니다.

new_node->left = top;
top->right는 이제 new_node를 가리킵니다.

top->right = new_node;
이렇게 하면 새 노드가 스택의 최상위에 추가되며, 기존의 노드들은 한 단계씩 아래로 밀리게 됩니다. 이 방식으로 LIFO(Last In, First Out) 원칙이 유지됩니다.
정확히는 새 노드가 top과 기존의 top->right 사이에 삽입되는 것입니다. 

 

 

The `push_top` function performs the operation of adding a new node to the top of the stack.

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

Local variables:
- `t_node *top`: Pointer to the top node of the stack (dummy node)
- `t_node *temp`: Pointer to temporarily store the current topmost data node of the stack

Operation process:

top = stack->top;

- Stores the top node of the stack (dummy node) in `top`.

temp = top->right;

- Stores the current topmost data node (or bottom node if empty) in `temp`.

 

top->right = new_node;

- Sets the right link of the top dummy node to the new node.

temp->left = new_node;

- Sets the left link of the existing topmost data node (or bottom node) to the new node.

new_node->left = top;

- Sets the left link of the new node to the top dummy node.

new_node->right = temp;

- Sets the right link of the new node to the existing topmost data node (or bottom node).

Result:
- The new node is successfully inserted at the top of the stack.
- Existing nodes are pushed down one level.
- The LIFO (Last In, First Out) characteristic of the stack is maintained.

This function works correctly for both empty stacks and stacks with existing nodes. In the case of an empty stack, `temp` will point to the bottom node, ensuring that the new node is inserted exactly between the top and bottom nodes.

In the Push operation, the new node is directly inserted at `top->right`. Here's a more accurate description of the process:

1. A new node `new_node` is created.

2. `new_node->right` points to the node currently pointed to by `top->right`:

 

   new_node->right = top->right;

3. The `left` of the node currently pointed to by `top->right` is set to `new_node` (if it's not the first insertion):

   if (top->right != bottom)
       top->right->left = new_node;


4. `new_node->left` points to `top`:

   new_node->left = top;


5. `top->right` now points to `new_node`:

   top->right = new_node;


This process adds the new node to the top of the stack, pushing existing nodes down one level. This method maintains the LIFO (Last In, First Out) principle.

To be precise, the new node is inserted between `top` and the existing `top->right`.