PDA

View Full Version : سوال: عملیات درج و حذف درلیست پیوندی دوطرفه



gam be gam ta khoda
دوشنبه 16 اردیبهشت 1392, 11:39 صبح
سلام
یه منبع کامل و جامع راجع به لیست پیوندی دوطرفه می خوام که تمام عملیات درج و حذف رو آموزش داده باشه.
کسی سراغ نداره؟
ممنون

arash691
دوشنبه 16 اردیبهشت 1392, 19:13 عصر
چیزی که شما دنبالش هستین رو من با سی شارپ پیاده سازی کردم . این هم کد ها ی برنامه :

پیاده سازی کلاس گره دو طرفه :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dlink_list_test
{
class Double_Node
{
object data;
Double_Node front_link;
Double_Node back_link;
public Double_Node()
{
data = null;
front_link = null;
back_link = null;
}
public Double_Node(object data)
{
this.data = data;
front_link = null;
back_link = null;
}
public object Data
{
get { return data; }
set { data = value; }
}
public Double_Node Front_Link
{
get { return front_link; }
set { front_link = value; }
}
public Double_Node Back_Link
{
get { return back_link; }
set { back_link = value; }
}
}
}


پیاده سازی متد ها در کلاس لیست پیوندی دو طرفه ( عملیات جستجو - درج - حذف )


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dlink_list_test
{
class DLink_List
{
protected Double_Node header;
public DLink_List()
{
header = new Double_Node("0");
}
private Double_Node Find(object item) // گره مورد نظر را پیدا میکند
{
Double_Node current = new Double_Node();
current = header;
while (current.Data!=item)
{
current = current.Front_Link;
}
return current;
}
public void Insert(object new_item, object after) // عمل درج بعد از گره after
{
Double_Node current = new Double_Node();
Double_Node new_node = new Double_Node(new_item);
current = Find(after);
if (current.Front_Link!=null)
{
current.Front_Link.Back_Link = new_node;
}
new_node.Front_Link = current.Front_Link;
new_node.Back_Link = current;
current.Front_Link = new_node;
header.Data = (object)(Convert.ToInt32(header.Data) + 1);

}
public void Remove(object r) // گره ای را حذف می کند
{
Double_Node dn = Find(r); // ابتدا گره را پیدا میکند
if (!(dn.Front_Link == null)) // اگر گره بعد از ان تهی نبود عمل حذف را انجام می دهد
{
dn.Back_Link.Front_Link = dn.Front_Link; // لینک گره قبل گره ی پیدا شده را به گره
//بعد از گره ی پیدا شده وصل می کند
dn.Front_Link.Back_Link = dn.Back_Link;
dn.Front_Link = null;
dn.Back_Link = null;
header.Data = (object)(Convert.ToInt32(header.Data) - 1);
}
}
public void Print_List() // چاپ کردن لیست
{
Double_Node current = new Double_Node();
current = header;
while (!(current.Front_Link==null))
{
Console.WriteLine(current.Front_Link.Data + " ");
current = current.Front_Link;
}
Console.WriteLine("Count = " + header.Data);
}
}
}


استفاده از کلاس ها در main برنامه :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dlink_list_test
{
class Program
{
static void Main(string[] args)
{
DLink_List dlist = new DLink_List();
string[] new_item = { "ali", "reza", "milad", "hasan", "hamed" };
dlist.Insert(new_item[0], "0");
for (int i = 1; i < new_item.Length; i++)
{
dlist.Insert(new_item[i], new_item[i - 1]);
}
dlist.Print_List();
Console.WriteLine();
Console.WriteLine("remove ( milad ) ");
Console.WriteLine();
dlist.Remove(new_item[2]);
dlist.Print_List();
Console.ReadKey();
}
}
}


کد برنامه :