Education Menu

 

Features Menu

 

Additional Menu

 

Now Online

We have 60 guests online
 

@edulanka

 

Follow US

Twitter You Tube Google Plus
Pinterest Facebook EduLanka Blog

 

Information Zone

eduLanka Sinhala

Welcome to eduLanka.LK, Largest online education web site in Sri Lanka & most popular educational web site among Sri Lankan students. EduLanka, Best nonprofit & educational website is provided large numbers of online lessons, education related course information, teachers information, E books, examination result alerts, goverment job alerts, tuition class information, career guide for school leavers and more resources related to education in Sri Lanka. Edulanka provides education related resources from primary education, O-Level & A-Level education, University & Postgraduate education and vocational training education.

EduLanka IDN Domains http://එඩියුලංකා.ලංකා http://எடியுலங்கா.இலங்கை


jobs Mathematics SLAS Exam Dharma Education Results



 

Inside Online School

Share Us

Home Page Information Technology Java Programing (ජාවා වැඩසටහන්කරණය)

Link List based STACK java code
Wednesday, 08 September 2010 08:55

Java Linked List based Stack Program

 

Stack is LIFO (Last In First Out) data structure algorithm. Stack can be represent in any programing language. Following source code is written in Java programing language. And also stack can be represent in Array based or Linked list based. This program shows you linked list based.

 


 




/*eduLanka Online education web site of Sri Lanka
-------- w w w . e d u L a n k a . c o m ------------
java programs for University students - who follow courses - BIT - SLIIT and more
find more lessons in http://www.edulanka.lk This e-mail address is being protected from spambots. You need JavaScript enabled to view it
this programs under GNU licence
*/

import TerminalIO.*;
import java.io.*;


class ListNode{

Object element;
ListNode next;
public ListNode(Object ele){
this(ele,null);
}

public ListNode(Object ele,ListNode n){
element=ele;
next=n;
}
}



class StackLt{
ListNode top;
ListNode newnode;
public StackLt(){
top=null;
}



public boolean isEmpty(){
return top==null;
}

 


public void push(Object x){
newnode = new ListNode(x,top);
top=newnode;
}




public Object pop(){
Object ele="Error";

if(isEmpty()){
System.out.println("Stack Error");}

else{
ele=top.element;
top=top.next;
}
return ele;
}




public void displayList(){
ListNode current=top;
if(isEmpty())
System.out.println("List Empty");
else{
while(current!=null){
System.out.println(current.element+" ");
current=current.next;
}
}
}
}




public class StackList{
public static void main(String arg[]){
int num;


/*
String S=" ";
System.out.println("Enter Your Text");
S=input.readLine();
for(int i=0;i<S.length(); i++){
mystk.push(S.charAt(i));
*/

StackLt mystk=new StackLt();
KeyboardReader input=new KeyboardReader();

for(int i=0;i<4; i++){
System.out.print("Enter Numbers : ");
int x=input.readInt();
mystk.push(x);
}

System.out.println("\n After Inserting");
mystk.displayList();

System.out.println("\n Deleted Element="+mystk.pop());
System.out.println("\n After deleting List Elements");
mystk.displayList();

}
}



Array based and Linked list based Java Theory also available in eduLanka.lk
Last Updated on Wednesday, 08 September 2010 09:13
 

Add comment

Add comments relevant to this Article. මෙම ලිපියට අදාලව පමණක් අදහස් එකතු කරන්න.


Security code
Refresh

16.jpg

Download Toolbar

 
 

Most Popular Articles

Latest Articles

Copyright © 2005 - 2016 (ඉස්කෝලේ) eduLanka Online Education School of Sri Lanka