Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Implementing a stack

 
Ranch Hand
Posts: 179
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I wish this comment finds you well, I think that I get your point that you want to make Stack API in order to perform multiple operations on the Stack, then, the Stack API below may fulfill your needs. moreover, either Stack and Queue, named Bag, each one of them based on LIFO and FIFO, transitively

 
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Houssam, I'm going to move this to the Beginner forum, and just a heads up, there we try not to give complete answers but to help the original poster to come up with the answer on their own through hints, explanations, and code snippets.

Also, in the code you posted there are a couple of bugs.
 
Houssam El
Ranch Hand
Posts: 179
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, I'm so sorry, I forget to implement a method that iterates through the stackArray[] field to exhibit elements that have been entered by the client.

 
Carey Brown
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you familiar with SSCCE (Simple Self Contained Compilable Example)? It would help if your code had a class name and a main() method that would test your class so that anyone could download, compile, and run it. You especially need some test code, that will point out some of your weak spots.
 
Carey Brown
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Houssam El wrote:

If maxSize is  65,000 are you sure you want to print that many?

Why not implement this as a toString() method?
 
Houssam El
Ranch Hand
Posts: 179
2
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:@Houssam, I'm going to move this to the Beginner forum, and just a heads up, there we try not to give complete answers but to help the original poster to come up with the answer on their own through hints, explanations, and code snippets.

Also, in the code you posted there are a couple of bugs.



Thanks for your remark, Brown, I did not test the code on my machine, although, I wrote it directly on the forum, moreover, change top = -1 to top = 0, therefore, the program will work properly.
 
Carey Brown
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Houssam, please do not edit a post once people have responded to it, instead create a new post with the changes. I've reverted your edit.
 
Houssam El
Ranch Hand
Posts: 179
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If maxSize is  65,000 are you sure you want to print that many?

Why not implement this as a toString() method?



1- 65,000 elements is a huge size, and it won't print, for sure, the array must be used for a small number of elements, or the length of the array is predictable in advance, moreover, for huge records, I will use Binary Search instead.

2 - toString() method used to display the current state of instance variables, unlike, iteration through the elements that have been pushed into the stack
 
Carey Brown
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Houssam El wrote:


If maxSize is  65,000 are you sure you want to print that many?

Why not implement this as a toString() method?



1- 65,000 elements is a huge size, and it won't print, for sure, the array must be used for a small number of elements, or the length of the array is predictable in advance, moreover, for huge records, I will use Binary Search instead.

2 - toString() method used to display the current state of instance variables, unlike, iteration through the elements that have been pushed into the stack

A binary search implies that the array is in sorted order which is not the case. And why would you print 65,000 if only one number has been pushed on the stack?

"toString()" is implemented as to whatever makes sense for the particular class to present to the user. You can see similar implementations in Arrays and Lists.
 
Houssam El
Ranch Hand
Posts: 179
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A binary search implies that the array is in sorted order which is not the case. And why would you print 65,000 if only one number has been pushed on the stack?

"toString()" is implemented as to whatever makes sense for the particular class to present to the user. You can see similar implementations in Arrays and Lists.



there were many ways to let the size increments by 1 in each push operation that has been made by the client, that way, you'll maintain the array's length




 
Carey Brown
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed, but if you've only pushed 1 why would you print more?
 
Houssam El
Ranch Hand
Posts: 179
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Agreed, but if you've only pushed 1 why would you print more?



do you mean by "more" 0's that exhibit on the console? shall you clarify your question, Please?
 
Carey Brown
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You won't necessarily get zeros (0) because as the stack grows and shrinks the old values are left behind.
 
Carey Brown
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A test might look something like this:
Output:

 
Houssam El
Ranch Hand
Posts: 179
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:You won't necessarily get zeros (0) because as the stack grows and shrinks the old values are left behind.



I dunno if I get your point or not, however, as all programmers know that array has a fixed length unlike List and its family, as a result when you initialize an array, its length would be the same on the heap, I mean it would be represented it with the length that you've been entered the first time, unless if you updated the array's length
 
Carey Brown
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With your code, let's say we have a size of 5. To start with, if we displayStack() we get
0 0 0 0 0
Now push '1' and display
1 0 0 0 0
Now push 2 through 5 and display
1 2 3 4 5
Now pop and pop and display
1 2 3 4 5
See, '4' and '5' will not be zero?
 
