📜  使用堆栈反转数组

📅  最后修改于: 2021-05-18 01:10:28             🧑  作者: Mango

给定大小为N的数组arr [] ,使用堆栈反转数组的任务。

例子:

迭代和递归方法:请参阅文章反向数组以迭代或递归地解决此问题。
时间复杂度: O(N)
辅助空间: O(1)

基于堆栈的方法:请按照以下步骤解决问题:

  • 初始化堆栈以存储数组元素。
  • 遍历数组并将所有数组元素推入堆栈。
  • >从堆栈中弹出元素,然后将其插入数组。
  • 最后,打印数组。

    下面是上述方法的实现:

    C++
    // C++ program to implement
    // the above approach
    #include 
    using namespace std;
      
    // Structure of stack
    class Stack {
    public:
        // Stores index of top
        // element of a stack
        int top;
      
        // Stores maximum count of
        // elements stored in a stack
        unsigned capacity;
      
        // Stores address of
        // array element
        int* array;
    };
      
    // Function to Initialize a stack
    // of given capacity.
    Stack* createStack(unsigned capacity)
    {
        Stack* stack = new Stack();
        stack->capacity = capacity;
        stack->top = -1;
        stack->array = new int[(stack->capacity
                                * sizeof(int))];
        return stack;
    }
      
    // Function to check if
    // the stack is full or not
    int isFull(Stack* stack)
    {
        return stack->top
               == stack->capacity - 1;
    }
      
    // Function to check if
    // the stack is empty or not
    int isEmpty(Stack* stack)
    {
        return stack->top == -1;
    }
      
    // Function to insert an element
    // into the stack.
    void push(Stack* stack, int item)
    {
      
        // If stack is full
        if (isFull(stack))
            return;
      
        // Insert element into stack
        stack->array[++stack->top] = item;
    }
      
    // Function to remove an element
    // from stack.
    int pop(Stack* stack)
    {
      
        // If stack is empty
        if (isEmpty(stack))
            return -1;
      
        // Pop element from stack
        return stack->array[stack->top--];
    }
      
    // Function to reverse the array elements
    void reverseArray(int arr[], int n)
    {
      
        // Initialize a stack of capacity n
        Stack* stack = createStack(n);
      
        for (int i = 0; i < n; i++) {
      
            // Insert arr[i] into the stack
            push(stack, arr[i]);
        }
      
        // Reverse the array elements
        for (int i = 0; i < n; i++) {
      
            // Update arr[i]
            arr[i] = pop(stack);
        }
      
        // Print array elements
        for (int i = 0; i < n; i++)
            cout << arr[i] << " ";
    }
      
    // Driver Code
    int main()
    {
      
        int arr[] = { 100, 200, 300, 400 };
      
        int N = sizeof(arr) / sizeof(arr[0]);
        reverseArray(arr, N);
        return 0;
    }


    C
    // C program to implement
    // the above approach
    #include 
    #include 
    #include 
    #include 
      
    // Structure of stack
    struct Stack {
      
        // Stores index of top
        // element of a stack
        int top;
      
        // Stores maximum count of
        // elements stored in a stack
        unsigned capacity;
      
        // Stores address of
        // array element
        int* array;
    };
      
    // Function to Initialize a stack
    // of given capacity.
    struct Stack* createStack(unsigned capacity)
    {
        struct Stack* stack
            = (struct Stack*)malloc(
                sizeof(struct Stack));
        stack->capacity = capacity;
        stack->top = -1;
        stack->array
            = (int*)malloc(
                stack->capacity
                * sizeof(int));
        return stack;
    }
      
    // Function to check if
    // the stack is full or not
    int isFull(struct Stack* stack)
    {
        return stack->top
               == stack->capacity - 1;
    }
      
    // Function to check if
    // the stack is empty or not
    int isEmpty(struct Stack* stack)
    {
        return stack->top == -1;
    }
      
    // Function to insert an element
    // into the stack.
    void push(struct Stack* stack, int item)
    {
      
        // If stack is full
        if (isFull(stack))
            return;
      
        // Insert element into stack
        stack->array[++stack->top] = item;
    }
      
    // Function to remove an element
    // from stack.
    int pop(struct Stack* stack)
    {
      
        // If stack is empty
        if (isEmpty(stack))
            return -1;
      
        // Pop element from stack
        return stack->array[stack->top--];
    }
      
    // Function to reverse the array elements
    void reverseArray(int arr[], int n)
    {
      
        // Initialize a stack of capacity n
        struct Stack* stack = createStack(n);
      
        for (int i = 0; i < n; i++) {
      
            // Insert arr[i] into the stack
            push(stack, arr[i]);
        }
      
        // Reverse the array elements
        for (int i = 0; i < n; i++) {
      
            // Update arr[i]
            arr[i] = pop(stack);
        }
      
        // Print array elements
        for (int i = 0; i < n; i++)
            printf("%d ", arr[i]);
    }
      
    // Driver Code
    int main()
    {
      
        int arr[] = { 100, 200, 300, 400 };
      
        int N = sizeof(arr) / sizeof(arr[0]);
        reverseArray(arr, N);
        return 0;
    }


    Java
    // Java program to implement
    // the above approach
    import java.util.*;
      
    // Structure of stack
    class Stack {
      
        // Stores maximum count of
        // elements stored in a stack
        int size;
      
        // Stores index of top
        // element of a stack
        int top;
      
        // Stores address of
        // array element
        int[] a;
      
        // Function to check if
        // the stack is empty or not
        boolean isEmpty()
        {
            return (top < 0);
        }
      
        // Function to Initialize
        // a stack of given capacity.
        Stack(int n)
        {
            top = -1;
            size = n;
            a = new int[size];
        }
      
        // Function to push
        // an element into Stack
        boolean push(int x)
        {
      
            // If Stack is full
            if (top >= size) {
                System.out.println(
                    "Stack Overflow");
                return false;
            }
            else {
      
                // Insert element
                // into stack
                a[++top] = x;
                return true;
            }
        }
      
        // Function to remove an element
        // from stack.
        int pop()
        {
      
            // If stack is empty
            if (top < 0) {
                System.out.println(
                    "Stack Underflow");
                return 0;
            }
      
            // Pop element from stack
            else {
                int x = a[top--];
                return x;
            }
        }
    }
      
    // Driver Code
    class Main {
      
        // Function to reverse the array elements
        public static void reverse(int arr[], int n)
        {
      
            // Initialize a stack of capacity n
            Stack obj = new Stack(n);
      
            for (int i = 0; i < n; i++) {
      
                // Insert arr[i] into the stack
                obj.push(arr[i]);
            }
      
            // Reverse the array elements
            for (int i = 0; i < n; i++) {
      
                // Update arr[i]
                arr[i] = obj.pop();
            }
      
            // Print array elements
            for (int i = 0; i < n; i++) {
                System.out.print(arr[i] + " ");
            }
        }
      
        // Driver function
        public static void main(String args[])
        {
            int n = 4;
      
            // Create a new array
            int[] a = new int[] { 100, 200, 300, 400 };
      
            // Call reverse method
            reverse(a, n);
        }
    }


    输出:
    400 300 200 100
    

    时间复杂度: O(N)
    辅助空间: O(N)