数组实现堆栈

在数组实现中,通过使用数组形成堆栈。 关于堆栈的所有操作都是使用数组执行的。 下面来看看如何使用数组数据结构在堆栈上实现每个操作。

1. 在堆栈中添加元素(推送操作)

将元素添加到堆栈顶部称为推送操作。推送操作包括以下两个步骤。
增加变量top,使其现在可以引用到下一个内存位置。
在顶部位置添加元素,这个操作称为在堆栈顶部添加新元素。
当尝试将一个元素插入一个完全填充的堆栈时,堆栈溢出,因此,main()函数必须始终避免堆栈溢出条件。

算法

开始   
    if top = n 那么堆栈满   
    top = top + 1  
    stack (top) : = item;  
结束

时间复杂度为:o(1)

C语言中推送算法的实现 -

void push (int val,int n) //n is size of the stack   
{  
    if (top == n ){
        printf("Overflow\n");   
    }else   
    {  
        top = top + 1;   
        stack[top] = val;   
    }   
}

2. 从堆栈中删除元素(弹出操作)

从堆栈顶部删除元素称为弹出操作。 每当从堆栈中删除一个项目时,变量top的值将增加1。 堆栈的最顶层元素存储在另一个变量中,然后顶部递减1。该操作返回作为结果存储在另一个变量中的已删除值。

当尝试从已经空的堆栈中删除元素时,会发生下溢情况。

算法

开始
    if top = 0 那么堆栈为空;   
    item := stack(top);  
    top = top ? 1;  
结束

时间复杂度为:o(1)

使用C语言实现弹出算法

int pop ()  
{   
    if(top == -1)   
    {  
        printf("Underflow");  
        return 0;  
    }  
    else  
    {  
        return stack[top -- ];   
    }    
}

3. 访问堆栈的每个元素(Peek操作)

Peek操作涉及返回堆栈顶部存在的元素但不删除它。如果尝试返回已经空堆栈中的顶部元素,则可能发生下溢情况。

算法:

开始
    if top = -1 那么堆栈   
    item = stack[top]   
    return item  
结束

时间复杂度为:o(1)

用C语言实现Peek算法

int peek()  
{  
    if (top == -1)   
    {  
        printf("Underflow");  
        return 0;   
    }  
    else  
    {  
        return stack [top];  
    }  
}

完整的C语言实现代码 -

#include <stdio.h>   
int stack[100], i, j, choice = 0, n, top = -1;
void push();
void pop();
void show();
void main()
{

    printf("Enter the number of elements in the stack ");
    scanf("%d", &n);
    printf("*********Stack operations using array*********");
    printf("----------------------------------------------\n");
    while (choice != 4)
    {
        printf("Chose one from the below options...\n");
        printf("1.Push\n2.Pop\n3.Show\n4.Exit");
        printf("Enter your choice \n");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
        {
            push();
            break;
        }
        case 2:
        {
            pop();
            break;
        }
        case 3:
        {
            show();
            break;
        }
        case 4:
        {
            printf("Exiting....");
            break;
        }
        default:
        {
            printf("Please Enter valid choice ");
        }
        };
    }
}

void push()
{
    int val;
    if (top == n)
        printf("Overflow");
    else
    {
        printf("Enter the value?");
        scanf("%d", &val);
        top = top + 1;
        stack[top] = val;
    }
}

void pop()
{
    if (top == -1)
        printf("Underflow");
    else
        top = top - 1;
}
void show()
{
    for (i = top;i >= 0;i--)
    {
        printf("%d\n", stack[i]);
    }
    if (top == -1)
    {
        printf("Stack is empty");
    }
}

完整的Java语言实现代码 -

import java.util.Scanner;

class Stack {
    int top;
    int maxsize = 10;
    int[] arr = new int[maxsize];

    boolean isEmpty() {
        return (top < 0);
    }

    Stack() {
        top = -1;
    }

    boolean push(Scanner sc) {
        if (top == maxsize - 1) {
            System.out.println("Overflow !!");
            return false;
        } else {
            System.out.println("Enter Value");
            int val = sc.nextInt();
            top++;
            arr[top] = val;
            System.out.println("Item pushed");
            return true;
        }
    }

    boolean pop() {
        if (top == -1) {
            System.out.println("Underflow !!");
            return false;
        } else {
            top--;
            System.out.println("Item popped");
            return true;
        }
    }

    void display() {
        System.out.println("Printing stack elements .....");
        for (int i = top; i >= 0; i--) {
            System.out.println(arr[i]);
        }
    }
}

public class Stack_Operations {
    public static void main(String[] args) {
        int choice = 0;
        Scanner sc = new Scanner(System.in);
        Stack s = new Stack();
        System.out.println("*********Stack operations using array*********\n");
        System.out.println("------------------------------------------------\n");
        while (choice != 4) {
            System.out.println("Chose one from the below options...\n");
            System.out.println("1.Push\n2.Pop\n3.Show\n4.Exit");
            System.out.println("Enter your choice \n");
            choice = sc.nextInt();
            switch (choice) {
            case 1: {
                s.push(sc);
                break;
            }
            case 2: {
                s.pop();
                break;
            }
            case 3: {
                s.display();
                break;
            }
            case 4: {
                System.out.println("Exiting....");
                System.exit(0);
                break;
            }
            default: {
                System.out.println("Please Enter valid choice ");
            }
            }
            ;
        }
    }
}

完整的C#语言实现代码 -

using System;  

public class Stack  
{  
    int top;  
    int maxsize=10;  
    int[] arr = new int[10];  
    public static void Main()  
    {  
    Stack st = new Stack();  
    st.top=-1;  
    int choice=0;  
    Console.WriteLine("*********Stack operations using array*********");  
    Console.WriteLine("----------------------------------------------\n");  
    while(choice != 4)  
    {  
        Console.WriteLine("Chose one from the below options...\n");  
        Console.WriteLine("1.Push\n2.Pop\n3.Show\n4.Exit");  
        Console.WriteLine("Enter your choice \n");         
        choice = Convert.ToInt32(Console.ReadLine());  
        switch(choice)  
        {  
            case 1:  
            {   
                st.push();  
                break;  
            }  
            case 2:  
            {  
                st.pop();  
                break;  
            }  
            case 3:  
            {  
                st.show();  
                break;  
            }  
            case 4:   
            {  
            Console.WriteLine("Exiting....");  
                break;   
            }  
            default:  
            {  
                Console.WriteLine("Please Enter valid choice ");  
                break;  
            }   
        };  
    }  
}   

Boolean push ()  
{  
    int val;      
    if(top == maxsize-1)   
    {  

        Console.WriteLine("Overflow");  
        return false;  
    }  
    else   
    {  
        Console.WriteLine("Enter the value?");  
        val = Convert.ToInt32(Console.ReadLine());        
        top = top +1;   
        arr[top] = val;  
        Console.WriteLine("Item pushed");  
        return true;  
    }   
}   

Boolean pop ()   
{   
    if (top == -1)   
    {  
        Console.WriteLine("Underflow");  
        return false;  
    }  

    else  

    {  
        top = top -1;  
        Console.WriteLine("Item popped");  
        return true;  
    }  
}   
void show()  
{  

    for (int i=top;i>=0;i--)  
    {  
        Console.WriteLine(arr[i]);  
    }  
    if(top == -1)   
    {  
        Console.WriteLine("Stack is empty");  
    }  
}  
}

上一篇: 堆栈 下一篇: 堆栈的链表实现