作者luj810714 (清风晓明月)
看板C_Sharp
标题[问题] 并未将物件参考设定为物件的执行个体
时间Sun Mar 31 02:08:30 2013
老师给了一段单向伫列的code,要改成双向伫列(其实也就是加上一个previous)
因为之前用C有写过类似的作业,用一样的逻辑写也没有bug
但是执行後却一直显示"并未将物件参考设定为物件的执行个体"
我只知道哪一段程式码出了问题,但不知道发生了甚麽事
下面附上我的code,希望版上大大不吝指教
谢谢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StackNodeTest
{
class Program
{
static void Main(string[] args)
{
String taken;
Stack s = new Stack(); ;
Console.WriteLine("Stack START:(Enter Number, '#' to End)\n-----");
do
{
taken = Console.ReadLine();
if (taken != "#")
{
s.push(int.Parse(taken));
}
} while (taken != "#");
do
{
int e;
s.pop(out e);
Console.WriteLine(e);
} while (s.checktop());
Console.Read();
}
}
class Stack
{
private Node Top;
private Node botton;
public Stack()
{
this.Top = null;
}
public bool push(int x)
{
Node p=new Node(x,Top);
this.Top.Setprevious(p);开发环境显示这一行有问题
if (this.Top == null)
{
this.Top =this.botton = p;
}
else
{
this.Top = p;
}
return true;
}
public bool pop(out int x)
{
x = Top.getdata();
this.Top = Top.getnext();
return true;
}
public bool popfrombotton(out int x)
{
x = botton.getdata();
this.botton = botton.getprevious();
return true;
}
public bool checktop()
{
return this.Top != null;
}
}
class Node
{
private int data;
private Node next;
public Node previous;
public Node(int x, Node next)
{
Setdata(x);
Setnext(next);
this.previous = null;
}
public void Setdata(int x)
{
this.data = x;
}
public void Setnext(Node next)
{
this.next = next;
}
public void Setprevious(Node previous)
{
this.previous = previous;
}
public int getdata()
{
return this.data;
}
public Node getnext()
{
return this.next;
}
public Node getprevious()
{
return this.previous;
}
}
}
--
1F:推 olduck:红豆生南国12/25 16:07
2F:推 newmatt: 春来发起痴12/25 16:56
3F:推 asdfzx: 愿插郭采洁12/25 17:34
4F:推 otakuwill: 耻物最香湿12/25 17:39
5F:推 a01000a:王摩诘-香湿12/25 18:10
6F:推 jpsstargazer:前几楼淫出好诗呀12/25 20:07
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 49.158.2.110
7F:→ luj810714:最後我用try-catch把那一行隔离後不管exception message 03/31 03:20
8F:→ luj810714:程式一样可以执行,但我还是希望可以了解问题发生原因@@ 03/31 03:20
9F:→ ssccg:因为this.Top一开始是null啊 03/31 04:22
10F:→ ssccg:就在那下面不就正有检查this.Top是不是null的code? 03/31 04:22
11F:→ ssccg:那一样使用this.Top的那行不也应该先确定是不是null? 03/31 04:26
12F:推 Eleganse:这样写当然可以 加个try-catch没啥问题 能跑就是正确程序 03/31 08:05
13F:→ Eleganse:但是类别stack的建构函数stack()中 程式塞了个null给它 03/31 08:06
14F:→ Eleganse:如果成员是null 程式还能正常跑不就活见鬼了 03/31 08:07
15F:→ Eleganse:当然当你作了第1次PUSH後 里面有值了 物件就开始正确运行 03/31 08:08
16F:→ Eleganse:因此try-catch程序ok 只在物件初始化时 进了1次catch而已 03/31 08:09
17F:→ Eleganse:不喜欢的话 那就把stack类别砍掉重练 03/31 08:11
18F:→ Eleganse:依你自已的逻辑重写 记得建构函数时要塞值进去 不要null 03/31 08:12
19F:→ Eleganse:然後我刚才突然想到 System.Collections里不是就有个 03/31 08:21
20F:→ Eleganse:stack类别...那干麻自已写啊 03/31 08:21
21F:→ luj810714:感谢!原来如此!!,但依我设计的逻辑是,当他POP时发现 03/31 21:18
22F:→ luj810714:Top是null时就跳出do-while回圈,那麽是否有其他替代方 03/31 21:19
23F:→ luj810714:案呢? 03/31 21:19
24F:→ ssccg:跟pop有什麽关系? 问题那行在push里面啊 04/01 00:41
25F:→ ssccg:把那行移到else里面,this.Top = p; 上面就好了 04/01 00:41
26F:→ ssccg:这明明是流程设计上有问题,因此加try-catch一点都不ok 04/01 00:42