[英]can any body help me in validating the working of my function in a linked list C#. I'm new to this
[英]Double Linked List C#. How do I delete all the nodes in my double linked list
我試圖刪除我的雙向鏈表中的所有節點,但遇到了麻煩。 調試時我的變量 this.Start 是 null,這導致我的代碼無法工作。 我想不出解決辦法。 是否有替代方法來刪除我的節點或修復我當前的代碼。 請幫忙 謝謝:D
public partial class Form2 : Form
{
SeatManager _seatManager = new SeatManager();
DoubleLinkedList seatList = new DoubleLinkedList();
Seat _seat = new Seat();
public Form2()
{
InitializeComponent();
}
private void buttonCreateAndDisplay_Click(object sender, EventArgs e)
{
Label labelSeat = new Label();
labelSeat.Click+=new EventHandler(HandleLabelClick);
Seat seat = _seatManager.InsertOneSeat(y, x);
labelSeat.Tag=seat;
labelSeat.Text=seat.ComputeSeatLabel();
}
}
}
}
}
private void HandleLabelClick(object sender, EventArgs e)
{
string labelName = "";
Label labelSeat = (Label)sender;
Seat seat = (Seat)labelSeat.Tag;
labelMessage.Text=labelSeat.Name;
if (seat.BookStatus==false)
{
seat=_seatManager.FindOneSeatToBook(seat.Row, seat.Column);
}
else
{
seat=_seatManager.FindOneSeatToUnbook(seat.Row, seat.Column);
}
if (seat.BookStatus==false)
{
labelSeat.BackColor=Color.LightGray;
}
else
{
labelSeat.BackColor=Color.LightGoldenrodYellow;
}
}
}
private void buttonReset_Click(object sender, EventArgs e)
{
seatList.ClearNodes(maxRow,maxColumn);
seatList.deleteAllNodes();
_resetbuttonclicked=true;
panelSeats.Controls.Clear();
buttonCreateAndDisplay_Click(sender, e);
}
雙鏈表
class DoubleLinkedList
{
public Node Start { get; set; }
public DoubleLinkedList()
{
this.Start=null;
}
public void InsertAtEnd(Seat seatData)
{
Node newNode = new Node(seatData);
if (this.Start==null)
{
this.Start=newNode;
return;
}
Node p = this.Start;
while (p.next!=null)
{
p=p.next;
}
p.next=newNode;
newNode.prev=p;
}
public Seat SearchByRowAndColumn(int pRow, int pColumn)
{
Node p = this.Start;
while (p!=null)
{
if ((p.seat.Column == pColumn) && (p.seat.Row == pRow))
{
break;
}
p=p.next;
}
if (p==null)
{
return null;
}
else
{
return p.seat;
}
}
public Seat ClearNodes(int pRow,int pColumn)
{
Node p = this.Start;
while (p != null)
{
p=p.prev;
p.next=null;
}
return null;
}
public void deleteAllNodes()
{
Node seat = new Node();
while (this.Start != null)
{
seat = this.Start;
this.Start = this.Start.next;
seat = null;
}
this.Start=null;
}
座位管理器
class SeatManager
{
DoubleLinkedList _seats;
public SeatManager()
{
_seats= new DoubleLinkedList();
}
public Seat InsertOneSeat(int row, int column)
{
Seat newSeat = new Seat();
newSeat.Row = row;
newSeat.Column = column;
newSeat.CanBook = true;
newSeat.Bookedby = "";
_seats.InsertAtEnd(newSeat);
return newSeat;
}
public Seat FindOneSeatToBook(int row, int column)
{
Seat seat = _seats.SearchByRowAndColumn(row, column);
seat.BookStatus=true;
return seat;
}
public Seat FindOneSeatToUnbook(int row, int column)
{
Seat seat = _seats.SearchByRowAndColumn(row, column);
seat.BookStatus = false;
return seat;
}
你不能只寫
public void deleteAllNodes()
{
this.Start = null;
}
讓垃圾收集器收拾殘局。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.