Results 1 to 24 of 24

Thread: [RESOLVED] [02/03] Stacks using Push and Pop

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    12

    Resolved [RESOLVED] [02/03] Stacks using Push and Pop

    Hi Everyone,
    I trying to write a program in Visual Basic .net using the GUI environment using a textbox, two buttons "Push", and "Pull", and a label that displays whether the item was pushed or popped, and show how many the stack contains
    I am wanting to create a stackarray that when a name is entered in the text box it will with "push" or "pull" depending on which button is clicked., the name onto the stack and keep a count of how many items is on the stack, which cannot have more than 100 names. I am having a hard time figuring the "push" and "pop" part and how to keep up with items in the list.
    Any information will be appreciated. computer_lady44

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: [02/03] Stacks using Push and Pop

    Are you aware that .NET already has a Stack class? If you are but you still want to write one yourself then I'd guess that this is homework. In that case we'd normally want to see your attempt before helping too much, and then mostly with advice rather than code.

    There are numerous ways you could implement this yourself. Do you have to use an array?

  3. #3
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: [02/03] Stacks using Push and Pop

    Are you trying to create a stack class or use the existing stack class in .NET?

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    12

    Re: [02/03] Stacks using Push and Pop

    Hi
    I am wanting to create a stack class using an array. I have already created the class ... a class called StackA, and a class called Customer.
    I have a push function already ....
    VB Code:
    1. Public Function Push(byRef item as Customer) as Boolean
    2.  If Size < 99  then
    3.  StackA(Size)=item
    4.  Size =Size +1
    5.  Return True
    6. End if
    7. Return False
    8. End Function
    this function will "Push" a Customer name, id # and balance onto the stack. This stack cannot accept more than 100 customers My problem is when I create the form and code the 'Push" button, I think I am having a problem calling my function. Because when I put

    dim n as Customer
    s.push(n)
    lblOutput.text=".......has been pushed onto the stack"

    I get an error message
    I don;t even know where to begin with my "Pop" button.
    Any information will be appreciated.
    thanks
    computer_lady44

  5. #5
    Fanatic Member Jumpercables's Avatar
    Join Date
    Jul 2005
    Location
    Colorado
    Posts
    592

    Re: [02/03] Stacks using Push and Pop

    What error message do you get?

    For Pop create a Function that returns the next element in the array. Remember that Stacks are last-In-First-Out (LIFO) meaning that last element you placed into your array should be the next element you get out.

    Depending on how you are filling your array it should either be the first element or the last.

    C# - .NET 1.1 / .NET 2.0

    "Take everything I say with a grain of salt, sometimes I'm right, sometimes I'm wrong but in the end we've both learned something."
    _____________________
    Regular Expressions Library
    Connection String
    API Functions
    Database FAQ & Tutorial

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: [02/03] Stacks using Push and Pop

    Please tell us what the error message is. They do give you the message as a diagnostic tool you know. The problem could have something to do with the fact that you have not assigned an object to the 'n' variable before calling Push. Also, there is no need to pass the argument by reference. You aren't assigning a new object to the variable within the method so by value is fine.

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    12

    Re: [02/03] Stacks using Push and Pop

    Hi
    thanks for replying. The error message did have something to do with me assigning my instance variables, but I was able to fix the problem, I think. Anyway as far as the "pop" function, the first name entered will be the last name out, the last name entered will be the first one out. I am not sure how to set this up. Any information will be appreciated.
    computer_lady44

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    12

    Re: [02/03] Stacks using Push and Pop

    This is what I have, and the error message I get:

    VB Code:
    1. Private Sub btnPush_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPush.Click
    2.         Dim n As Customer
    3.         Dim b As Single  'this variable is for the "balance" instance variable
    4.         Dim i, j As String  'this is for the "custNo, Name, instance variable
    5.  
    6.         b = Val(Me.txtBalance.Text)
    7.         n = New Customer(Me.txtNbr.Text, Me.txtName.Text, b)
    8.         s.Push(n)
    9.  
    10.         Me.lblOutput.Text = (c.ToString & "has been pushed onto the stack")
    And this is the error message I get:
    An unhandled exception of type 'System.NullReferenceException' occurred in CS 103-4 HW 03 Project.exe
    Additional information: Object reference not set to an instance of an object.

  9. #9
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [02/03] Stacks using Push and Pop

    VB Code:
    1. s.Push(n)

    Where is s defined? Is there a 'new' keyword before its object?

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    12

    Re: [02/03] Stacks using Push and Pop

    If I understand you correctly, I need to define a new stack where
    s=New Stack. Is this what you mean? If not, could you explain.
    thanks

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    12

    Re: [02/03] Stacks using Push and Pop

    Ok, I got my "Push" button working, which with the advice of everyone, I was guided in the right direction. I defined a new stack. Thanks.

    Now could someone guide me in coding my "Pop" button and a function or procedure to count how many has been 'Pushed" on the stack or "popped' off the stack.
    any information and help will be appreciated
    computer_mom44

  12. #12
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: [02/03] Stacks using Push and Pop

    It really depends how you're storing the values. If you're using a fixed-size array as you indicated earlier then you will need to use a variable to store the number of items in the stack, which will basically be one greater than the index of the last item pushed. When you want to pop an item you simply decrement that variable and then get the element at that index in the array.

  13. #13

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    12

    Re: [02/03] Stacks using Push and Pop

    Hi Everyone...
    I am still trying to implement this stack using an array. I think I have pretty much set up the Push and Pop function, but I am not sure how to count how many items are in the stack. When I run the program, "Push" appears to push and item onto the stack. When I "Pop", I get an error telling my my stack is empy. I am trying to figure out how to count how many items are in the stack to determine if anything have been actually pushed.
    Any help or information will be much appreciate it, cause I am really having a hard time with this. This is the function and procedure I have
    VB Code:
    1. Public Sub New()
    2.         Dim i As Integer
    3.  
    4.         Size = 0
    5.         For i = 0 To StackA.Length - 1
    6.             StackA(i) = Nothing
    7.         Next
    8.  
    9.   Public Function Push(ByRef item As Customer) As Boolean
    10.        If Size < 99 Then
    11.             StackA(Size) = item
    12.             Size = Size + 1
    13.             Return True
    14.         End If
    15.         Return False
    16.  
    17. Public Function Pop() As Customer
    18.                Dim i As Integer
    19.         Dim topitem As Customer
    20.  
    21.         If Size <= 1 Then
    22.             Try
    23.                 IsEmpty()
    24.             Catch ex As Exception
    25.                 MessageBox.Show("Stack is empty")
    26.             End Try
    27.  
    28.         Else
    29.             Size = Size - 1
    30.             topitem = StackA(Size)
    31.         End If
    32.         Return topitem
    I hope someone can help me determine how to set up a size function to determine how many items are in the stack.
    thanks in advance
    computer_mom44

  14. #14
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: [02/03] Stacks using Push and Pop

    You don't count the number of items. You explicitly keep a record with your Size variable. The variable will initially be zero. When you want to Push an item you place it at that index and increment the variable. When you want to Pop an item you decrement the variable and return the object at that index.

    In your code you're testing whether the Size is less than or equal to 1. Why would a size of 1 indicate that the stack is empty? Also, do NOT use a Try...Catch block to test any condition. Exceptions are for exceptional conditions. There's nothing excpetional about an empty stack. That is a perfectly normal condition.

  15. #15

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    12

    Re: [02/03] Stacks using Push and Pop

    Hi jmcilhinney, thanks for the reply.
    Could you give me an example of what you are talking about so I will know where to direct my thinking?
    Any information will be appreciated.
    computer_lady44

  16. #16
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: [02/03] Stacks using Push and Pop

    I can't really give an example without doing the whole thing, but given that you're almost ther anyway:
    VB Code:
    1. Public Class Stack
    2.  
    3.     'The maximum number of items unless otherwise stated.
    4.     Private Const DEFAULT_CAPACITY As Integer = 100
    5.  
    6.     'The items contained on the stack.
    7.     Private items As Object()
    8.  
    9.     'The current number of items.
    10.     Private _count As Integer = 0
    11.  
    12.     'The maximum number of items.
    13.     Public ReadOnly Property Capacity() As Integer
    14.         Get
    15.             Return Me.items.Length
    16.         End Get
    17.     End Property
    18.  
    19.     'The current number of items.
    20.     Public ReadOnly Property Count() As Integer
    21.         Get
    22.             Return Me._count
    23.         End Get
    24.     End Property
    25.  
    26.     'Creates a new Stack with the default capacity.
    27.     Public Sub New()
    28.         Me.New(DEFAULT_CAPACITY)
    29.     End Sub
    30.  
    31.     'Creates a new Stack with the specified capacity.
    32.     Public Sub New(ByVal capacity As Integer)
    33.         ReDim Me.items(capacity - 1)
    34.     End Sub
    35.  
    36.     'Adds an item to the stack.
    37.     Public Sub Push(ByVal item As Object)
    38.         If Me.Count < Me.Capacity Then
    39.             'Add the new item and increment the count.
    40.             Me.items(Me._count) = item
    41.             Me._count += 1
    42.         Else
    43.             'Can't push an item onto a full stack.
    44.             Throw New IndexOutOfRangeException("The stack is full.")
    45.         End If
    46.     End Sub
    47.  
    48.     'Removes the next item from the stack.
    49.     Public Function Pop() As Object
    50.         If Me.Count > 0 Then
    51.             Me._count -= 1
    52.  
    53.             Dim item As Object = Me.items(Me._count)
    54.  
    55.             'Remove the last item and return it.
    56.             Me.items(Me._count) = Nothing
    57.             Return item
    58.         Else
    59.             'Can't pop an item from an empty stack.
    60.             Throw New IndexOutOfRangeException("The stack is empty.")
    61.         End If
    62.     End Function
    63.  
    64. End Class

  17. #17

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    12

    Re: [02/03] Stacks using Push and Pop

    thanks,
    I really appreciate that. It helps me out alot. It's due tomorrow, so if I get everything working right, I let ya know.
    thanks again
    computer_lady44

  18. #18

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    12

    Re: [02/03] Stacks using Push and Pop

    Hi again,
    With all of that said, if I have a button on my form "Size" that when pushed will give a message " The stack contains......items". would I use a function to code my size button or would I just cold the button. This is what I have, but it's not incrementing.
    VB Code:
    1. Dim count, i As Integer
    2.         s.Size = 0
    3.         For i = 0 To s.Size
    4.             count = i + 1
    5.         Next
    6.         Me.lblOutput.Text = ("The stack contain " & count & " items")

    any information will be appreciated.
    Thanks in advance
    computer_lady44

  19. #19
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: [02/03] Stacks using Push and Pop

    That code isn't going to do much at all. You're setting s.Size to zero and then running a For loop from zero to s.Size, so it's always going to run from zero to zero and count will be 1. Take a look at the code I provided. The '_count' variable will always contain the number of items on the Stack and that is exposed publicly through the 'Count' property, so whenever you want to get the number of items in a Stack object you simply get the Count property.

  20. #20

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    12

    Re: [02/03] Stacks using Push and Pop

    thanks, and I am sorry I am not understanding exactly what I am suppose to do with this count property.
    If I were using this in a button click event, how would I go about this?
    Please your information is very valuable.
    computer_lady44

  21. #21
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: [02/03] Stacks using Push and Pop

    Try putting MessageBox.Show("The stack contains " & s.Count & " items") in the button click event. You don't need to use a loop to count the items in the stack. The stack keeps a count of the items as you push and pop items and exposes the count through the Count property.
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  22. #22
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: [02/03] Stacks using Push and Pop

    That class I posted is fully self-contained, i.e. you don't need to do anything extra to it to get it to work. As the comment says, the Count property is the number of items currently on the Stack. Count is a standard property for any collection. If you want to know how many items are on the Stack you simply get the Count property:
    VB Code:
    1. Dim myStack As New Stack
    2.  
    3. myStacK.Push("Hello")
    4. myStacK.Push("World")
    5. myStacK.Push("Goodbye")
    6.  
    7. MessageBox.Show("The stack contains " & myStack.Count & " items.)
    This will popup a message saying "The stack contains 3 items.".

  23. #23

    Thread Starter
    New Member
    Join Date
    Apr 2006
    Posts
    12

    Re: [02/03] Stacks using Push and Pop

    Yes, and thanks.
    I finally got it to working. I was initializing my count to zero and each time I added something to the stack, although it was being pushed, it was always zero. Also I saw where I was calling a new stack each time I pushed the "Push" button, so my program thought I was making a new stack. I had to make some minor adjustments, but it's working now.
    Thanks for all of your help.
    computer_lady44

  24. #24
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,449

    Re: [02/03] Stacks using Push and Pop

    Cool. Don't forget to resolve your thread from the Thread Tools menu if you have a satisfactory answer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width