One stack class

Source: Internet
Author: User
A stack class-general Linux technology-Linux programming and kernel information. The following is a detailed description. Although the Box class is useful in describing the necessary elements of a class, its practical application is not of great value. To show the real functions of a class, this chapter uses a more complex example to describe the powerful functions of the class. If you recall the discussion of object-oriented programming mentioned in chapter 2nd, you will think of one of the most important advantages of object-oriented programming, namely encapsulation of data and code that operates on the data. As you know, in Java, encapsulation is achieved through a mechanism such as a class. When creating a class, you are creating a new data type. You must not only define the data attributes, but also define the code for operating the data. Further, the method defines the control interfaces that are consistent with the data of this type. Therefore, you can use the class through the class method, without worrying about its implementation details or how the internal data of the class is actually managed. In a sense, a class is like a Data Engine ". You can use a joystick to control the use of the engine without knowing how the engine works. In fact, since the details are concealed, its internal work can be changed as needed. As long as your code uses it through the class method, internal details can be changed without negatively affecting the external class.

To look at a practical application of the concepts discussed above, let's develop a typical example of encapsulation: stack ). The stack stores data in an advanced sequence. The stack is controlled through two traditional operations: push and pop ). Add an item to the stack, use the pressure stack to extract an item from the stack, and use the out stack. You will see that it is easy to encapsulate the entire stack mechanism.

The following is a Stack class that implements an integer Stack.

// This class defines an integer stack that can hold 10 values.

Class Stack {int stck [] = new int [10]; int tos;

// Initialize top-of-stack
Stack (){
Tos =-1;
}

// Push an item onto the stack void push (int item) {if (tos = 9) System. out. println ("Stack is full. "); else stck [++ tos] = item ;}

// Pop an item from the stack int pop (){

If (tos <0) {System. out. println ("Stack underflow."); return 0;

}
Else
Return stck [tos --];
}
}

As you can see, the Stack class defines two data items and three methods. The integer stack is stored by the array stck. The subscript of the array is controlled by the variable tos, which always contains the subscript at the top of the stack. Stack () constructor initializes tos to-1, which points to an empty Stack. Push () method to push a project into the stack. Call pop () to retrieve the items that are pushed into the stack (). Since data is accessed through push () and pop (), the fact that the stack is stored in the array is actually irrelevant to the stack used. For example, a stack can be stored in a more complex data structure, such as a linked list, but the interfaces defined by push () and pop () are still the same.

The following example uses TestStack to verify the Stack class. This class generates two integer stacks, stores some values, and extracts them.

Class TestStack {

Public static void main (String args []) {
Stack mystack1 = new Stack ();
Stack mystack2 = new Stack ();

// Push some numbers onto the stack
For (int I = 0; I <10; I ++) mystack1.push (I );
For (int I = 10; I <20; I ++) mystack2.push (I );

// Pop those numbers off the stack
System. out. println ("Stack in mystack1 :");
For (int I = 0; I <10; I ++)

System. out. println (mystack1.pop ());

System. out. println ("Stack in mystack2 :");
For (int I = 0; I <10; I ++)
System. out. println (mystack2.pop ());
}
}

The output of this program is as follows:

Stack in mystack1:
9
8
7
6
5
4
3
2
1
0
Stack in mystack2:
19
18
17
16

15
14
13
12
11
10

As you can see, the content in each stack is separated.

The last point of the Stack class. As it is currently executed, the code outside the Stack class can change the stck array that saves the Stack. Such a Stack is open and prone to misuse or damage. In the next chapter, you will see how to remedy this situation.
Related Article

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.