Solved

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

Posted on 1998-04-20
7
262 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 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
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!

 

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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Test against App 49 139
Problem to show menu 5 107
How to translate this 2-line while loop into C from Perl? 8 165
nested if statement in excel help 4 49
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

734 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