高手来,如何用C#实现一个添加,移出操作的时间复杂度为O(1)的双向链表,什么ArrayList,Queue,LinkedList都可以不用说了,说了也不给分.

zhiang75 2007-11-19 11:20:55

public class MyQueue
{
public class Node
{
public Node(object data)
{
Data = data;
}

public Node NextNode = null;
public Node UpNode = null;
public object Data = null;
}
Node beginNode = null;
Node endNode = null;

public override string ToString()
{
return count.ToString();
}

int count = 0;

public int Count
{
get
{
return count;
}
set
{
count = value;
}
}

public Node Dequeue()
{
if (beginNode.NextNode == null && count>1)
{
return null;
}


Node Rerurn = beginNode;
if (Rerurn != null)
{
beginNode = beginNode.NextNode;
--count;
Rerurn.NextNode = null;
Rerurn.UpNode = null;

}



return Rerurn;
}

public void Enqueue(Node node)
{
if (beginNode == null)
{
beginNode = node;
endNode = node;
}
else
{
node.UpNode = endNode;
endNode.NextNode = node;
endNode = node;
endNode.NextNode = null;
}
++count;
}
}




这是我的代码,这段代码我是应用在基数排序中的...作为基数排序的桶应该是插入,以及移出的操作都是O(1)的操作,
经过我的测试ArrayList,Queue,LinkedList的插入以及移出的的操作近似为O(nLogn),这不是我需要的..包括上面的代码.我不清楚这是为什么,因此向大家提问.
To:jinjazz
.net奇怪吗?如果你学过基础的数据结构应该知道,链表的时间负责度不可能是o(1)的,hashtable才有可能
以上这是你的回复
我原贴写的是
"如何用C#实现一个添加,移出操作的时间复杂度为O(1)的双向链表"
添加移出操作的时间复杂度为O(1)的双向链表这个在数据结构与算法里早有定论,见<<数据结构与算法-面向对象的C++设计模式>> ISBN 7-5053-8339-6 P122
原贴见:
http://topic.csdn.net/u/20071115/15/de9587d8-aad9-4dd8-9a5d-73769068c167.html

To:viena

你把http://topic.csdn.net/u/20071115/15/de9587d8-aad9-4dd8-9a5d-73769068c167.html移到非技术区我非常不满意,我已经向CSDN投诉了见
http://topic.csdn.net/u/20071119/10/c7787dd1-e5d7-4a59-8852-e37745cb0dec.html

我可以很明确的说这个帖子不是作业贴,是我在讨论技术的帖子,如果你要是还挪,我会继续发,并且继续投诉...









...全文
533 49 打赏 收藏 转发到动态 举报
写回复
用AI写文章
49 条回复
切换为时间正序
请发表友善的回复…
发表回复
li45214521 2007-11-20
  • 打赏
  • 举报
回复
不要用我的测试 我的测试不对
li45214521 2007-11-20
  • 打赏
  • 举报
回复
你用这个整数队列看看
using System;
using System.Collections.Generic;
using System.Text;

namespace test
{
struct node
{
public int[] Data;
public int front;
public int rear;
}
public class MyQueue
{
private node temp;
private int count;
public MyQueue()
{
temp.Data = new int[160000];
temp.front = 159999;
temp.rear = 159999;
count = 0;
}
public void Enqueue(int a)
{
if (temp.front == (temp.rear+1)%160000)
{
Console.WriteLine("queue is full");
}
else
{
temp.rear = (temp.rear + 1) % 160000;
temp.Data[temp.rear] = a;
count++;
}
}
public int Dequeue()
{
if (temp.rear == temp.front)
{
Console.WriteLine("queue is empty");
return 0;
}
else
{
temp.front = (temp.front + 1) % 160000;
count--;
return temp.Data[temp.front];
}
}
public int Count
{
get
{
return count;
}
}
}
class Program
{
static void Main(string[] args)
{
test();
}
private static void test()
{
for (int a = 1000; a < 160000; a += 1000)
{

MyQueue myQueue = new MyQueue();
//以上是初始化代码

long bl2 = 0;

for (int d = 0; d < 10; d++)
{
long bl1 = DateTime.Now.Ticks;
for (int c = 0; c < 10000; c++)
{
myQueue.Enqueue(c);
}
for (int c = 0; c < 10000; c++)
{
myQueue.Dequeue();
}
bl1 = DateTime.Now.Ticks - bl1;
bl2 += bl1;
}

//以上是运行测试代码
bl2 = (bl2 / 10) / a;
Console.WriteLine(a+", "+bl2.ToString());
}
Console.Read();

}
}
}
zhiang75 2007-11-20
  • 打赏
  • 举报
