Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

Conversion

Posted on 1998-10-04
Medium Priority
259 Views
I have a conversion problem for you:
#define MAX 100
void put(int v)
{
queue[tail++];
if (tail >max) tail=0;
}

int get (void)
{
return t;
}

void queueinit (void)
{
}

Please convert this to a dynamically allocated Queue, John
0
Question by:JTM100498
[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
• 2

LVL 5

Accepted Solution

scrapdog earned 600 total points
ID: 1253335
#include <alloc.h>

struct node
{
int value;
node *next;
};

node *tail;

void put(int v)
{
node *newnode;
tail->value = v;
newnode = (node *) malloc(sizeof(node));
tail->next = newnode;
tail = newnode;
tail->next = NULL;
}

int get (void)
{
return(t);
}

void queueinit (void)
{
}

------------------------------

I left out error checking for the sake of simplicity.

When the queue is initialized, the head pointer and the tail pointer point to the same node.  If the queue is used correctly (i.e. never take something out that is not there), it should not cause any errors.

0

LVL 5

Expert Comment

ID: 1253336
However, if you do want to catch when an error occurs, you could add a statement to the get function:

int get (void)
{

{
//act accordingly
}

return(t);
}

The "if (newnode==NULL)" statement checks if the node following the head pointer is defined.  If it is not, then the head pointer has passed the tail pointer, and you can put any error handling code in here if you want.

One thing I might not have made clear when I posted the answer is that in my example, there is no variable named "queue".  There is simply a head and tail pointer which are transparent to the user.  All you have to do after calling queueinit is call the get and put functions.

You can also add this function to return the number of nodes currently in the queue:

int getnumnodes(void)
{
int n=0;
node *current;
if (current != NULL)
{
n=1;
while (current->next != NULL) n++;
}
return(n);
}

scrapdog

0

Featured Post

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses
Course of the Month8 days, 22 hours left to enroll