Java program to implement stack and queue using linked list




















It allows programmers to pass the object type the stack will store at run time. A special feature of Java called generics brings us this capability. The above implementation of LinkedStack class takes a type parameter Item which would be replaced with concrete type by the client code when the LinkedStack object will be created as follows, for an example:.

The above code completes the stack implementation using linked list but we definitely will be further interested in implementing iterator for the newly created LinkedStack type, so that we can iterate through the items currently stored in the data structure. Following section implements iterator for LinkedStack class and a driver to run LinkedStack class code.

By definition of stack data structure you would use it as a container where items are added to and removed from one end. But, if you like to make the Stack a usual collection where you can iterate through the stack items, you should implement Iterable and Iterator interfaces as part of your LinkedStack implementation. Java provides a special syntax of for loop also called for-each loop to process arrays and iterable collections.

Any iterable collection has to implement an iterator method that returns an Iterator object. And the class implements Iterator has to implement two methods: hasNext returns true if there are more items to process, false otherwise and next returns an item from the collection.

Interface Iterable is already defined as part of java. Generally, an iterator is an object of a class that implements hasNext , and next as they are declared in Iterator interface. The Iterator interface also declares a remove method but we will not specify anything in its body.

To complete the code first modify the existing signature of interface Stack in Stack. Thereafter modifying Stack interface, we need to modify LinkedStack class as follows:. If you observe the output generated by LinkedStackDemo class, you will find that stack items are processed in reverse order of their insertion.

It is because items pushed at last are popped first. In this tutorial we talked of implementation of stack in Java using linked list. We implemented generic stack in Java using linked list to create a stack of any user defined type. In implementation of stack using linked list memory is used efficiently and no resize operations are required as they are required in array implementation. Hope you have enjoyed reading this tutorial. Python Turtle.

Verbal Ability. Interview Questions. Company Questions. Artificial Intelligence. Cloud Computing. Data Science. Angular 7. Machine Learning. Data Structures. Operating System. Computer Network.

Compiler Design. Computer Organization. Discrete Mathematics. Ethical Hacking. Computer Graphics. Software Engineering. Web Technology. Cyber Security. C Programming. Control System. Data Mining. Data Warehouse. Javatpoint Services JavaTpoint offers too many high quality services. Adding a node to the stack Push operation Adding a node to the stack is referred to as push operation.

Create a node first and allocate memory to it. If the list is empty then the item is to be pushed as the start node of the list. This includes assigning value to the data part of the node and assign null to the address part of the node.

If there are some nodes in the list already, then we have to add the new element in the beginning of the list to not violate the property of the stack. For this purpose, assign the address of the starting element to the address field of the new node and make the new node, the starting node of the list. In order to pop an element from the stack, we need to follow the following steps : Check for the underflow condition: The underflow condition occurs when we try to pop from an already empty stack.



0コメント

  • 1000 / 1000