Solved

Conversion

Posted on 1998-10-04
2
251 Views
Last Modified: 2010-05-18
I have a conversion problem for you:
#define MAX 100
int queue[max+1];head,tail
void put(int v)
{
   queue[tail++];
   if (tail >max) tail=0;
}

int get (void)
{
  int t= queue[head++];
  if (head>max) head=0;
  return t;
}

void queueinit (void)
{
  head=0; tail=0;
}

Please convert this to a dynamically allocated Queue, John
0
Comment
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
  • Learn & ask questions
  • 2
2 Comments
 
LVL 5

Accepted Solution

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

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

node *tail;
node *head;

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)
{
  node *newhead;
  int t= head->value;
  newhead = head->next;
  free(head);
  head = newhead;
  return(t);
}

void queueinit (void)
{
  head=(node *) malloc(sizeof(node));
  head->next = NULL;
  tail = head;
}

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

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

by:scrapdog
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)
{
  node *newhead;
  int t= head->value;
  newhead = head->next;
  free(head);
  head = newhead;

  if (newhead==NULL)
       {
             //head has surpassed the tail
             //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;
  current=head;
  if (current != NULL)
       {
             n=1;
             while (current->next != NULL) n++;
       }
  return(n);
}




scrapdog

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
Best UNIX-compatible free C compiler for Windows or Mac 6 289
Problem to save 10 180
Socket Programming (Unix) 8 160
keep track of class structure 1 53
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

739 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