Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can to copy a stack to another and delete elements without affecting the initial stack??? 1

Status
Not open for further replies.

WomanPro

Programmer
Nov 1, 2012
180
GR
Hello everybody, a have a little trouble.
I am trying to copy the contents of a stack to a new stack and when I delete the top element of the new stack, the initial stack is being affected even I used the new keyword and I don't want that. Any suggestions please? Here is my code I' ve been trying. Any help will be much appreciated. Thank you so much in advanced.

Dim NewSt As New Stack(Of Integer)
Dim InitialSt As Stack(Of Integer)
InitialSt = New Stack(Of Integer)
InitialSt.Push(50)
InitialSt.Push(40)
InitialSt.Push(30)

NewSt = New Stack(Of Integer)
NewSt.Pop()

 

Not quite sure what you're describing follows along with the code you've given. You've created the InitialSt and pushed three items, but instantiate a New NewSt (twice). The last line of the code (NewSt.Pop) will produce an exception since the NewSt stack is empty.

Code:
[red]NewSt = New Stack(Of Integer)
NewSt.Pop()[/red]



Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Excuse me, I forgot that to copy paste it
NewSt = New Stack(Of Integer)
NewSt = InitialSt
NewSt.Pop()

Anyway the problem remains...
 
The NewSt = InitialSt command really just sets NewSt as a reference to the InitialSt stack. When you Pop() from NewSt from that point on, you're actually manipulating InitialSt using a different name.

The Stack(Of T) generic class does not have a Clone method, you'll have to create one yourself. The Stack(Of T) class is implemented as an array, so you can use an Array to store the values of the InitialSt and Push() to another stack:

Code:
Private Function CloneStack(sourceStack As Stack(Of Integer)) As Stack(Of Integer)

        Dim newStack As New Stack(Of Integer)
        Dim myTargetArray As Array = Array.CreateInstance(GetType(Integer), sourceStack.Count)

        sourceStack.CopyTo(myTargetArray, 0)

        [green]' Reverse the array elements to load back in the stack in the correct order[/green]
        Array.Reverse(myTargetArray)

        For Each num In myTargetArray
            newStack.Push(num)
        Next

        Return newStack

    End Function

And your code modified:

Code:
Dim InitialSt As Stack(Of Integer)
InitialSt = New Stack(Of Integer)
InitialSt.Push(50)
InitialSt.Push(40)
InitialSt.Push(30)

Dim NewSt As New Stack(Of Integer)()
NewSt = CloneStack(InitialSt)
NewSt.Pop()

[green]' The InitialSt still has 3 elements[/green]
Debug.Print(InitialSt.Count)


Mark

"You guys pair up in groups of three, then line up in a circle."
- Bill Peterson, a Florida State football coach
 
Respect Mark!!!! Thank you so much for your detailed explanation!!!! Thank you very much
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top