Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 1998-04-20
7
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 

Author Comment

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

Accepted Solution

by:
alexo earned 150 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

618 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