• C

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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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!
Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

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++  :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.