Skip to content

Instantly share code, notes, and snippets.

@kamaubrian
Created February 27, 2018 21:11
Show Gist options
  • Save kamaubrian/f032107b60971d21493c01b466a22b20 to your computer and use it in GitHub Desktop.
Save kamaubrian/f032107b60971d21493c01b466a22b20 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
import java.util.LinkedList;
/*
Authors: Brian Kamau, Winny Kiburu
* */
public class Linked {
private static LinkedList<Object> items = new LinkedList<Object>();
private static Scanner scanner = new Scanner(System.in);
public void push(Object item){
/*This method pushes item to LinkedList to the last .. */
System.out.println("Stack Size Before Pushing "+items.size());
items.addLast(item);
System.out.println("Item Pushed"+item+ "\nStack Size After Adding Item "+items.size());
}
public void pop(){
/*This method pops an item from the linkedList at the last element*/
System.out.println("Size of Stack before Destacking: \t"+ items.size());
items.removeLast();
System.out.println("Size of Stack After Destacking: \t" +items.size());
}
public static boolean checkStackEmpty(){
return items.isEmpty();
}
public static void main(String [] args){
Linked linked = new Linked();
int number_of_items;
String items;
System.out.println("Enter Number of Items");
number_of_items = scanner.nextInt();
for(int i = 1;i<=number_of_items;i++){
System.out.println("Enter Item Number "+i);
items = scanner.next();
linked.push(items);
}
for(Object list : linked.items){
System.out.println("Stacked: "+list.toString());
}
if (checkStackEmpty()){
System.out.println("Stack is Empty");
return;
}else{
linked.pop();
for(Object list : linked.items){
System.out.println("Stacked:"+list.toString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment