1 Replies - 2193 Views - Last Post: 13 February 2009 - 03:06 PM Rate Topic: -----

#1 klmr3d  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 05-November 08

Stacks with Arrays and Linked Lists

Posted 13 February 2009 - 02:36 PM

Hey everyone,

I have a lot of source code files so please bare with me.

I have made stacks by implementing an array and a linked list. I just want to add a new method now to each to convert the array stack to the linked list stack and likewise I want to convert the linked list stack into an array stack.

Here's what I have so far...

AgeRecord.java

package stackdemo;

import java.io.*;

public class AgeRecord
{
	String lastName;
	String firstName;
	int age;

	public AgeRecord()
	{

	}

	public AgeRecord(int x)
	{
		age = x;
	}

	public AgeRecord(int x, String y)
	{
		age = x;
		lastName = y;
	}

	public AgeRecord(int x, String y, String z)
	{
		age = x;
		firstName = y;
		lastName = z;
	}

	public void setAge(int newValue)
	{
		age = newValue;
	}

	public void setLastName(String newName)
	{
		lastName = newName;
	}

	public void setFirstName(String newName)
	{
		firstName = newName;
	}

	public int getAge()
	{
		return age;
	}

	public String getLastName()
	{
		return lastName;
	}

	public String getFirstName()
	{
		return firstName;
	}

	public String toString()
	{
		return this.getFirstName() + " " + this.getLastName() + " who is " +
				this.getAge() + " years old";
	}
 }



ArrayStack.java
/*
 * Written by P. E. Kenison
 * Written on 1/27/2009
 * This class defines a data structure for holding a an array of object  It
 * is one way in which you can implement the data structure called a stack.
 * You can push onto the stack, pop off the stack, or output the stack.
 * It is a generic stack in that it can hold any type of object.  The object
 * are all stored in an array of object.  The variable top acts as a pointer to
 * the next location of the array where an items can be pushed.
 *
 */

package stackdemo;

public class ArrayStack {
	private int top;
	private int size;
	private Object [] items;

	public ArrayStack() {
		top = 0;
		items = new Object[10];
	}

	public ArrayStack(int s) {
		top = 0;
		size = s;
		items = new Object[s];
	}

	public ArrayStack(Object [] i) {
		top = 0;
		size = items.length;
		items = i;
	}

	/**
	 * @return the top
	 */
	public int getTop() {
		return top;
	}

	/**
	 * @param top the top to set
	 */
	public void setTop(int top) {
		this.top = top;
	}

	/**
	 * @return the items
	 */
	public Object[] getItems() {
		return items;
	}

	/**
	 * @param items the items to set
	 */
	public void setItems(Object[] items) {
		this.items = items;
	}

	/**
	 * @return the size
	 */
	public int getSize() {
		return size;
	}

	/**
	 * @param size the size to set
	 */
	public void setSize(int size) {
		this.size = size;
	}

	public void push(Object obj) {
		if (top == size)
			System.out.println("The stack was full " + obj +
					" could not be inserted");
		else {
			this.getItems()[top++] = obj;
		}
	}

	public Object pop() {
		return this.getItems()[--top];
	}

	public void showStack() {
		System.out.println("Showing the stack using the array data strucutre");
		if (top == 0)
			System.out.println("The stack is empty - There is nothing to print");
		else {
			for (int i = top-1; i >= 0; i--)
				System.out.println(items[i]);
		}
		System.out.println("- - - - - - -\n\n");
	}
}



LinkedStack.java
/*
 * Written by P. E. Kenison
 * Written on 1/27/2009
 * This class defines a data structure for holding object on the stack that is
 * implemented with a link list.  This class is for individual items stored on the
 * stack.  Each item has an object and a pointer to the item below it on the stack
 * You can push onto the stack, pop off the stack, or output the stack.
 * It is a generic stack in that it can hold any type of object
 *
 */
package stackdemo;

public class LinkedStack {
	private Object stackItem;
	private LinkedStack next;

	public LinkedStack() {
	}

	public LinkedStack(Object item) {
		stackItem = item;
	}
	public LinkedStack(Object item, LinkedStack n) {
		stackItem = item;
		next = n;
	}

	/**
	 * @return the stackItem
	 */
	public Object getStackItem() {
		return stackItem;
	}

	/**
	 * @param stackItem the stackItem to set
	 */
	public void setStackItem(Object stackItem) {
		this.stackItem = stackItem;
	}

	/**
	 * @return the next
	 */
	public LinkedStack getNext() {
		return next;
	}

	/**
	 * @param next the next to set
	 */
	public void setNext(LinkedStack next) {
		this.next = next;
	}
}



ShowStacks.java
/*
 * Written by P. E. Kenison
 * Written on 1/27/2009
 * This class declares an object for each type of two data strucutres introduced
 * here that can be used for implelmenting a stack.  All data here is hard coded,
 * that is, there is no variable input (from a file or the kyboard).  All data
 * pushed on to the stacks is fixed in the main method.
 *
 */
package stackdemo;

public class ShowStacks {

	public static void main(String[] args) {
	   ArrayStack myRayStack = new ArrayStack(10);

	   myRayStack.push("this is a test");
	   myRayStack.push(new Integer(34));
	   myRayStack.push("This is a second string");
	   myRayStack.push(new Double(14.21));
	   myRayStack.push(new AgeRecord(60,"Alex","Mavrogeorge"));
	   myRayStack.push(new Integer(15));

	   myRayStack.showStack();

	   StackPointer ptStack = new StackPointer();

	   ptStack.push("First Item");
	   ptStack.push(new AgeRecord(61,"Curt","Schneider"));
	   ptStack.push(new Integer(19));
	   ptStack.push(new AgeRecord(83,"Bob","Dunn"));
	   ptStack.push(new AgeRecord(48,"Evelyn","Sinclair"));
	   ptStack.push(new AgeRecord(91,"Rae","Gross"));

	   ptStack.showStack();

	   ptStack.push(new Integer(75));
	   ptStack.push(new Double(25.52));

	   ptStack.showStack();

	   System.out.println("The following item was popped off of the array stack - "
			   + myRayStack.pop());
	   System.out.println("The following item was popped off of the linked list stack - "
			   + ptStack.pop());
	   System.out.println("The following item was popped off of the linked list stack - "
			   + ptStack.pop());
	   System.out.println("The following item was popped off of the array stack - "
			   + myRayStack.pop());

	   myRayStack.showStack();
	   ptStack.showStack();
	}
}



StackPointer.java
/*
 * Written by P. E. Kenison
 * Written on 1/27/2009
 * This class defines a data structure for holding a pointer to the top of a stack
 * You can push onto the stack, pop off the stack, or output the stack
 * It is a generic stack in that it can hold any type of object
 *
 */

package stackdemo;

public class StackPointer {
	private LinkedStack stackTop;

	public StackPointer() {
		stackTop = null;
	}

	public void push(Object obj) {
		LinkedStack newItem = new LinkedStack(obj);
		if (stackTop == null)
			stackTop = newItem;
		else {
			newItem.setNext(stackTop);
			stackTop = newItem;
		}
	}

	public Object pop() {
		if (stackTop == null)
			return "The stack is empty, there is nothing to pop";
		else {
			LinkedStack temp = stackTop;
			stackTop = stackTop.getNext();
			return temp.getStackItem();
		}
	}

	public void showStack() {
		LinkedStack temp = stackTop;

		System.out.println("Showing the stack using the linked list data structure");
	   while (temp != null) {
			System.out.println(temp.getStackItem());
			temp = temp.getNext();
		}
		System.out.println("- - - - - - -\n\n");
	}
}



Thanks everyone, I reall appreciate your time

Is This A Good Question/Topic? 0
  • +

Replies To: Stacks with Arrays and Linked Lists

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: Stacks with Arrays and Linked Lists

Posted 13 February 2009 - 03:06 PM

I have made stacks by implementing an array and a linked list.

Should be one or the other not both
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1