-
Stack Program
I have recently been assigned an assignment where I cannot use the stack library and I have to write all the functions for stack (ie. pop, push, empty, etc).
I have to use a class and an array to keep track of the stack.
My problem is how can I delete elements in the array? Can you?
The function "pop" would require me to delete the element at the top of the array, not set it to 0 but delete it. How can this be done?
Any help would be appreciated (info on the stack library would be cool too).
Thanks
Mingk
-
Hi,
Then you should implement a linked list.
Initially you can allocate a list of fixed size using calloc and then you can access the elements as getting from an array.
If this can help you , then i can give you some more info on this
Thanks
Raghu
-
Keep a variable called count to keep track of number of entries in the stack.
-
You should be able to find LOTS of info on the web on this topic.
With a "fixed" array, the bottom of the stack is at element 0 and the top of the stack moves "upward". You don't want to get into the re-sizing of the array so just decrement the pointer for top of the stack; if you are going overboard about it all, over-write the old entry with 0 or null (depending on what you are storing in your stack) as a way to test if the entry is "empty".
A very nice implementation would be using a "circular array" - use a pointer to the top of the stack, and the bottom element, and the size. Some tricky math to determine whether you are full, but it keeps you within the bounds of the array. To "pop" you just decrement the top of the stack pointer ...
Similar Threads
-
By divagoddess in forum C++
Replies: 5
Last Post: 08-14-2009, 03:12 PM
-
Replies: 1
Last Post: 02-18-2007, 07:43 PM
-
Replies: 3
Last Post: 02-22-2006, 04:18 AM
-
Replies: 4
Last Post: 08-14-2005, 01:03 PM
-
By Gordon Reichhardt in forum VB Classic
Replies: 2
Last Post: 01-08-2002, 10:06 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks