[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

To alexo; ref.- que ans. on priority stack as dbl. linked list

I was just hunting around the answered questions in the C topic area and came across this one which you answered. The que. was accepted on 4-19-98. Out of curiosity and to learn from it, as I am learning this topic, I compiled and ran the program. It compiles with no errors, but when I run it, nothing happens. Can you tell me why? Can you show me how to run it with results? i.e., do you have to put some printf's in certain locations to see the results? Thank you; this would be a great help.
  • 4
  • 3
1 Solution
vicky022098Author Commented:
Edited text of question
Hi vicky.

>> when I run it, nothing happens.
But of course!  There is no user interface, the code just demonstrates a concept.

>> Can you show me how to run it with results?
Not really.  It is not supposed to do anything.  The code implements a priority stack.  The main() function does nothing useful, it just demonstrates how to use the stack.  The stack itself is like a filing cabinet.  I mean, you wouldn't complain to the owner of a store "I bought this filing cabinet and it doesn.t do anything", would you?

However, I can explain what happens internally:

First, the main program creates a priority stack.  The stack is initially empty, so I'll draw it as:

Stack: <END>

Then, it executes a series of commands to push and pop items on the stack.

>>    Push(&theList, 20, 20);

We push the number 20 with a priority of 20.  Since the stack was previously empty, the priority does not matter.  The stack now looks like:

Stack: 20, <END>

>>    Push(&theList, 10, 10);

Now we push the number 10 with a priority of 10. Ordinarily, pushed items are added to the top of the stack but since the new item has a lower priority than the first, it is inserted after it.  The stack now looks like:

Stack: 20, 10, <END>

>>    Push(&theList, 30, 30);

Now we push the number 30 with a priority of 30. The pushed item is added to the top of the stack since there are no items with a higher priority.  The stack now looks like:

Stack: 30, 20, 10, <END>

>>    item = Pop(&theList);

Finally, we pop an item from the top of the stack.  The popped item is removed from the stack.  The stack now looks like:

Stack: 20, 10, <END>

The variable "item" holds the number 30 that was removed from the stack.

Note that I used items whose priorities were equal to their values.  This was only done to simplify the example.

You can also add a function that will display all elements in the list:

#include <stdio.h>

void Display(Element *list)
    Element *pCurrent;

    /* Header */

    /* Display all items */
    for (pCurrent = list->pNext; pCurrent; pCurrent = pCurrent->pNext)
        printf("Item: %d (priority: %d)\n", pCurrent->item, pCurrent->priority);

    /* Close the list */
    puts("End of list\n");

Oh, one more thing.  The priority stack was implemented using a doubly linked list because that was what missq asked for.  Actually, as you can see in the code, there is no use for the pPrevious member pointer (it is just maintained in a correct state for each item).  A singly linked list would have sufficed.

vicky022098Author Commented:
Thank you for writing the function to display all elements in the list. That's what I want to do, so I can see what's going on. I know that I would add this function to the original file you wrote, probably after the last function defined before main. However, could you add the function call to the display function in main for me? I'm not sure how to call it in main. Then, I will be able to see a run and be better able to understand it. Thank you!
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

vicky022098Author Commented:
I hope I did this correctly, i.e, I wanted to grade you answer, but ask you to expand on your answer by calling the display function from main. Then I would grade your final answer. Please - this would give me exactly what I need to see the run. Thank you!
Vicky, what is the amount of your C knowledge?  The thing you ask is trivial.

Call as: Display(&theList);

You cannot re-grade an answer.  Once you've accepted it - that's it.
However, I'm not doing it for the points so there's no problem with a continued discussion.

vicky022098Author Commented:
Thank you so much. Now I can see what's going on. Actually, I did it correctly, except in defining the function, I neglected to put a space between void and Display.  I am just learning C. I am having a hard time; the instructor is teaching the class for intermediates when the class is supposed to be for beginners. I am persevering. My instructor briefly discussed pointers, and structs, and lists - all within 2 hours. They will be on the final, which is in 2 weeks. There will be no more discussion on them in class before the final as we are still going on.  Your example will help me study. One day, I hope that I will be able to say 'this is trivial' about anything in C. You have been more help than any tutors or other students I have talked with!
>> You have been more help than any tutors or other students I have talked with!
Hey, always glad to help.

>> One day, I hope that I will be able to say 'this is trivial' about anything in C.
C is a difficult language to master but wait until you try C++  :-)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now