Advertisement
TomSkarsts

[DS] LD3 9./25. variants

Apr 21st, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.30 KB | None | 0 0
  1. /* Java Program to Implement Stack*/
  2.     import java.util.*;
  3.      
  4.     /*  Class arrayStack  */
  5.     class arrStack{
  6.         protected int arr[];
  7.         protected int top, size, len;
  8.  
  9.         /*  Constructor for arrayStack */
  10.         public arrStack(int n){
  11.             size = n;
  12.             len = 0;
  13.             arr = new int[size];
  14.             top = -1;
  15.         }
  16.  
  17.         /*  Function to check if stack is empty */
  18.         public boolean isEmpty(){
  19.             return top == -1;
  20.         }
  21.  
  22.         /*  Function to check if stack is full */
  23.         public boolean isFull(){
  24.             return top == size -1 ;        
  25.         }
  26.  
  27.         /*  Function to get the size of the stack */
  28.         public int getSize(){
  29.             return len ;
  30.         }
  31.  
  32.         /*  Function to check the top element of the stack */
  33.         public int peek(){
  34.             if( isEmpty() )
  35.                 throw new NoSuchElementException("Underflow Exception");
  36.             return arr[top];
  37.         }
  38.  
  39.         /*  Function to add an element to the stack */
  40.         public void push(int i){
  41.             if(top + 1 >= size)
  42.                 throw new IndexOutOfBoundsException("Overflow Exception");
  43.             if(top + 1 < size )
  44.                 arr[++top] = i;
  45.             len++ ;
  46.         }
  47.  
  48.         /*  Function to delete an element from the stack */
  49.         public int pop(){
  50.             if( isEmpty() )
  51.                 throw new NoSuchElementException("Underflow Exception");
  52.             len-- ;
  53.             return arr[top--];
  54.         }    
  55.        
  56.         /*  Function to display the status of the stack */
  57.         public void display(){
  58.             System.out.print("\nSteks = ");
  59.             if (len == 0){
  60.                 System.out.print("Tukss\n");
  61.                 return ;
  62.             }
  63.             for (int i = top; i >= 0; i--)
  64.                 System.out.print(arr[i]+" ");
  65.             System.out.println();
  66.         }    
  67.        
  68.         /*  Elementu skaits [-100;100] */
  69.         public void elCount(){
  70.             System.out.print("\nSkaits = ");
  71.             int skaits = 0;
  72.             for (int i = top; i >= 0; i--)
  73.                 if (arr[i] >= -100 && arr[i] <= 100) {
  74.                     skaits++;
  75.                 }
  76.             System.out.print(skaits);
  77.             System.out.println();
  78.         }  
  79.     }
  80.    
  81.     /*  Class StackImplement  */
  82. public class Ld3151rdb414 {
  83.  
  84.     public static void main(String[] args){
  85.         Scanner scan = new Scanner(System.in);
  86.         System.out.println("Toms Škarsts RDBIO 151RDB414");
  87.         System.out.println("25. variants\n");
  88.         System.out.println("---------Vektoriala forma attelots steks---------");
  89.         System.out.println("Ievadiet steka izmeru ");
  90.         int n = scan.nextInt();
  91.        
  92.         /* Creating object of class arrayStack */
  93.         arrStack stk = new arrStack(n);
  94.  
  95.         /* Perform Stack Operations */
  96.         char ch;
  97.         do{
  98.             System.out.println("\nSteka operacijas");
  99.             System.out.println("1. pievienot elementu");
  100.             System.out.println("2. dzest elementu");
  101.             System.out.println("3. augsejais elements");
  102.             System.out.println("4. parbaudit vai steks ir tukss");
  103.             System.out.println("5. parbaudit va isteks ir pilns");
  104.             System.out.println("6. steka izmers");
  105.             System.out.println("7. elementi, kas pieder [-100;100]");
  106.             int choice = scan.nextInt();
  107.             switch (choice)
  108.             {
  109.             case 1 :
  110.                 System.out.println("Ievadiet skaitli");
  111.                 try {
  112.                     stk.push( scan.nextInt() );
  113.                 }
  114.                 catch (Exception e){
  115.                     System.out.println("Kluda : " + e.getMessage());
  116.                 }                        
  117.                 break;                        
  118.             case 2 :
  119.                 try{
  120.                     System.out.println("Izdzestais elements = " + stk.pop());
  121.                 }
  122.                 catch (Exception e){
  123.                     System.out.println("Kluda : " + e.getMessage());
  124.                 }    
  125.                 break;                        
  126.             case 3 :        
  127.                 try{
  128.                     System.out.println("Augsejais elements = " + stk.peek());
  129.                 }
  130.                 catch (Exception e){
  131.                     System.out.println("Kluda : " + e.getMessage());
  132.                 }
  133.                 break;                        
  134.             case 4 :
  135.                 System.out.println("Steks tukss = " + stk.isEmpty());
  136.                 break;                
  137.             case 5 :
  138.                 System.out.println("Steks pilns = " + stk.isFull());
  139.                 break;                
  140.             case 6 :
  141.                 System.out.println("Steka izmers = " + stk.getSize());
  142.                 break;    
  143.             case 7 :
  144.                 stk.elCount();
  145.                 break;
  146.             default :
  147.                 System.out.println("Wrong Entry \n ");
  148.                 break;
  149.             }
  150.             /* display stack */
  151.             stk.display();            
  152.             System.out.println("\nVai velaties turpinat? (Ievadiet J vai N) \n");
  153.             ch = scan.next().charAt(0);
  154.         } while (ch == 'J'|| ch == 'j');
  155.         scan.close();
  156.     }
  157.    
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement