Important alert: (current site time 5/21/2013 2:16:05 AM EDT)
 

VB icon

Stack implementation using arrays

Email
Submitted on: 4/27/2012 5:22:36 PM
By: Creator_1493  
Level: Intermediate
User Rating: Unrated
Compatibility: Java (JDK 1.1), Java (JDK 1.2), Java (JDK 1.3), Java (JDK 1.4), Java (JDK 1.5)
Views: 1519
(About the author)
 
     performs basic operations on stack. (please let me know if there are errors)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Stack implementation using arrays
// Description:performs basic operations on stack.
(please let me know if there are errors)
// By: Creator_1493
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7025&lngWId=2//for details.//**************************************

// simple operations on stack using array.
 
import javax.swing.JOptionPane;// importing swing class
class stack{
	private static final int size = 20;//declaring and initialising constant 'size' 
	int top;
	int ele[] = new int[size];
	
	stack(){ // initialising stack using constructor of class 
			top = -1; // no elements in stack
	}
	
	
	public void push(){// to insert elements on stack
		String input = JOptionPane.showInputDialog(null, "Enter number you want to insert");
		int info = Integer.parseInt(input);
		if(isfull() ) // checking if stack is full
	JOptionPane.showMessageDialog(null, "stack is full cannot insert", "", JOptionPane.ERROR_MESSAGE);
		else	
	ele[++top] = info; // increment top by one and then, assign data to top index
	} // end of push
	
	private boolean isfull() { // checks if stack is full
		// TODO Auto-generated method stub
		if(top == ele.length - 1)
			return true; // returns true if stack is full
			else
		return false;// returns false if stack is not full
	} // end of is full
	
	
	private boolean isempty(){ // checks if stack is empty
		if (top == -1 )
	return true; // returns true if stack is empty
		else
		return false; // returns false if stack has some elements 
	} // end of isempty
	
	
	public int pop(){// delete an element from top
		int item;
		if(isempty()){ // checking if stack is empty
			 JOptionPane.showMessageDialog(null, "stack is empty cannot delete", "ERROR", JOptionPane.ERROR_MESSAGE);
		 return 0;
		 }
		else
			{
			item = ele[top--]; // decrement top by one
			return item;
			}
	} // end of pop
	
	
	public int peek(){ // to have a look at an element at the top 
		if(isempty() ){ // calls method isempty
			JOptionPane.showMessageDialog(null, "stack is empty", "ERROR", JOptionPane.ERROR_MESSAGE);
		return 0; 
		}
		else
		return ele[top]; // returns element at top
	} // end of peek
	
	
	public void display(){// displays the elements in the stack
		int index = 0;
		String s = "";
		
		if(isempty()) // calls method isempty()
			JOptionPane.showMessageDialog(null, "stack is empty ", "ERROR", JOptionPane.ERROR_MESSAGE);
		else
			{
			while(index <= top)
			 s = s + (index+1) +". "+ele[index++] + "\n";
		
		JOptionPane.showMessageDialog(null, "stack is \n" + s);
		}
	}// end of display
	
 
	public void search(int data){// searches an element in stack
		int index = 0;
		if(isempty()) // calls method is empty
			JOptionPane.showMessageDialog(null, "Stack is empty", "ERROR", JOptionPane.ERROR_MESSAGE);
else{
		while(index <= top){
			if(ele[index] == data){
				JOptionPane.showMessageDialog(null, "Element found at location "+ index);
				break; // if element found then, break out of while loop
			} 
			else
				index++;
		}
		if(index > top) // only case if element is not in stack
		JOptionPane.showMessageDialog(null, "Element " + data +"not found on stack ");
		}
 }// end of search
} // end of class stack
public class Stack_usingarr { // main class 
public static void main(String args[]){
	stack s = new stack(); // creating object of stack class
	int ch = 0;
while(ch < 6){ // while choice is less than six 
	String p = "PRESS";
		String choice = "1.To insert or push\n"+"2.To delete or pop \n"+"3.To search an element\n"+ 
	"4.To traverse stored data \n "+"5.To see element at top of stack"+"\n6.To exit " ;
		
		String c = JOptionPane.showInputDialog(null,p+"\n\n"+choice,"CHOICES",JOptionPane.PLAIN_MESSAGE); // input string 
		ch = Integer.parseInt(c); // converting input string to an integer 
		
		switch(ch){
		case 1:s.push();// calls push method 
			break;
		case 2:int item = s.pop(); // calls pop method 
		JOptionPane.showMessageDialog(null, "Element" + item + " deleted from stack.", "INFORMATION", JOptionPane.INFORMATION_MESSAGE); 
			break;
		case 3:String c1 = JOptionPane.showInputDialog(null,"Enter element you want to search:");
		 int ch1 = Integer.parseInt(c1); 
			 s.search(ch1);// calls search method with parameter as the number to search 
			break;
		case 4:s.display();// calls display method
		break;
		case 5:JOptionPane.showMessageDialog(null, "Element at the top of stack "+s.peek() /*calling peek method*/ ); 
		break;
		case 6:break; // coming out of while loop
			 } // end of switch
		 } // end of while
} //end of main method 
}//end of class stack_usingarr
 
// end


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/17/2013 7:26:16 AMDONNA

theres a lot of error when i paste it on the netbeans.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.