Saloon Keeper
Posts: 1658
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Again, Houssam:

Even before that, the current output would look the same whether we had not used the stack at all yet, or if we had pushed five 0 elements on to it and it was full.

We are using an array internally, yes, but we are trying to provide a class that will have normal stack-like behavior, except probably for a fixed maximum size.

Up to that limit, pushing one on, popping one off, displaying the contents, etc. should behave similarly as for a general stack class, which means showing everything that has been pushed on to it but nothing more.
 
Houssam El
Ranch Hand
Posts: 179
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:With your code, let's say we have a size of 5. To start with, if we displayStack() we get
0 0 0 0 0
Now push '1' and display
1 0 0 0 0
Now push 2 through 5 and display
1 2 3 4 5
Now pop and pop and display
1 2 3 4 5
See, '4' and '5' will not be zero?



Thanks for clarification, Brown, do a little alteration in the code, therefore, 0's will not exhibit, remove the maxSize field from the class, the theStack class would be looked like this,



a test would be displayed as follow,



output : 1 2 3 4



 
Houssam El
Ranch Hand
Posts: 179
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:Hi Again, Houssam:

Even before that, the current output would look the same whether we had not used the stack at all yet, or if we had pushed five 0 elements on to it and it was full.

We are using an array internally, yes, but we are trying to provide a class that will have normal stack-like behavior, except probably for a fixed maximum size.

Up to that limit, pushing one on, popping one off, displaying the contents, etc. should behave similarly as for a general stack class, which means showing everything that has been pushed on to it but nothing more.



Hi Jesse,
I think that your question is answered in the last comment of mine,
 
Jesse Silverman
Saloon Keeper
Posts: 1658
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Houssam, do you see two problems with that tho?

1. What if we attempt to push on to the stack when it is full?  There is no way to even check that in the new code, the user would have to remember the size in their own code.
2. What if we attempt to pop from the stack when it is empty?  That can still be prevented by calling isEmpty() first, but it is easier to remember 0 than an arbitrary size.

By adjusting things, we can get an API that will be self-aware of full as well as empty conditions, and to display only what is currently on the stack.


 
Carey Brown
Saloon Keeper
Posts: 8770
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Houssam El
Ranch Hand
Posts: 179
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:pop() is wrong.



the program works properly, and here is a test that I made on my own machine.



where is the bug in the pop() ?
 
Jesse Silverman
Saloon Keeper
Posts: 1658
61
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't seem that the test you ran could be running against this code you showed earlier:



What do those two methods look like right now for you?
 
Houssam El
Ranch Hand
Posts: 179
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:Houssam, do you see two problems with that tho?

1. What if we attempt to push on to the stack when it is full?  There is no way to even check that in the new code, the user would have to remember the size in their own code.
2. What if we attempt to pop from the stack when it is empty?  That can still be prevented by calling isEmpty() first, but it is easier to remember 0 than an arbitrary size.

By adjusting things, we can get an API that will be self-aware of full as well as empty conditions, and to display only what is currently on the stack.




I've answered those questions above, I will repeat

1,2 you can exploit Exceptions to get rid of issues that you've revealed in your comment.

The Stack API above is created to fulfill the needs of the user that has been struggled on how to create a Stack API using array,
 
Master Rancher
Posts: 4509
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened to the original OP?

It looks like Houssam El has stolen this thread for his own code.  

Should Houssam El's code be moved to its own thread so that the original OP can work on his problem?
 
Houssam El
Ranch Hand
Posts: 179
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:What happened to the original OP?

It looks like Houssam El has stolen this thread for his own code.  

Should Houssam El's code be moved to its own thread so that the original OP can work on his problem?



I did not steal nothing, they’ve bombarded me with questions 😅
 
Marshal
Posts: 74388
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Houssam El wrote:. . . the program works properly . . .

No, it doesn't. It has a potential memory leak. You won't notice it at present because you have created a stack for primitives, rather than what you should have created, viz. a Stack<E>.
 
Campbell Ritchie
Marshal
Posts: 74388
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with NR's comments and have created a new thread for you.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic