logo

스택이란 무엇입니까?

스택은 다음과 같은 선형 데이터 구조입니다. LIFO(후입선출) 원칙. 스택에는 끝이 하나 있는 반면 큐에는 끝이 두 개 있습니다( 전면 및 후면 ). 포인터가 하나만 포함되어 있습니다. 상단 포인터 스택의 최상위 요소를 가리킨다. 스택에 요소가 추가될 때마다 해당 요소는 스택의 맨 위에 추가되며 해당 요소는 스택에서만 삭제할 수 있습니다. 즉, 스택은 스택의 상단이라고 하는 한쪽 끝에서 삽입과 삭제가 가능한 컨테이너로 정의할 수 있습니다.

스택과 관련된 몇 가지 핵심 사항

  • 실제 스택, 책 더미 등과 같이 동작하기 때문에 스택이라고 합니다.
  • 스택은 미리 정의된 용량을 가진 추상 데이터 유형입니다. 즉, 제한된 크기의 요소를 저장할 수 있습니다.
  • 요소를 삽입하고 삭제하기 위해 어떤 순서를 따르는 데이터 구조이며, 그 순서는 LIFO 또는 FILO일 수 있습니다.

스택 작업

스택은 LIFO 패턴에서 작동합니다. 아래 그림에서 볼 수 있듯이 스택에는 5개의 메모리 블록이 있습니다. 따라서 스택의 크기는 5입니다.

c에서 무작위

스택에 요소를 저장하고 스택이 비어 있다고 가정해 보겠습니다. 아래 그림과 같이 크기 5의 스택을 사용하여 스택이 가득 찰 때까지 요소를 하나씩 푸시합니다.

DS 스택 소개

스택의 크기가 5이므로 스택이 가득 차 있기 때문입니다. 위의 경우 스택에 새 요소를 입력할 때 위에서 아래로 이동하는 것을 관찰할 수 있습니다. 스택은 아래에서 위로 채워집니다.

스택에서 삭제 작업을 수행할 때 다른 쪽 끝이 닫혀 있으므로 진입 및 퇴출에 대한 방법은 단 하나뿐입니다. LIFO 패턴을 따르므로 먼저 입력한 값이 마지막에 제거됩니다. 위의 경우 값 5가 먼저 입력되므로 다른 요소를 모두 삭제한 후에야 제거됩니다.

표준 스택 작업

다음은 스택에 구현된 몇 가지 일반적인 작업입니다.

    푸시():스택에 요소를 삽입하는 작업을 푸시라고 합니다. 스택이 가득 차면 오버플로 상황이 발생합니다.팝():스택에서 요소를 삭제하는 작업을 팝이라고 합니다. 스택이 비어 있으면 스택에 요소가 없다는 의미이며 이 상태를 언더플로 상태라고 합니다.비었다():스택이 비어 있는지 여부를 결정합니다.가득():스택이 가득 찼는지 여부를 결정합니다.'몰래 엿보다():주어진 위치의 요소를 반환합니다.세다():스택에서 사용 가능한 총 요소 수를 반환합니다.변화():주어진 위치의 요소를 변경합니다.표시하다():스택에서 사용 가능한 모든 요소를 ​​인쇄합니다.

푸시 작업

PUSH 작업과 관련된 단계는 다음과 같습니다.

  • 스택에 요소를 삽입하기 전에 스택이 가득 찼는지 확인합니다.
  • 스택에 요소를 삽입하려고 하는데 스택이 가득 차면 과다 상태가 발생합니다.
  • 스택을 초기화할 때 스택이 비어 있는지 확인하기 위해 top 값을 -1로 설정합니다.
  • 새 요소가 스택에 푸시되면 먼저 맨 위의 값이 증가합니다. 즉, 상단=상단+1, 요소는 새 위치에 배치됩니다. 맨 위 .
  • 요소는 다음 위치에 도달할 때까지 삽입됩니다. 최대 스택의 크기.
DS 스택 소개

POP 운영

POP 작업과 관련된 단계는 다음과 같습니다.

자바 hasnext
  • 스택에서 요소를 삭제하기 전에 스택이 비어 있는지 확인합니다.
  • 빈 스택에서 요소를 삭제하려고 하면 언더플로 상태가 발생합니다.
  • 스택이 비어 있지 않으면 먼저 스택이 가리키는 요소에 액세스합니다. 맨 위
  • Pop 작업이 수행되면 상단이 1씩 감소합니다. 즉, 상단=상단-1 .
DS 스택 소개

스택의 응용

스택의 응용 프로그램은 다음과 같습니다.

    기호 균형:스택은 기호의 균형을 맞추는 데 사용됩니다. 예를 들어 다음과 같은 프로그램이 있습니다.
 int main() { cout&lt;<'hello'; cout<<'javatpoint'; } < pre> <p>As we know, each program has <em>an opening</em> and <em>closing</em> braces; when the opening braces come, we push the braces in a stack, and when the closing braces appear, we pop the opening braces from the stack. Therefore, the net value comes out to be zero. If any symbol is left in the stack, it means that some syntax occurs in a program.</p> <ul> <tr><td>String reversal:</td> Stack is also used for reversing a string. For example, we want to reverse a &apos; <strong>javaTpoint</strong> &apos; string, so we can achieve this with the help of a stack. <br> First, we push all the characters of the string in a stack until we reach the null character. <br> After pushing all the characters, we start taking out the character one by one until we reach the bottom of the stack. </tr><tr><td>UNDO/REDO:</td> It can also be used for performing UNDO/REDO operations. For example, we have an editor in which we write &apos;a&apos;, then &apos;b&apos;, and then &apos;c&apos;; therefore, the text written in an editor is abc. So, there are three states, a, ab, and abc, which are stored in a stack. There would be two stacks in which one stack shows UNDO state, and the other shows REDO state. <br> If we want to perform UNDO operation, and want to achieve &apos;ab&apos; state, then we implement pop operation. </tr><tr><td>Recursion:</td> The recursion means that the function is calling itself again. To maintain the previous states, the compiler creates a system stack in which all the previous records of the function are maintained. </tr><tr><td>DFS(Depth First Search):</td> This search is implemented on a Graph, and Graph uses the stack data structure. </tr><tr><td>Backtracking:</td> Suppose we have to create a path to solve a maze problem. If we are moving in a particular path, and we realize that we come on the wrong way. In order to come at the beginning of the path to create a new path, we have to use the stack data structure. </tr><tr><td>Expression conversion:</td> Stack can also be used for expression conversion. This is one of the most important applications of stack. The list of the expression conversion is given below: <pre>Infix to prefix Infix to postfix Prefix to infix Prefix to postfix Postfix to infix</pre> </tr><tr><td>Memory management:</td> The stack manages the memory. The memory is assigned in the contiguous memory blocks. The memory is known as stack memory as all the variables are assigned in a function call stack memory. The memory size assigned to the program is known to the compiler. When the function is created, all its variables are assigned in the stack memory. When the function completed its execution, all the variables assigned in the stack are released. </tr></ul> <hr></'hello';>
메모리 관리:스택은 메모리를 관리합니다. 메모리는 인접한 메모리 블록에 할당됩니다. 모든 변수가 함수 호출 스택 메모리에 할당되므로 메모리를 스택 메모리라고 합니다. 프로그램에 할당된 메모리 크기는 컴파일러에 알려져 있습니다. 함수가 생성되면 모든 변수가 스택 메모리에 할당됩니다. 함수 실행이 완료되면 스택에 할당된 모든 변수가 해제됩니다.