Ad Code

Responsive Advertisement

How to create a Stack program in Java

This tutorial How to create a Stack program in Java is very similar to queue program in java as I already posted in this blog. The Stack has to fuctions, the push and pop, push means insert and pop means remove.

If we compare it to Queue, the elements is inserted at the end of the queue while removing the first element. While the Stack program, the element is inserted and deleted only from the end (top).

Image: Incodom
The same with Queue, I created this program with two java class for codes and 1 text file for the data. We used File Scanner and String Tokenizer to read and split row data. In my case, I used the following filenames:

Demo_Stack.java
Stack_Book.java
what_Book.txt




public class Demo_Stack {
/*
Step 1: create a main method - line 28 to 45
Step 2: inside the method, declare file as new File and initialized it with a file path - line 30
Step 3: declare sc as new Scanner and initialized using the file - line 31
Step 4: create a while loop to check if the scanner element - line 32
Step 5: inside the loop, create stl as new String tokenizer - line 33
Step 6: create a while loop to separate the string - line 34 to 41
Step 7: display book using push or pop - line 40
Step 8: display book whatThis method - line 43
*/
public static void main(String[] args) throws Exception{
Stack_Book dBook = new Stack_Book();
File file = new File("D:\\MIX FILES\\Assorted Files\\DataStructure\\DataStructure2\\DataStucture\\src\\DoublyLinkedList2\\bookList.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()){
StringTokenizer st1 = new StringTokenizer(sc.nextLine(), ",");
while (st1.hasMoreTokens()){
String callNum = st1.nextToken();
String title = st1.nextToken();
String author = st1.nextToken();
String publisher = st1.nextToken();
String year = st1.nextToken();
System.out.println(dBook.push(callNum,title,author,publisher,year));
}
}
dBook.whatThis();
}
}
import java.io.File;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
*
* @author Jillian
*/
public class Stack_Book {
/*
Step 1: create a top variable as new Book - line 20
Step 2: create a class Book - line 22 to 31
Step 3: inside class book, declare data as string, next and prev as Book - line 24 to 26
Step 4: create a Book method with parameters - line 29
Step 5: initialized data with the Book parameter, next and prev with null values - line 30 to 32
*/
Book top;
class Book{
String data;
Book next;
Book prev;
Book(String id,String title,String author,String publisher,String year){
data = id + " | " + title + " | " + author + " | " + publisher + " | " + year;
next = null;
prev = null;
}
}
/*
Step 1: create a string method push with parameters - line 38
Step 2: inside the method, create a new string val and initialized with push--> - line 40
Step 3: declare newBook as Book and initialized it with the parameters of the method - line 42
Step 4: create an if else statement to check if the top is not null or empty - line 44 to 52
Step 5: in if block, initialized newBook with top, top with newBook, and val with top.data - line 46 to 49
Step 6: in else block, initialized top with newBook, val with top.data - line 52 to 53
Step 7: return val value - line 56
*/
public String push(String id,String title,String author,String publisher,String year){
String val = "push -->";
Book newBook = new Book(id,title,author,publisher,year);
if (top!=null){
newBook.next = top;
top.prev = newBook;
top = newBook;
val += top.data;
}else{
top = newBook;
val += top.data;
}
return val;
}
/*
Step 1: create a string method named pop
Step 2: inside the method, declare content as String and initialize with pop--> string - line 63
Step 3: declare x as Book and initialized with null - line 65
Step 4: create if statement to check if top is not null - line 67
Step 5: inside if block, initialized x with top, top with top.next and content with x.data - line 70 to 72
Step 6: return content value - line 74
*/
public String pop(){
String content = "pop -->";;
Book x = null;
if(top!=null){
x = top;
top = top.next;
content += x.data;
}
return content;
}
String top(){
return top.data;
}
/*
Step 1: create a new method named whatThis - line 88
Step 2: inside the method, declare file as new File and initialized it with a file path - line 89
Step 3: declare sc as new Scanner and initialized using the file - line 90
Step 4: create a while loop to check if the scanner element - line 91
Step 5: inside the loop, create tks as new String tokenizer - line 93
Step 6: create a while loop to separate the string - line 94 to 102
*/
public void whatThis()throws Exception{
File file = new File("D:\\MIX FILES\\Assorted Files\\DataStructure\\DataStructure2\\DataStucture\\src\\Stock\\what_Stock.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()){
StringTokenizer tks = new StringTokenizer(sc.nextLine(), ",");
while (tks.hasMoreTokens()){
String problem = tks.nextToken();
String implemen = tks.nextToken();
String subject = tks.nextToken();
String programmer = tks.nextToken();
System.out.println("--------------------------------------");
System.out.println("Problem Domain: " + problem);
System.out.println("Implementation: " +implemen);
System.out.println("Subject: " +subject);
System.out.println("Programmer: " +programmer);
}
}
}
}

Post a Comment

0 Comments

Close Menu