[問題] 並未將物件參考設定為物件的執行個體
老師給了一段單向佇列的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;
}
}
}
--
推
12/25 16:07,
12/25 16:07
推
12/25 16:56,
12/25 16:56
推
12/25 17:34,
12/25 17:34
推
12/25 17:39,
12/25 17:39
推
12/25 18:10,
12/25 18:10
推
12/25 20:07,
12/25 20:07
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 49.158.2.110
→
03/31 03:20, , 1F
03/31 03:20, 1F
→
03/31 03:20, , 2F
03/31 03:20, 2F
→
03/31 04:22, , 3F
03/31 04:22, 3F
→
03/31 04:22, , 4F
03/31 04:22, 4F
→
03/31 04:26, , 5F
03/31 04:26, 5F
推
03/31 08:05, , 6F
03/31 08:05, 6F
→
03/31 08:06, , 7F
03/31 08:06, 7F
→
03/31 08:07, , 8F
03/31 08:07, 8F
→
03/31 08:08, , 9F
03/31 08:08, 9F
→
03/31 08:09, , 10F
03/31 08:09, 10F
→
03/31 08:11, , 11F
03/31 08:11, 11F
→
03/31 08:12, , 12F
03/31 08:12, 12F
→
03/31 08:21, , 13F
03/31 08:21, 13F
→
03/31 08:21, , 14F
03/31 08:21, 14F
→
03/31 21:18, , 15F
03/31 21:18, 15F
→
03/31 21:19, , 16F
03/31 21:19, 16F
→
03/31 21:19, , 17F
03/31 21:19, 17F
→
04/01 00:41, , 18F
04/01 00:41, 18F
→
04/01 00:41, , 19F
04/01 00:41, 19F
→
04/01 00:42, , 20F
04/01 00:42, 20F
討論串 (同標題文章)
完整討論串 (本文為第 6 之 9 篇):
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章