1.57. C# Stack

发布时间 :2023-10-12 23:00:05 UTC      

The Stack represents a collection of last-in, first-out objects. Use the stack when you need last-in-first-out access to items. When you add an item to the list called a push element, when you remove an item from the list, itis called a pop-up element.

1.57.1. Methods and properties of the Stack class #

The following table lists some common properties of Stack class:

Attribute

Description

Count

Gets the number of elements contained in the Stack.

The following table lists some common methods of Stack .

Serial number

Method name & description

1

Public virtual void Clear (); removes all elements from the Stack.

2

Public virtual bool Contains (object obj); determines whether an element is in Stack.

3

Public virtual object Peek (); returns the object at the top of the Stack without removing it.

4

Public virtual object Pop (); removes and returns the object at the top of the Stack.

5

Public virtual void Push (object obj); add an object to the top of the Stack.

6

Public virtual object [] ToArray (); copy Stack into a new array.

1.57.2. Example #

The following example demonstrates the use of Stack:

Example #

using System;
using System.Collections;
namespace CollectionsApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack st = new Stack();
            st.Push('A');
            st.Push('M');
            st.Push('G');
            st.Push('W');

            Console.WriteLine("Current stack: ");
            foreach (char c in st)
            {
                Console.Write(c + " ");
            }
            Console.WriteLine();

            st.Push('V');
            st.Push('H');
            Console.WriteLine("The next poppable value in stack: {0}",
            st.Peek());
            Console.WriteLine("Current stack: ");
            foreach (char c in st)
            {
               Console.Write(c + " ");
            }
            Console.WriteLine();
            Console.WriteLine("Removing values ");
            st.Pop();
            st.Pop();
            st.Pop();

            Console.WriteLine("Current stack: ");
            foreach (char c in st)
            {
               Console.Write(c + " ");
            }
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Current stack:
W G M A
The next poppable value in stack: H
Current stack:
H V W G M A
Removing values
Current stack:
G M A

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.