Here is the Code:
class Program
{
static void Main(string[] args)
{
Queue q = new Queue();
Console.WriteLine(q.isEmpty());
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(4);
q.enqueue(5);
q.enqueue(6);
q.enqueue(7);
Console.WriteLine(q.isEmpty());
Console.WriteLine(q.peek());
q.display();
q.dequeue();
q.display();
}
}
public class node
{
public int data;
public node next;
public node(int _data)
{
data = _data;
}
}
public class Queue
{
public node first;
public node last;
public void enqueue(int data)
{
node n = new node(data);
if (first == null)
{
last = n;
first = n;
}
else
{
last.next = n;
last = n;
}
}
public int dequeue()
{
int data=0;
if (first != null)
{
data = first.data;
first = first.next;
if (first == null)
{
last = null;
}
}
return data;
}
public int peek()
{
return first.data;
}
public bool isEmpty()
{
if(first==null)
{
return true;
}
else
{
return false;
}
}
public void display()
{
var temp = first;
Console.WriteLine("________________");
while (temp != null)
{
Console.WriteLine(temp.data);
temp = temp.next;
}
Console.WriteLine("________________");
}
}
Popular Posts
-
Here is the code: // => Brute Force Solution: O(m* n) static long arrayManipulation(int n, int[][] queries) { ...
-
Whatever it is one fine day everyone on this planet who are born have to die for sure. When you are close ones are with you, you wont know ...
-
HTML: <div class="outer-container"> <div class="inner-container"> <div class="t...
No comments:
Post a Comment