Lost Password?


  #1 (permalink)  
Old 05-24-2008, 12:02 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,224
Last Blog:
Ext JS or Ext GWT
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default VS2008 C# Sequential Lists: The Queue And Stack Class

SEQUENTIAL LISTS: THE QUEUE AND STACK CLASS



1. INTRODUCTION
Sequential lists are collection of objects that can be entered or extracted in a sequential way. They don’t provide access to objects in the middle on the list. You can only access a specific object each time you extract it. The two classes named “Queue” and ”Stack”, both belonging to the “System.Collections” namespace provide you with this kind of functionality, each of them in a slightly different way. The Queue class offers you access to the first object on the list, whereas the Stack class offers you access to the last object on the list. By first and last, we mean the first and last object that entered the list. So, the Queue class is a First-in, First-out collection(FIFO Collection) whereas the Stack class is a Last-in, First-out(LIFO Collection). In this tutorial you will create and use both classes to see how they behave when entering and extracting data.


2. GRAPHICAL USER INTERFACE
The graphical user interface for our application is simple enough for beginners. Create a simple form with four textboxes and four buttons as they are depicted here:



The two top textboxes are named textbox1 and textbox2 from left to right. Likewise, the two bottom textboxes are named textbox3 and textbox4 from left to right. Add the buttons accordingly and the two labels to distinguish the two operations.


3. CODE DEVELOPMENT
The first thing you must do is to create two public objects that represent the two classes. These two objects will be later used to enter and extract text objects from them. We could also add any other object but for this tutorial some plain text will suffice.

csharp Code:
  1. public Queue q = new Queue();
  2. public Stack s = new Stack();

By pressing the button1 the text on the textBox1 will be entered in the Queue object. By pressing the button3 any text on the textBox2 will be added to the Stack object. Buttons 2 and 4 are used for the extraction of one text object at a time. It is imperative to check if there are any objects on the lists before trying to extract one of them or we risk triggering an exception. You could write the following code to add objects in the lists:

csharp Code:
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.             q.Enqueue(textBox1.Text);
  4.             textBox1.Text = "";
  5. }
  6.  
  7. private void button3_Click(object sender, EventArgs e)
  8. {
  9.             s.Push(textBox3.Text);
  10.             textBox3.Text = "";
  11. }

We make use of the “Enqueue” and the “Push” methods for the two objects. Both methods add an item to the lists, but they name the process differently. To extract objects one at a time we use the “Dequeue” and “Pop” methods respectively.

csharp Code:
  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3.             if (q.Count > 0)
  4.                 textBox2.Text = q.Dequeue().ToString();
  5. }
  6.  
  7. private void button4_Click(object sender, EventArgs e)
  8. {
  9.             if (s.Count > 0)
  10.                 textBox4.Text = s.Pop().ToString();           
  11. }

Remember that the “If” statement when proceeded by only one line of code doesn’t need to have braces ({}). So, with the method named “Count” we count the number of objects in the lists and if this number is greater than zero then we extract an object from the list. When extracting an object it is being removed from the list and the next one takes it place. It is not like an array were we can access array members again and again.


4. APPLICATION
Build the code and run it in debug mode. Add three or more objects to both lists by pressing the “Enqueue” and “Push” buttons. Then, retrieve the items one at a time from the sequential lists. See the difference?

Besides the three methods for each class that we saw here, there is also a method called “Peek”. The “Peek” method in both lists has the advantage that it can retrieve the next object in line without actually removing it. With this method you can test whether an object is an integer or a float for example and act accordingly. Try to replace the “Dequeue” and “Pop” with the “Peek” method and see what happens.


Questions/Comments?
Have questions or comments? Post them here.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-24-2008, 02:11 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,589
Last Blog:
Web slideshow in JavaS...
Rep Power: 76
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: VS2008 C# Sequential Lists: The Queue And Stack Class

How do you highlight the syntax of the code? Is there a tag I can use, as I can't see one in the toolbar on the Reply to Thread box.
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-25-2008, 09:03 AM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,224
Last Blog:
Ext JS or Ext GWT
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: VS2008 C# Sequential Lists: The Queue And Stack Class

It is the Geshi plugin, it use to be in the toolbar. You can use it by typing:

Code:
[ highlight="language" ]
Your code
[ /highlight ]
In this case:
Code:
[ highlight="csharp" ]
code...
[ /highlight ]
Remove the spaces before and after highlight and /highlight.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-25-2008, 01:49 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,589
Last Blog:
Web slideshow in JavaS...
Rep Power: 76
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: VS2008 C# Sequential Lists: The Queue And Stack Class

Ta. How come it's not in the toolbar anymore?
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-25-2008, 09:53 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,224
Last Blog:
Ext JS or Ext GWT
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Re: VS2008 C# Sequential Lists: The Queue And Stack Class

The same changes that worked with vBulletin 3.6x wouldn't work with 3.7. They also obfuscated their JavaScript (somewhat) and I haven't taken the time to figure it out yet....
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 05-26-2008, 12:43 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: On God's Planet
Posts: 9,589
Last Blog:
Web slideshow in JavaS...
Rep Power: 76
Xav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud ofXav has much to be proud of
Send a message via MSN to Xav
Default Re: VS2008 C# Sequential Lists: The Queue And Stack Class

Oh - it's a shame - they're very useful. Could you replace the word "highlight" with "hl" or something, to make it easier to type? Or maybe an extra parameter for the "code" tag to accept a language to highlight syntax in.
__________________


Mr. Xav | Website | Forums | Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
c sharp, classes, queue, sequential lists, stack, visual studio 2008



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -5. The time now is 12:00 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 98%

Ads