Jump to content

Div Deque?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
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.
<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.

<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.

<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(mynode, mydeque.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.
<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!


Posted Image

<html>
<head>
<title>Div Deque!</title>
<style>
	div.glow0 {
		border-left:4px solid #952fa4;
		padding: 0px 0px 0px 4px;
	}
	div.glow1 {
		border-left:4px solid #40af0e;
		padding: 0px 0px 0px 4px;
	}
	div.glow2 {
		border-left:4px solid #3d769e;
		padding: 0px 0px 0px 4px;
	}
</style>
<script>
	var i = 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(mynode, mydeque.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>

Edited by BlaineSch, 11 November 2009 - 08:05 AM.


#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Neat! Where can I see a working version?
+rep

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Very neat. +rep
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Yay it didn't get lost in the forums of time :D I was getting scared I would have to remake this lol.

Working sample here.

#5
debtboy

debtboy

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 916 posts
Very Nice!! +rep :)

#6
Guest_Jordan_*

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

#7
amrosama

amrosama

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 8,674 posts
love it
+rep
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#8
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts

Jordan said:

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
Guest

Guest

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,414 posts
Posted Image
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#10
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Cool man :)
JS's yet another useful thing about it :P
+rep.
Posted Image

#11
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Looks awesome but I haven't had time to read it yet. But I'm sure you're worth some +rep (seems good the thing I've read).

#12
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
I just made things up closer to the end so I'm glad you didn't read it all :P