static void Main(string[] args) { IQueue<string> queue = new LinkQueue<string>(); queue.EnQueue("a1"); queue.EnQueue("a2"); queue.EnQueue("a3"); while (queue.IsEmpty() == false) { Console.WriteLine(queue.QueueFront); queue.DeQueue(); } }
/// <summary> /// 文本消息包接收后处理 /// </summary> /// <param name="e"></param> void RecieveForTextMessage(PackageReceiveArgs e) { var msg = e.Message as TextMessage; if (null == msg) { return; } msg.MessageID = IDProvider.NewId(); msg.SendTime = DateTime.Now.ToDateTimeString(); //加入写入历史消息队列 QueueWriteToHistory.EnQueue(msg); IChannel channel = GetUserChannel(msg.ReceiverID); //如果消息接收者已连接 if (null != channel) { //发送消息给接收者 Server.Send(msg, channel); //放入发送队列 //QueueWaitSend.EnQueue(msg); } else { //加入未发送消息队列 QueueUnSendToDB.EnQueue(msg); //记录未发送消息标识 AddUnrecived(msg.ReceiverID, 1); } }
public void Test() { LinkQueue <int> stack = new LinkQueue <int>(); stack.EnQueue(1); stack.EnQueue(2); stack.EnQueue(3); Assert.Equal(1, stack.DeQueue()); Assert.Equal(2, stack.DeQueue()); Assert.Equal(3, stack.DeQueue()); stack.EnQueue(1); stack.EnQueue(2); Assert.Equal(1, stack.DeQueue()); Assert.Equal(2, stack.DeQueue()); stack.EnQueue(3); Assert.Equal(3, stack.DeQueue()); Assert.Equal(0, stack.DeQueue()); }
/// <summary> /// �õ�AOV���������������� /// </summary> /// <returns>AOV����������������</returns> public string TopoSort() { string Result = string.Empty; int[] ID = GetInDegressList(); LinkQueue<int> LQ = new LinkQueue<int>(); for (int i = 0; i < vertexCount; i++) { if (ID[i] == 0) LQ.EnQueue(i); } if (LQ.Length == vertexCount) throw new Exception("������ͼ�������."); while (LQ.IsEmpty() == false) { int j = LQ.QueueFront; LQ.DeQueue(); Result += vertexList[j].VertexName + "\n"; EdgeNode p = vertexList[j].FirstNode; while (p != null) { ID[p.Index]--; if (ID[p.Index] == 0) { LQ.EnQueue(p.Index); } p = p.Next; } } int k; for (k = 0; k < vertexCount; k++) if (ID[k] != 0) break; return (k == vertexCount) ? Result : "��AOV���л�."; }
/// <summary> /// �õ���������������� /// </summary> /// <param name="startNodeName">���й��������������ʼ������</param> /// <returns>���������������</returns> public string BFSTraversal(string startNodeName) { string BFSResult = string.Empty; int i = GetIndex(startNodeName); if (i != -1) { for (int j = 0; j < vertexCount; j++) vertexList[j].Visited = false; vertexList[i].Visited = true; BFSResult += vertexList[i].VertexName + "\n"; LinkQueue<int> Q = new LinkQueue<int>(); Q.EnQueue(i); while (Q.IsEmpty() == false) { int j = Q.QueueFront; Q.DeQueue(); EdgeNode p = vertexList[j].FirstNode; while (p != null) { if (vertexList[p.Index].Visited == false) { vertexList[p.Index].Visited = true; BFSResult += vertexList[p.Index].VertexName + "\n"; Q.EnQueue(p.Index); } p = p.Next; } } } return BFSResult; }
static void Main(string[] args) { Console.WriteLine(Test(3)); #region 线性表测试 ////A<string> a = new A<string>(); //SingleLinkList<string> list1 = new SingleLinkList<string>(); //Console.WriteLine("是否是空表:{0}", list1.IsEmpty); //list1.Append("早上"); //ListPrint(list1); //Console.WriteLine(list1.GetLength); //list1.Insert("你好", 0); //ListPrint(list1); //list1.Append("中上"); //ListPrint(list1); //list1.Insert("陌生人", 0); //ListPrint(list1); //Console.WriteLine(list1.GetLength); //list1.Insert("傻逼", list1.GetLength-1); //ListPrint(list1); //Console.WriteLine("倒置表:" + list1.ReverseList()); //ListPrint(list1); //Console.WriteLine("表长:{0}", list1.GetLength); //Console.WriteLine("是否是空表:{0}", list1.IsEmpty); //Console.WriteLine("删除的是:{0}", list1.Delete(4)); //Console.WriteLine("表长:{0}", list1.GetLength); //ListPrint(list1); //Console.WriteLine("获取的是:{0}", list1.GetElem(0)); //Console.WriteLine("获取的是:{0}", list1.Locate("傻逼")); //Console.WriteLine("表长:{0}", list1.GetLength); //list1.Delete(list1.GetLength - 2); //list1.SetElem("睡觉了", 0); //ListPrint(list1); //Console.WriteLine("倒置表:" + list1.ReverseList()); //ListPrint(list1); //Console.WriteLine("清空"); //list1.Clear(); //Console.WriteLine("倒置表:" + list1.ReverseList()); //ListPrint(list1); //list1.Append("中上"); //Console.WriteLine("倒置表:" + list1.ReverseList()); //ListPrint(list1); //ListPrint(list1); //Console.ReadKey(); #endregion #region Stack测试 //SeqStack<string> list2 = new SeqStack<string>(4); //Console.WriteLine($"判空{list2.IsEmpty},元素:{list2.Count}"); //DataStructPrint(list2); //list2.Clear(); //DataStructPrint(list2); //list2.Push("一"); //list2.Push("二"); //list2.Push("三"); //list2.Push("四"); //list2.Push("五"); //DataStructPrint(list2); //Console.WriteLine($"判空{list2.IsEmpty},元素:{list2.Count}"); //Console.WriteLine($"获取:{list2.GetTop()}"); //Console.WriteLine($"出栈:{list2.Pop()}"); //Console.WriteLine($"获取:{list2.GetTop()}"); //Console.WriteLine($"出栈:{list2.Pop()}"); //Console.WriteLine($"出栈:{list2.Pop()}"); //Console.WriteLine($"判空{list2.IsEmpty},元素:{list2.Count}"); //Console.WriteLine($"出栈:{list2.Pop()}"); //Console.WriteLine($"出栈:{list2.Pop()}"); //Console.WriteLine($"出栈:{list2.Pop()}"); //Console.WriteLine($"判空{list2.IsEmpty},元素:{list2.Count}"); //list2.Push("六"); //DataStructPrint(list2); #endregion #region 队列 LinkQueue <string> list = new LinkQueue <string>(); Console.WriteLine($"元素个数:{list.Count};是否空:{list.IsEmpty}"); DataStructPrint(list); list.EnQueue("一"); list.EnQueue("二"); list.EnQueue("三"); Console.WriteLine($"元素个数:{list.Count};是否空:{list.IsEmpty}"); list.EnQueue("四"); list.EnQueue("五"); list.EnQueue("六"); DataStructPrint(list); Console.WriteLine($"元素个数:{list.Count};是否空:{list.IsEmpty}"); Console.WriteLine($"取出:{list.DeQueue()}"); Console.WriteLine($"取出:{list.DeQueue()}"); DataStructPrint(list); Console.WriteLine($"队头:{list.GetHead()}"); list.Clear(); //Console.WriteLine($"取出:{list.DeQueue()}"); //Console.WriteLine($"取出:{list.DeQueue()}"); //Console.WriteLine($"取出:{list.DeQueue()}"); //Console.WriteLine($"取出:{list.DeQueue()}"); DataStructPrint(list); #endregion Console.ReadKey(); }
/// <summary> /// 登录消息包接收后处理 /// </summary> /// <param name="e"></param> void ReceiveForLogin(PackageReceiveArgs e) { var login = e.Message as Login; if (null == login) { return; } login.LoginTime = DateTime.Now.ToDateTimeString(); //设置通道名称为UserID e.Channel.Name = login.UserID.ToString(); //写入登录队列处理 QueueLogin.EnQueue(login); #region 发送登录结果消息 //登录信息 var loginInfo = ServicesProvider.Items.UserService.GetUserLoginInfo(login.UserID); //如果登录信息不存在,则表明当前用户为首次登录 if (null == loginInfo) { loginInfo = new Td.Kylin.IM.Data.Model.UserLoginInfo { UserID = login.UserID, LastLoginAddress = login.AreaName, LastLoginTime = login.LoginTime.ToDateTime(), PrevLoginAddress = login.AreaName, PrevLoginTime = login.LoginTime.ToDateTime() }; } //如果获取的登录信息记录的最后登录时间与本次登录时间不一致,说明未更新到当前登录,则采用最后前置 else if ((loginInfo.LastLoginTime - login.LoginTime.ToDateTime()).TotalSeconds < 1) { loginInfo.PrevLoginAddress = loginInfo.LastLoginAddress; loginInfo.PrevLoginTime = loginInfo.LastLoginTime; loginInfo.LastLoginAddress = login.AreaName; loginInfo.LastLoginTime = login.LoginTime.ToDateTime(); } //登录结果回传 LoginPostBack backData = new LoginPostBack { Error = null, IsSuccess = true, LoginTime = login.LoginTime, PrevLoginAddress = loginInfo.PrevLoginAddress, PrevLoginTime = loginInfo.PrevLoginTime, UserID = login.UserID, UserName = login.UserName }; Server.Send(backData, e.Channel); #endregion #region 发送离线消息 int unreceivedCount = GetUnrecivedCount(login.UserID); //如果存在未接收的消息 if (unreceivedCount > 0) { //未接收成功的消息包集合 var unreceivedList = ServicesProvider.Items.UnsendMessageService.GetList(login.UserID); if (null != unreceivedList) { //放入待发送消息队列中 foreach (var m in unreceivedList) { var waitMsg = new TextMessage { Content = m.Content, MessageID = m.MessageID, MessageType = m.MessageType, ReceiverID = m.ReceiverID, ReceiverName = string.Empty, SenderID = m.SenderID, SendTime = m.SendTime.ToDateTimeString(), SernderName = m.SenderName }; QueueWaitSend.EnQueue(waitMsg); } //更新未接收数量标识信息 AddUnrecived(login.UserID, unreceivedList.Count()); //将本次回传的消息从未发送记录数据库中移除 long[] delIDs = unreceivedList.Select(p => p.MessageID).ToArray(); ServicesProvider.Items.UnsendMessageService.DeleteMessage(delIDs); } } #endregion }