using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LinkedList
{
public partial class Form1 : Form
{
singlrLinkedList sll;
public Form1()
{
InitializeComponent();
startup();
}
public void startup()
{
sll = new LinkedList.singlrLinkedList();
}
private void button1_Click(object sender, EventArgs e)
{
sll.addLinkedList(textBox1.Text, Int32.Parse(textBox2.Text.ToString()));
textBox1.Clear();
textBox2.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
textBox3.Text = sll.showall();
}
private void button3_Click(object sender, EventArgs e)
{
sll.deletepos(Convert.ToInt32(textBox4.Text));
textBox3.Text = sll.showall();
}
}
public class node
{
public string name;
public int age;
public node next;
public node()
{
next = null;
}
}
public class singlrLinkedList
{
public node header;
public node cur;
public node newnode;
public singlrLinkedList()
{
header = null;
cur = null;
}
public void addLinkedList(string name ,int age)
{
newnode = new LinkedList.node();
newnode.name = name;
newnode.age = age;
if (header == null)
{
header = newnode;
cur = header;
}
else
{
cur.next = newnode;
cur = cur.next;
}
}
public void deletepos(int i)
{
node delx;
cur = header;
if(i==1)
{
header = cur.next;
cur = header;
}
else
{
for(int j=1;j<i-1;j++)
{
cur = cur.next;
}
delx = cur.next;
if(delx != null)
{
cur.next = delx.next;
}
}
}
public string showall()
{
int i = 1;
string str = "";
cur = header;
while(cur !=null)
{
str = str + i.ToString() + " : " + cur.name + "," + cur.age.ToString()+Environment.NewLine;
i++;
cur = cur.next;
}
return str;
}
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น