回复
to:jinjazz
希望你把这150个数据跑完在看看,我的也是双核,不过上面的测试数据不是在线程优先级为高的情况下跑得,是普通.
谢谢你的回答,我想这个帖子还是先结了吧,我在问问别人.
to:li45214521
谢谢你的回答,不过你说的问题总不是重点.
xiaoku 2007-11-20
  • 打赏
  • 举报
回复
我来学习的...
li45214521 2007-11-20
  • 打赏
  • 举报
回复
LZ是不时Debug模式下阿
li45214521 2007-11-20
  • 打赏
  • 举报
回复
还有 你写的为什么会比类库好?你想过没有类的实例化顺序.

li45214521 2007-11-20
  • 打赏
  • 举报
回复
还有你编的程序为什么用 object 为什么不用int 知不知道 拆箱和装箱 是很费时间的
还有 内存管理 是在你这个线程使用多了 会启用 是不定期的 谁知道启用多少次
li45214521 2007-11-20
  • 打赏
  • 举报
回复
你开了一个线程 这个线程开始只有1M的内存阿 不够的话 需要扩展阿 开线程 很费时间的
图表曲线可以用数组存点 然后用连结相邻两点 所以时间复杂度 O(n)
jinjazz 2007-11-20
  • 打赏
  • 举报
回复
你的代码我测试了test1,我是双核酷睿,明显比你的结果更集中 ,还是和你的测试环境有关系的
60000 425
61000 453
62000 444
63000 444
64000 446
65000 437
66000 412
67000 415
68000 420
69000 430
70000 417
71000 432
72000 438
73000 449
74000 439
75000 433
76000 436
77000 434
78000 427
79000 431
80000 418
81000 432
82000 440
83000 425
84000 432
85000 430
86000 435
87000 447
88000 444
89000 451
90000 442
91000 435


这样的,考虑到cpu切片和内存读写也有不稳定因素,应该可以认为是平稳的了,毕竟我们不是搞航天飞机的。你应该尽可能的把耗费资源的应用关掉。
zjmotion 2007-11-20
  • 打赏
  • 举报
回复
牛帖要mark一下
honey52570 2007-11-20
  • 打赏
  • 举报
回复
O_O
zhiang75 2007-11-20
  • 打赏
  • 举报
回复
顶一下.
ETstudio 2007-11-19
  • 打赏
  • 举报
回复
回完再看
shrinerain 2007-11-19
  • 打赏
  • 举报
回复
请教可以告诉我双向链表应该如何写啊,谢谢了..
----------------------------------------

它不具备一般双向链表的任意位置插入删除操作.
你的这种特殊链表称之为队列, 我想你不会有什么疑问.

正如树是一种图, 但是我们都说"查找树",没人说"查找图".

特殊化就有特殊名称. 钻牛角尖没有什么意思.

shrinerain 2007-11-19
  • 打赏
  • 举报
回复
另外,,,时间复杂度是理论上的.

并不可能完全的O(1).



你的代码, 将class声明成sealed class, 取消虚函数表.

将Data声明为范型.



zhiang75 2007-11-19
  • 打赏
  • 举报
回复
To:shrinerain
请教可以告诉我双向链表应该如何写啊,谢谢了..
shrinerain 2007-11-19
  • 打赏
  • 举报
回复
...但是你的代码是队列,不是一般双向链表...

zhiang75 2007-11-19
  • 打赏
  • 举报
回复
谢谢了...现在是.NET什么都达不到阿...我上面的代码别说O(1)了...恐怕连O(n)都算不上...怀疑是垃圾回收器的问题,想禁用不知道如何禁用,谢谢了..
Red_angelX 2007-11-19
  • 打赏
  • 举报
回复
日他妈的CSDN 回复打倒一半给我来个自动刷新刷半天刷不出来慢死

添加移出操作的时间复杂度为O(1)的双向链表这个在数据结构与算法里早有定论,见 < <数据结构与算法-面向对象的C++设计模式> > ISBN 7-5053-8339-6 P122

移出是可以做到o(1)的
添加只是理论上可以 实际达不到
因为碰撞折射消耗太大
zhiang75 2007-11-19
  • 打赏
  • 举报
回复
to:flarejune
先别在一边笑了..看看图表的曲线是什么?,4年前就上CSDN了这样的曲线没见过吧?
顺注:这个你看来不是算法的,或者说很糟糕的东西,不过看曲线比System.Collections的还是强一点阿...
加载更多回复(28)

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