0% found this document useful (0 votes)
1 views

Queue using Struct

algorithm

Uploaded by

Min Shosho
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Queue using Struct

algorithm

Uploaded by

Min Shosho
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Queue using Struct

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Structwithstackandqueue
{
class Program
{
struct StudentInfo
{
public string StdName;
public int StdID;
public Boolean StdGender;
}

static void Main(string[] args)


{
Queue<StudentInfo> StdQueue = new Queue<StudentInfo>();
StudentInfo T;
string C;
Boolean P;
do
{
Console.Clear();
Console.WriteLine("Welcome to this program");
Console.WriteLine("press A to add new data");
Console.WriteLine("press P to print all data");
Console.WriteLine("Press 1 to print the top element");
Console.WriteLine("Press C to know how many element in the queue");
Console.WriteLine("Press S to search for an elemet by the ID");
Console.WriteLine("Press R to remove the top element");
Console.WriteLine("Press 0 to remove all elements");
Console.WriteLine("Press x to quit");
C=Console.ReadLine();
switch (C.ToLower())
{
case "a": //adding element
Console.Write("enter Student name:");
T.StdName = Console.ReadLine();
Console.Write("Enter the ID:");
T.StdID = int.Parse(Console.ReadLine());
Console.Write("Enter F for females or M for males: ");
T.StdGender = (Console.ReadLine().ToUpper() == "F") ? false : true;
StdQueue.Enqueue(T);
Console.WriteLine("\n\n\t\tDone");
break;
case "p": //printing all element
if (StdQueue.Count == 0)
Console.WriteLine("No element is found");
else
foreach (StudentInfo S in StdQueue)
Console.WriteLine(S.StdName + " " + S.StdID + " " +
((S.StdGender == true) ? "male" : "female"));
break;
case "1": //printing the top element only
if (StdQueue.Count == 0)
Console.WriteLine("No element is found");
else
{
T = StdQueue.Peek();
Console.WriteLine(T.StdName + " " + T.StdID + " " + ((T.StdGender ==
true) ? "male" : "female"));
}
break;
case "c":
Console.WriteLine("You have {0} element", StdQueue.Count());
break;
case "s":
Console.WriteLine("Please enter the ID you would like to search for:");
int N = int.Parse(Console.ReadLine());
P = false;
foreach (StudentInfo S in StdQueue)
{
if (N == S.StdID)
{
P = true;
Console.WriteLine(S.StdName + " " + S.StdID + " " +
((S.StdGender == true) ? "male" : "female"));
}
}
if (P == false)
Console.WriteLine("No element is found with ID of "+N);
break;
case "r":
if (StdQueue.Count == 0)
Console.WriteLine("No element is found");
else
{
T = StdQueue.Dequeue();
Console.WriteLine(T.StdName + "has been removed!");
}

break;
case "0":
if (StdQueue.Count == 0)
Console.WriteLine("No element is found");
else
{
StdQueue.Clear();
Console.WriteLine("All elements have been removed!");
}
break;
case "x":
Console.Beep(2000, 1000);
Console.WriteLine("See you later!");
break;
}
Console.ReadKey();
} while (C.ToLower() != "x");
Console.ReadKey();
}
}
}

You might also like