+ Reply to Thread
Page 1 of 2
1 2 LastLast
Results 1 to 10 of 13

Thread: Div Deque?

  1. #1
    Code Warrior BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch's Avatar
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Age
    19
    Posts
    2,223
    Blog Entries
    8

    Div Deque?

    So I was trying to make something for a project and though I might need a deque for the project I was working on. Not knowing a ton about OOP inside of JS or how various things pass (reference or copy) etc I thought this would be a realistic approach for what I needed. When I got done I realized this would be a great tutorial since it was so easy to make.

    What is a deque? A deque is a double ended que. Most of you are familiar with it, but for those who are not, a stack is like a stack of plates. You put one plate on the top, and when you want the plate again you pick it up. First in last out. A deque is double ended so its kinda like those plates are hovering in the air, and you can put plates on the bottom and on the top and take one off from either end as well. If that was a horrible explanation click here.

    The idea behind this is having a div, and inside of that div I will use JS to move around there.

    So lets start off making a div, set the id to something we can remember "deque" seems appropriate to me. Add a input box and submit box to help us add things as well. I put an id on the text box as well since we will need the value.
    HTML Code:
    <input type="text" value="" id="insert">
    <input type="submit" value="Push!">
    <!-- Nodes here! -->
    <div id="deque"></div>
    To make this work should not be too hard, basically get the deque by the id, create a new div and add the content inside of it (innerHTML) then use javascripts "appendChild" to insert it into the bottom of the list.

    Code:
    <script>
        function 
    Push() {
            
    //declare variables
            
    var mydeque document.getElementById('deque'); //gets the deque
            
    var nodevalue document.getElementById('insert'); //gets the input box
            
    var mynode document.createElement('div'); //creates a div
            
            //insert
            
    mynode.innerHTML nodevalue.value//sets the value
                
    nodevalue.value ""//resets input box
            
    mydeque.appendChild(mynode);
        }
    </script>
    <input type="text" value="" id="insert">
    <input type="submit" onclick="Push();" value="Push!">
    <!-- Nodes here! -->
    <div id="deque"></div> 
    Now we need to be able to add from the top, so we going to basically do the same approach, add a dropdown so we know which side to add too, instead of appending were going to use "insertBefore" in javascript to add them.

    Code:
    <script>
        function 
    Push() {
            
    //declare variables
            
    var order document.getElementById('side'); //gets the oder 0 = first element, 1 = second
            
    var mydeque document.getElementById('deque'); //gets the deque
            
    var nodevalue document.getElementById('insert'); //gets the input box
            
    var mynode document.createElement('div'); //creates a div
            
            //insert
            
    mynode.innerHTML nodevalue.value//sets the value
                
    nodevalue.value ""//resets input box
            
    if(order.options.selectedIndex==0) {
                
    mydeque.insertBefore(mynodemydeque.firstChild);
            } else {
                
    mydeque.appendChild(mynode);
            }
        }
    </script>
    <input type="text" value="" id="insert">
        <select id="side">
            <option>Top</option>
            <option>Bottom</option>
        </select>
    <input type="submit" onclick="Push();" value="Push!">
    <!-- Nodes here! -->
    <div id="deque"></div> 
    Now to get values! This is really easy to do, were going to alert the value, and then remove the child using "removeChild" and with that use "firstChild" or "lastChild" so really simple.
    Code:
    <script>
        function 
    pop(id) {
            if(
    id==0) {
                
    //top
                
    var deque document.getElementById('deque');
                
    alert(deque.firstChild.innerHTML);
                
    deque.removeChild(deque.firstChild);
            } else {
                
    //bottom
                
    var deque document.getElementById('deque');
                
    alert(deque.lastChild.innerHTML);
                
    deque.removeChild(deque.lastChild);
            }
        }
    </script> 
    And the finished script, I added a little color to make it easier on the eyes!




    Code:
    <html>
    <
    head>
    <
    title>Div Deque!</title>
    <
    style>
        
    div.glow0 {
            
    border-left:4px solid #952fa4;
            
    padding0px 0px 0px 4px;
        }
        
    div.glow1 {
            
    border-left:4px solid #40af0e;
            
    padding0px 0px 0px 4px;
        }
        
    div.glow2 {
            
    border-left:4px solid #3d769e;
            
    padding0px 0px 0px 4px;
        }
    </
    style>
    <
    script>
        var 
    0;
        function 
    Push() {
            
    //declare variables
            
    var order document.getElementById('side');
            var 
    mydeque document.getElementById('deque');
            var 
    nodevalue document.getElementById('insert');
            var 
    mynode document.createElement('div');
            
            
    //insert
            
    mynode.setAttribute('class''glow'+ (i%3));
            
    mynode.innerHTML nodevalue.value;
                
    nodevalue.value "";
            if(
    order.options.selectedIndex==0) {
                
    mydeque.insertBefore(mynodemydeque.firstChild);
            } else {
                
    mydeque.appendChild(mynode);
            }
            
    i++;
        }
        function 
    pop(id) {
            if(
    id==0) {
                
    //top
                
    var deque document.getElementById('deque');
                
    alert(deque.firstChild.innerHTML);
                
    deque.removeChild(deque.firstChild);
            } else {
                
    //bottom
                
    var deque document.getElementById('deque');
                
    alert(deque.lastChild.innerHTML);
                
    deque.removeChild(deque.lastChild);
            }
        }
    </script>
    </head>
    <body>
    <a href="javascript:pop(0)">Pop from Top!</a> | <a href="javascript:pop(1)">Pop from Bottom!</a><br />
    <input type="text" value="" id="insert">
        <select id="side">
            <option>Top</option>
            <option>Bottom</option>
        </select>
    <input type="submit" onclick="Push();" value="Push!">
    <div id="deque"></div>
    </body>
    </html> 
    Last edited by BlaineSch; 11-11-2009 at 10:05 AM.

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Div Deque?

    Neat! Where can I see a working version?
    +rep

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,662
    Blog Entries
    57

    Re: Div Deque?

    Very neat. +rep
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #4
    Code Warrior BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch's Avatar
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Age
    19
    Posts
    2,223
    Blog Entries
    8

    Re: Div Deque?

    Yay it didn't get lost in the forums of time I was getting scared I would have to remake this lol.

    Working sample here.

  5. #5
    Guru debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy is just really nice debtboy's Avatar
    Join Date
    Aug 2009
    Location
    I'm in the... Black Lodge
    Posts
    908

    Re: Div Deque?

    Very Nice!! +rep
    The owls are not what they seem...

  6. #6
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Div Deque?

    Very cool demo! The first time I press "Pop from bottom" I get undefined. After that, it works fine.

  7. #7
    Code Warrior
    /////////|||||\\\\\\\\\
    amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama is a splendid one to behold amrosama's Avatar
    Join Date
    Aug 2007
    Location
    Pyramids st, Giza, Egypt
    Age
    21
    Posts
    8,182
    Blog Entries
    12

    Re: Div Deque?

    love it
    +rep

  8. #8
    Code Warrior BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch is a glorious beacon of light BlaineSch's Avatar
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Age
    19
    Posts
    2,223
    Blog Entries
    8

    Re: Div Deque?

    Quote Originally Posted by Jordan View Post
    Very cool demo! The first time I press "Pop from bottom" I get undefined. After that, it works fine.
    Weird, for some reason the spacing inside the original div was used as a child? When I take it out it works fine.

  9. #9
    Code Warrior Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest is a name known to all Guest's Avatar
    Join Date
    Sep 2009
    Location
    United States
    Age
    16
    Posts
    2,607
    Blog Entries
    2

    Re: Div Deque?

    Code:
     / ___|_   _  ___  ___| |_ 
    | |  _| | | |/ _ \/ __| __|
    | |_| | |_| |  __/\__ \ |_ 
     \____|\__,_|\___||___/\__|

  10. #10
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    Re: Div Deque?

    Cool man
    JS's yet another useful thing about it
    +rep.

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts