0% found this document useful (0 votes)
19 views16 pages

Understanding Stacks and Queues in .NET

Uploaded by

nevak97071
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views16 pages

Understanding Stacks and Queues in .NET

Uploaded by

nevak97071
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Stacks and Queues

Stack

• Stack is a data structure implemented in the .NET


Framework in two ways, simple stack
in System.Collections namespace, and stack as generic
data structure in System.Collections.Generic namespace,
the principle of stack structure operation is LIFO (last in first
out), the last element entered first out.
• three methods from this class, namely:
• Push, to insert an element at the top of the stack
• Pop, to remove an element from the top of the stack and return it
• Peek, to return an element from the top of the stack without
removing it
using System;
using System.Collections;
public class SamplesStack {

public static void Main() {

// Creates and initializes a new Stack.


Stack myStack = new Stack();
myStack.Push("Hello");
myStack.Push("World");
myStack.Push("!");
// Displays the properties and values of the Stack.
Console.WriteLine( "myStack" );
Console.WriteLine( "\tCount: {0}", myStack.Count );
Console.Write( "\tValues:" );
PrintValues( myStack );
}
public static void PrintValues( IEnumerable myCollection ) {
foreach ( Object obj in myCollection )
Console.Write( " {0}", obj );
Console.WriteLine();
}
}
/*
This code produces the following output.
myStack
Count: 3
Values: ! World Hello
Example of using generic
stack from the
namespace System.Collections.G
eneric:
using System;
using System.Collections.Generic;

class Example
{
public static void Main()
{
Stack<string> numbers = new Stack<string>();
numbers.Push("one");
numbers.Push("two");
numbers.Push("three");
numbers.Push("four");
numbers.Push("five");

// A stack can be enumerated without disturbing its contents.


foreach( string number in numbers )
{
Console.WriteLine(number);
}
https://dev.to/adavidoaiei/fundamental-data-structures-and-algorithms-in-c-4ocf#stiva
Example – reversing words
You receive the reversed word, as shown in the following diagram, which presents the
process of reversing the MARCIN word:
The implementation code, which should be added
to the Main method within the Program
class, is shown in the following code snippet:

Stack<char> chars = new Stack<char>();


foreach (char c in "LET'S REVERSE!")
{
chars.Push(c);
}
while (chars.Count > 0)
{
Console.Write(chars.Pop());
}
Console.WriteLine();
Stack applications:
• undo / redo functionality
• word reversal
• stack back/forward on browsers
• backtracking algorithms
• bracket verification
Queue
• Queue is a data structure implemented in the .NET
Framework in two ways, the simple queue
in System.Collections namespace, and the queue as
the generic data structure
in System.Collections.Generic namespace, the
working principle of queue structures is FIFO (first in
first out), the first element entered first out.
• FIFO principle, which stands for First-In First-Out.
The Queue class contains a set of
methods
• Enqueue, to add an element at the end of the queue
• Dequeue, to remove an element from the beginning and return it
• Peek, to return an element from the beginning without removing it
• Clear, to remove all elements from the queue
• Contains, to check whether the queue contains the given element

• Enqueue, to add an element at the end of the queue


• TryDequeue, to try to remove an element from the beginning and return it
• TryPeek, to try to return an element from the beginning without removing it
Example of using the simple queue from the
namespace System.Collections:
using System;
using System.Collections;
public class SamplesQueue {
public static void Main() {
// Creates and initializes a new Queue.
Queue myQ = new Queue();
myQ.Enqueue("Hello");
myQ.Enqueue("World");
myQ.Enqueue("!");

// Displays the properties and values of the Queue.


Console.WriteLine( "myQ" );
Console.WriteLine( "\tCount: {0}", myQ.Count );
Console.Write( "\tValues:" );
PrintValues( myQ );
}
public static void PrintValues( IEnumerable myCollection ) {
foreach ( Object obj in myCollection )
Console.Write( " {0}", obj );
Console.WriteLine();
}
}
/*
This code produces the following output.
myQ
Count: 3
Values: Hello World !
Example of using the generic
queue from the
namespace System.Collections.G
eneric:
using System;
using System.Collections.Generic;

class Example
{
public static void Main()
{
Queue<string> numbers = new Queue<string>();
numbers.Enqueue("one");
numbers.Enqueue("two");
numbers.Enqueue("three");
numbers.Enqueue("four");
numbers.Enqueue("five");

// A queue can be enumerated without disturbing its contents

https://dev.to/adavidoaiei/fundamental-data-structures-and-algorithms-in-c-4ocf#stiva
Real life example of queue:
The system from the point of sale of a restaurant.
Example – call center with a single
consultant
• Let's take a look at the code of the first class, named IncomingCall, which represents
a single incoming call performed by a caller. Its code is as follows:
• public class IncomingCall
• {
• public int Id { get; set; }
• public int ClientId { get; set; }
• public DateTime CallTime { get; set; }
• public DateTime StartTime { get; set; }
• public DateTime EndTime { get; set; }
• public string Consultant { get; set; }
• }
public class CallCenter public void Call(int clientId)
{ {
private int _counter = 0; IncomingCall call = new IncomingCall()
public Queue<IncomingCall> Calls { get; {
private set; } Id = ++_counter,
public CallCenter() ClientId = clientId,
{ CallTime = DateTime.Now
Calls = new Queue<IncomingCall>(); };
} Calls.Enqueue(call);
} }

public IncomingCall Answer(string


consultant)
{
if (Calls.Count > 0)
{
IncomingCall call = Calls.Dequeue();
call.Consultant = consultant;
call.StartTime = DateTime.Now;
return call;
}
return null;
Priority queues
A priority queue makes it possible to extend the concept of a queue by setting priority
for each element in the queue.
priority can be specified simply as an integer value

You might also like