Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Java Program to Implement Stack*/
- import java.util.*;
- /* Class arrayStack */
- class arrStack{
- protected int arr[];
- protected int top, size, len;
- /* Constructor for arrayStack */
- public arrStack(int n){
- size = n;
- len = 0;
- arr = new int[size];
- top = -1;
- }
- /* Function to check if stack is empty */
- public boolean isEmpty(){
- return top == -1;
- }
- /* Function to check if stack is full */
- public boolean isFull(){
- return top == size -1 ;
- }
- /* Function to get the size of the stack */
- public int getSize(){
- return len ;
- }
- /* Function to check the top element of the stack */
- public int peek(){
- if( isEmpty() )
- throw new NoSuchElementException("Underflow Exception");
- return arr[top];
- }
- /* Function to add an element to the stack */
- public void push(int i){
- if(top + 1 >= size)
- throw new IndexOutOfBoundsException("Overflow Exception");
- if(top + 1 < size )
- arr[++top] = i;
- len++ ;
- }
- /* Function to delete an element from the stack */
- public int pop(){
- if( isEmpty() )
- throw new NoSuchElementException("Underflow Exception");
- len-- ;
- return arr[top--];
- }
- /* Function to display the status of the stack */
- public void display(){
- System.out.print("\nSteks = ");
- if (len == 0){
- System.out.print("Tukss\n");
- return ;
- }
- for (int i = top; i >= 0; i--)
- System.out.print(arr[i]+" ");
- System.out.println();
- }
- /* Elementu skaits [-100;100] */
- public void elCount(){
- System.out.print("\nSkaits = ");
- int skaits = 0;
- for (int i = top; i >= 0; i--)
- if (arr[i] >= -100 && arr[i] <= 100) {
- skaits++;
- }
- System.out.print(skaits);
- System.out.println();
- }
- }
- /* Class StackImplement */
- public class Ld3151rdb414 {
- public static void main(String[] args){
- Scanner scan = new Scanner(System.in);
- System.out.println("Toms Škarsts RDBIO 151RDB414");
- System.out.println("25. variants\n");
- System.out.println("---------Vektoriala forma attelots steks---------");
- System.out.println("Ievadiet steka izmeru ");
- int n = scan.nextInt();
- /* Creating object of class arrayStack */
- arrStack stk = new arrStack(n);
- /* Perform Stack Operations */
- char ch;
- do{
- System.out.println("\nSteka operacijas");
- System.out.println("1. pievienot elementu");
- System.out.println("2. dzest elementu");
- System.out.println("3. augsejais elements");
- System.out.println("4. parbaudit vai steks ir tukss");
- System.out.println("5. parbaudit va isteks ir pilns");
- System.out.println("6. steka izmers");
- System.out.println("7. elementi, kas pieder [-100;100]");
- int choice = scan.nextInt();
- switch (choice)
- {
- case 1 :
- System.out.println("Ievadiet skaitli");
- try {
- stk.push( scan.nextInt() );
- }
- catch (Exception e){
- System.out.println("Kluda : " + e.getMessage());
- }
- break;
- case 2 :
- try{
- System.out.println("Izdzestais elements = " + stk.pop());
- }
- catch (Exception e){
- System.out.println("Kluda : " + e.getMessage());
- }
- break;
- case 3 :
- try{
- System.out.println("Augsejais elements = " + stk.peek());
- }
- catch (Exception e){
- System.out.println("Kluda : " + e.getMessage());
- }
- break;
- case 4 :
- System.out.println("Steks tukss = " + stk.isEmpty());
- break;
- case 5 :
- System.out.println("Steks pilns = " + stk.isFull());
- break;
- case 6 :
- System.out.println("Steka izmers = " + stk.getSize());
- break;
- case 7 :
- stk.elCount();
- break;
- default :
- System.out.println("Wrong Entry \n ");
- break;
- }
- /* display stack */
- stk.display();
- System.out.println("\nVai velaties turpinat? (Ievadiet J vai N) \n");
- ch = scan.next().charAt(0);
- } while (ch == 'J'|| ch == 'j');
- scan.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement