Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This no longer needs to be here as I have solved it myself
Posted
Updated 19-Dec-13 14:12pm
v4
Comments
sp_suresh 19-Dec-13 18:09pm    
please briefly explain your code
here what u have given isfull size value?....
PIEBALDconsult 19-Dec-13 18:16pm    
"insert the new value to the top of the list and move every other value down one"

You have that backward for one thing.
BillWoodruff 19-Dec-13 18:27pm    
Do keep in mind that we can't see the definition of 'vals.

We also can't see where 'size is defined, or where the 'destroy method is called, where the 'isFull method is called, what you are doing with 'myList ... and so forth.

If you must use an Array, or ArrayList, please clarify your question.

For a solution using a Stack see my response below.
AghaKhan 19-Dec-13 18:29pm    
Where you have created vals. Why are you doing that. This can be done with list<int>. Even your for loop is wrong.
It should be
for (int i = size - 1; i <= 1; I--)
{
vals[i - 1] = vals[i];
}

1 solution

When you assign to vals[0] in 'addFirst, you overwrite whatever value is in it.

It's more work than necessary to turn an Array (or ArrayList ?) into the equivalent of LIFO (last in, first out) stack, when .NET gives you one ready to use in the System.Collections.Generic Library.
// create a new integer Stack
private Stack<int> intStack = new Stack<int>();

// somewhere in your code:

// add a value to the Stack
intStack.Push(100);

// return the value at the top of the Stack
int TopInt = intStack.Peek();

// remove and return the value at the top of the Stack
int TopInt = intStack.Pop();
What you give up by using a Stack is any way to use an index to access a particular element that is not on "top" of the Stack.
 
Share this answer
 
Comments
[no name] 19-Dec-13 18:34pm    
It is a good solution.

If one takes it quite fussy, accessing a particular index is not given up. But accessing a particular index costs a lot.
BillWoodruff 19-Dec-13 18:54pm    
It's my belief that to get a Stack value at an arbitrary location that you'd first have to convert it to an Array. Is there another method ?
[no name] 19-Dec-13 19:11pm    
You are right, that why I wrote "quite fussy" (sorry English is not my native lng).

But there are two costly ways to get a specific index:

a.) pop elements to an temp stack until "ix" is reached, afterwards push back

b.) ElementAt
I think it is really fussy and needs not to be worked out more ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900