stack of data structures

Source: Internet
Author: User
Tags constructor data structures

stacks and queues are two table-oriented data structures that provide an easy-to-understand abstraction. The data in the stack can only be added and deleted at one end of the table, whereas the data in the queue is added at the end of the table and deleted at the other end of the table. Stacks are widely used in implementations of any programming language that evaluates from an expression to a processing function call. Queues are used in priority-sensitive operating systems for handling and simulating real-world events, such as queues at bank teller counters and elevator operations in buildings. The C # language provides two types for using these data structures: the Stack class and the queue class.

1. Stack. Stacks are one of the most frequently used data structures. This defines the stack as a list of data items that can be accessed only from the end of the table. This end of accessible access is referred to as the top of the stack. The standard model of the stack is the cafeteria's plate heap. People always take the plate from the top, and when the dishwasher or the handyman puts the plate back on the plate, it is placed on top of the pile. The stack is a well-known last-in-first-out (LIFO) data structure. The most basic of the stack is to add data items to the stack and delete data items from the stack. The Push (stack) operation is to add data items to the stack. The data item is taken out of the stack by a pop (out of the stack) operation. Another basic operation of the stack is to look at the data items at the top of the stack. The POP operation returns the data item at the top of the stack, but this action also removes the data item from the stack. If you just want to see the data item at the top of the stack instead of actually removing it, there is an operation called PEEK (FETCH) in the C # language. Of course, this operation may take other names (such as top) in other languages and implementations. The stack, stack, and fetch are the basic operations that are performed when you use the stacks. However, there are other actions that need to be performed and the properties that need to be checked. Removing all data items from the stack is a useful operation. You can empty the stack by calling the clear (clear) operation. In addition, it is useful to know the number of data items in the stack at any time. This can be achieved by invoking the count (count) property. Many implementations have stackempty methods. This method returns a true or False value based on the state of the stack, but can also be used with the Count property for the same purpose. The. NET Framework's stack class implements all of these operations and properties, even more. But before we discuss how to use them, let's take a look at how to implement a stack if there is no stack class.

(1). The implementation of the Stack requires a potential structure to hold the data. Since there is no need to worry about resizing the table when the new data entry is in the stack, choose ArrayList. Because the C # language has such a powerful object-oriented programming feature, the stack is implemented as a class. This class is known as Cstack. A constructor method and a method for the above mentioned actions are also included here. To illustrate the procedures implemented in the C # language, the Count property is implemented as a property. The most important variable required is the ArrayList object used to store the stack data item. In addition, the other data to be concerned about is the top of the stack. This will be handled with a simple integer variable to provide an index-like capability. When instantiating a new Cstack object, the initial value of this variable is set to-1. Each time a new data item is pressed into the stack, the variable is added 1. The constructor method only completes the operation to initialize the index variable to-1. The first method of implementation is push. The program calls Arraylsit's Add method and adds the value passed to it to the ArrayList. The Pop method accomplishes three things: Call the RemoveAt method to fetch the data item at the top of the stack (out of ArrayList), the index variable to decrement the 1 operation, and the object that eventually returns the stack. The Peek method is implemented by calling the Item method that contains the index variable as a parameter. The Clear method simply calls the same method in the ArrayList class. Since there is no need to burst the number of data items on the stack, the Count property is written here as a read-only property. The code looks like this:[CSharp]  View plain copy class cstack   {        private int  p_index;        private ArrayList list;         public cstack ()         {              list = new arraylist ();              p_index = -1;         }        public int count         {             get             {                   return list. count;             }        }         Public void push (Object item)         {              list. ADD (item);             p_index++;         }        public object pop ()         {             object  obj = list[p_index];              List. RemoveAt (p_index);             p_index--;              return obj;         }        Public void clear ()         {              list. Clear ();             p_index = -1;         }        public object peek ()         {              return list[p_index];        }  }  

      use palindrome test as follows [CSharp]   View plain copy static void  Main (String[] args)    {        CStack alist =  New cstack ();        string ch;         string word =  "sees";        bool  ispalindrome = true;        for  (int x = 0;  x < word. length; x++)              alist.push (Word. Substring (x, 1));        int pos = 0;         while  (alist.count > 0)         {   

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.