Stack implementation based on one-way linked list

Source: Internet
Author: User

As we all know about the concept of stack, we can use arrays to implement the first, first, and foremost concepts. But the disadvantage of this implementation is that there is a limit on the quantity, we need to first define the size of this array. therefore, the emergence of the linked list solves the following problem: we need to insert and delete node data in the head of the linked list, because the time complexity of the header is at least O (1 ), if it is at the end, it is necessary to facilitate the tail position first, and then insert and delete, so that the time complexity is programmed O (n ). stack pressure: 1. create a node and direct it to the next node. point the top position of the current stack to the current node's output Stack: 1. get the current node element 2. obtain the next element 3 of the current node element. point the top of the stack to element 4 of the next node and return the node element obtained in step 1. The following is an implementation example/** <p> title: simple title </P> * <p> Description: A Brief Description </P> * <p> copyright: Copyright (c) </P> */ PackageStack; Public ClassNode { PublicNode nextnode; // point to the next node PublicOBJECT element; // The current object PublicNode () {nextnode = Null; Element = Null;} PublicNode (node, object element) {nextnode = node; This. Element = element;}/*** sets the element and returns the original element * @ ParamElement * @ Return*/ PublicObject setele (object element) {object oldelement = This. Element; This. Element = element; ReturnOldelement;}/*** get the current element * @ Return*/ PublicObject getelement (){ ReturnElement;}/*** sets the current element and returns the original element */ PublicObject setelement (object element) {object OBJ = This. Element; This. Element = element; ReturnOBJ;}/*** get the next node * @ Return*/ PublicNode getnextnode (){ ReturnNextnode;}/*** set the next node * @ ParamNextnode */ Public VoidSetnextnode (node nextnode ){ This. Nextnode = nextnode; }}/ ** <p> title: simple title </P> * <p> description: </P> * <p> copyright: Copyright (c) </P> */package stack; public class Stack {public node top; // stack top element public int size; // stack size public stack () {Top = NULL; size = 0 ;} /*** number of elements in the stack * @ return */Public int size () {return size ;} /*** is the stack empty? * @ return */Public Boolean isempty () {return Top = NULL? True: false;}/*** get the top element of the stack * @ return */public object top () {return top. getelement ();}/**** output stack * @ return */public object POP () {If (isempty () {// you can create an exception class in this place, then throw system. out. println ("Stack is empty, cannot pop up"); return NULL;} object OBJ = top. getelement (); // Top = Top of the current element. getnextnode (); // update the size of the first node --; return OBJ;}/***** pressure stack * @ Param element */Public void push (object element) {node = new node (top, element); // create a node in the first part of the linked list and point to the next node Top = node; // set the current node as the stack header size ++ ;}}

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.