enqueue and dequeue pointer

This is a discussion on enqueue and dequeue pointer within the C++ Programming forums, part of the General Programming Boards category; I'm trying to create a menu style enqueue and dequeue with pointers. I have this so far I want to ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    46

    Question enqueue and dequeue pointer

    I'm trying to create a menu style enqueue and dequeue with pointers. I have this so far I want to use the left and right pointers to lets say dequeue a number. I also thought about using two temps. Any suggestions?

    Code:
    #include<iostream>
    using namespace std;
    
    struct qNode{
      int num;
      qNode *next;
      qNode *left;
      qNode *right;
    };
    
    
    int main(){
      qNode *temp = NULL;
      qNode *front = NULL;
      qNode *back = NULL;
    
      temp = new qNode;
      
      temp->num = 7;
      
      cout<<temp->num<<endl;
      cout<<endl;
      //  cout<<temp<<endl;
      
      front = temp;
      back = front;
      
      // cout<<"front: "<<front<<endl;
      // cout<<"back: "<<back<<endl;
    
      temp = new qNode;
      temp->num = 9;
      
      back->next = temp;
      back = temp;
      
      cout<<front->next->num<<endl;
    
      temp = front;
      while(temp){
        cout<<temp->num<<endl;
        temp = temp->next;
      }
    
      front = front->next;
      delete temp;
    
      return 0;
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't see why you would need left and right, to be honest; if it's an honest-to-goodness queue, you're only go to go through the thing one way.

    I'm guessing this is your enqueue:
    Code:
      back->next = temp;
      back = temp;
    After this, you need to set back->next to NULL, otherwise your "walk through the queue" loop becomes infinite (unless you happen to find a NULL pointer on your random walk through core). (That is why you asked the question, isn't it?)

    Also, I'm guessing this is the dequeue:
    Code:
      front = front->next;
      delete temp;
    You would need to set temp = front first before this. (As it stands in your program, temp would have to be NULL to get here after the while loop; I can never remember whether delete NULL is syntactically valid or not, but regardless, I would be willing to bet it isn't what you want.)

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    46

    Question

    Are there any simple Linked lists "examples" out there that you can enqueue, dequeue and print. I'm getting confused with my pointers and I wanted to see how you can actually implement this Linked list with a Menu. I think it would help me understand it better. I've searched the internet but I can't find anything that's easy for me to understand. I'd like to implement my linked list like I sorta have it in my first post.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    46

    Question

    ok so i figured some more stuff out. I can't seem to get my dequeue working though. I need to also figure out how to show the queue.

    Code:
    #include<iostream>
    using namespace std;
    
    class qNode{
    public:
      int num;
      qNode *next;
    };
    
    int main(){
      int num;
      int selection;
      void menu();
      int enqueue(int);
      void dequeue();
      qNode *temp = NULL;
      qNode *front = NULL;
      qNode *back = NULL;
      
      do{
      cout<<"1. Enqueue"<<endl;
      cout<<"2. Dequeue"<<endl;
      cout<<"3. Print"<<endl;
      cout<<"4. Exit"<<endl;
      cout<<"Enter a selection: ";
      cin>>selection;
      
      if(selection == 1){
        cout<<"Enter a number: ";
        cin>>num;
        temp = new qNode;
        temp->num=num;
        if(front == NULL){
          front = temp;
          back = front;
          cout<<num<<endl;;
        }
        else{
          back->next = temp;
          back = temp;
        }
        
      }
      
      if(selection == 2){
        temp = front;
        front = front->next;
        delete temp;
        
      }
    
    }while(selection != 4);
        return 0;
    
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Maybe a hint: add
    Code:
      printf("%p %p %p\n", front->next, back, back->next);
    at the bottom of your do-while loop and watch it go through some enqueue-dequeue cycles. What happens when you do three enqueues and three dequeues?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And implement choice 3, to print the list. That way, you can again, observe what's going on with the list. I don't see anything obviously wrong, but then I only spent a few seconds on it.

    I may come back to it.

    Edit: What do you set "next" to?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Quote Originally Posted by tabstop View Post
    Maybe a hint: add
    Code:
      printf("&#37;p %p %p\n", front->next, back, back->next);
    at the bottom of your do-while loop and watch it go through some enqueue-dequeue cycles. What happens when you do three enqueues and three dequeues?
    In the C++ forum, it behooves you to suggest cout instead of printf.

    Code:
      if(selection == 1){
        cout<<"Enter a number: ";
        cin>>num;
        temp = new qNode;
        temp->num=num;
        if(front == NULL){
          front = temp;
          back = front;
          cout<<num<<endl;;
    What about setting temp->next to NULL? And why the double semicolon? [edit] matsp's edit may have beaten me, we cannot know! [/edit]

    Printing the queue should be easy. Something as simple as
    Code:
    for(cur = front; cur; cur = cur->next) {
        cout << cur->data << endl;
    }
    should cut it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by dwks View Post
    In the C++ forum, it behooves you to suggest cout instead of printf.
    I'm over here sitting in the corner now Today I have learned: just because the title bar says "C Board" doesn't mean I'm in the C board.

    Quote Originally Posted by dwks View Post
    What about setting temp->next to NULL? And why the double semicolon? [edit] matsp's edit may have beaten me, we cannot know! [/edit]
    If we say it three times, it must be true.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    46
    haha... Thanks for the help guys. Yeah I left an extra semicolon there....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing lists to functions
    By Griever in forum C Programming
    Replies: 23
    Last Post: 11-14-2008, 05:26 PM
  2. enqueue and deuque funct.
    By m0ntana in forum C Programming
    Replies: 5
    Last Post: 04-22-2007, 02:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21