Solved

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

Posted on 1998-04-20
7
257 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:vicky022098
  • 4
  • 3
7 Comments
 

Author Comment

by:vicky022098
ID: 1250088
Edited text of question
0
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1250089
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 */
    puts("List:\n-----");

    /* 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.

0
 

Author Comment

by:vicky022098
ID: 1250090
alexo,
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!
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:vicky022098
ID: 1250091
alexo,
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!
0
 
LVL 11

Expert Comment

by:alexo
ID: 1250092
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.

0
 

Author Comment

by:vicky022098
ID: 1250093
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!
0
 
LVL 11

Expert Comment

by:alexo
ID: 1250094
>> 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++  :-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now