5 Replies - 824 Views - Last Post: 24 March 2016 - 10:40 AM Rate Topic: -----

#1 Blooch   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 35
  • Joined: 17-October 15

Pointer based Queue help

Posted 23 March 2016 - 08:49 PM

I'm trying to implement a queue with pointers and I've gotten the code to compile but when I run the code it only seems to print the first element entered, please help! I have a feeling the problem either lies in my push function or my pull function but i can't seem to pinpoint it.
#include <stdio.h>
#include <stdlib.h>

struct Node
{
  char data;
  struct Node * next;
};

struct Node *anchor = NULL;
struct Node *tail = NULL;
int Push(char usrinp);
char Pull();
char Front();
int isFull();

int Push(char usrinp)
{
 if(isFull() == 1)
  {
    return -1;
  }
    else if(isFull() == 0) //create first element
    {
      anchor = (struct Node *)malloc(sizeof(struct Node));
      anchor->next = NULL;
      anchor->data = usrinp;
      tail = (struct Node *)malloc(sizeof(struct Node));
      tail->next = NULL;
      return 0;
    }
      else
      {
        struct Node *node = (struct Node *)malloc(sizeof(struct Node));
        node->data = usrinp;
          if(anchor == NULL)
          {
            node->next=anchor;
            node->data=usrinp;
            tail=node;
            anchor->next=tail->next=NULL;
          }
          else
          {
            tail->next=node;
            tail=node;
            tail->next=NULL;
          }
        return 0;
      }
}

char Pull()
{
  char front;
  if(isFull() == 0)
  {
    return '\0';
  }
  else
  {
    struct Node *node = (struct Node *)malloc(sizeof(struct Node));
        node = anchor;
        node = node->next;
        front = Front();
        free(anchor);
        anchor = node;
        return front;
  }
}

char Front()
{
 char front;

 if(isFull() == 0)
 {
  return '\0';
 }
  else
  {
    front = anchor->data;
    return front;
  }
}

int isFull()
{
  if (anchor == NULL) {
      return 0;
  }
  struct Node *node = (struct Node *)malloc(sizeof(struct Node));
  if (node == NULL) {
      return 1;
  }
  else
  {
  return -1;
  }
}

int main()
{
    char usrinp;
    char stackoutput;
    scanf("%c", &usrinp);
    while (usrinp!= '^')
    {
        if (usrinp != '\n')
        {
            if (Push(usrinp) == -1)
            {
                printf("Stack full, enter '^' to exit: ");
                scanf("%c", &usrinp);
            }
            else
            {
                scanf("%c", &usrinp);
            }
        }
        else
        {
            scanf("%c", &usrinp);
        }
    }
    while (isFull() != 0)
    {
        stackoutput = Pull();
        printf("%c\n", stackoutput);
    }
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Pointer based Queue help

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Pointer based Queue help

Posted 24 March 2016 - 05:13 AM

scanf does not read white space, which includes line feeds.

You should take a modular approach. Start by checking to see that push works, and when it does, verify that pull works. When that does, begin adding user interactions.

Do not try to do it all at once: write small sections of code, verifying the code works, before moving on.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Pointer based Queue help

Posted 24 March 2016 - 07:52 AM

Quote

scanf does not read white space, which includes line feeds.

What? The scanf() function can read whitespace, including line feeds, if you use the proper type of variable with the proper format specifier.



Jim
Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Cannot compute!
  • member icon

Reputation: 1868
  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Pointer based Queue help

Posted 24 March 2016 - 08:51 AM

Hi, the isFull function is doing two things - checking whether the queue is empty and checking if a node can be allocated. Additionally the allocated memory is not freed.

So isFull could be divided into two separate functions.

You could use an isEmpty function returning a bool (most compilers allow bool now).

// check if queue is empty or not
bool isEmpty();



Instead of allocating and freeing a Node in an isFull function, a function could be made to allocate a Node and return a pointer to it.

// allocate memory for Node and return pointer to it
// returns NULL if Node not allocated
struct Node * createNode();



Push could return a bool.

// push the data onto the end of the queue
bool Push();



In Push a temporary pointer can be used for a newly created Node, which can be checked for validity.

Currently in Push, when the queue is empty you create two Nodes.

Writing the odd comment might help with the logic.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Pointer based Queue help

Posted 24 March 2016 - 10:11 AM

View Postjimblumberg, on 24 March 2016 - 09:52 AM, said:

Quote

scanf does not read white space, which includes line feeds.

What? The scanf() function can read whitespace, including line feeds, if you use the proper type of variable with the proper format specifier.

Yeah, sorry. I should have said as used.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Pointer based Queue help

Posted 24 March 2016 - 10:40 AM

Actually the program seems to be using the "%c" format specifier so it should retrieve any character including whitespace characters, the "%c" format specifier doesn't skip leading whitespace characters like most of the other specifiers.

However normally when you're retrieving single characters you press the enter key. That enter key places the end of line character ('\n') in the input buffer which must be handled on the next input. I haven't looked really close at the program logic to see what is actually happening to this character but it is possible that it is being handled correctly. This is something the OP should verify using his debugger. T

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1