Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

using System;

using System.Collections;
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;
using System.Xml.Linq;
using static QueueLinklist.Form1;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace QueueLinklist
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();

public class Node


{
public int element;
public Node next;
public Node(int element, Node next)
{
this.element = element;
this.next = next;
}
}
public class QueueLinkedlist
{
public Node front;
public Node rear;
public int size;
public bool IsEmpty()
{
return size == 0;
}
public int Length()
{
return size ;
}
public void EnQueue(int element)
{
Node newest = new Node(element, null);
if (IsEmpty())
{
front = rear = newest;
}
else
{
rear.next = newest;
rear = newest;
}
size++;

}
public int DeQueue()
{
if (IsEmpty())
{
return -1;
}
int element = front.element;
front = front.next;
size--;
if (IsEmpty())
{
rear = null;
}
return element;
}
}

private void txtPT_TextChanged(object sender, EventArgs e)


{

}
private QueueLinkedlist queue = new QueueLinkedlist();

private void btnAdd_Click(object sender, EventArgs e)


{
int element;
if (int.TryParse(txtPT.Text, out element))
{
queue.EnQueue(element);
int index = lstHT.Items.Count + 1;
txtPT.Clear();

string itemsString = "";


Node node = queue.front;
while (node != null)
{
itemsString += node.element + " ";
node = node.next;
}

lstHT.Items.Add($"List phần tử : {itemsString}");


}
else
{
MessageBox.Show("Vui lòng nhập số nguyên hợp lệ!");
}

private void lstHT_SelectedIndexChanged(object sender, EventArgs e)


{

private void btnDelete_Click(object sender, EventArgs e)


{

int deletedElement = queue.DeQueue();


if (deletedElement != -1)
{

lstHT.Items.Clear();
Node node = queue.front;
string itemstring = "";
while (node != null)
{
itemstring += node.element + " ";
lstHT.Items.Add($"Phần tử đã xóa {itemstring}");
node = node.next;
}
}
else
{
MessageBox.Show("Hàng đợi đã rỗng hoặc không thể xóa phần tử đầu tiên!");
}
}

private void btnHTSL_Click(object sender, EventArgs e)


{
int length = queue.Length();
MessageBox.Show($"Số lượng phần tử hiện tại trong hàng đợi là {length}");
}
}

Ví dụ:
- Thêm phần tử 5 6 7 8 vào hàng đợi :
- Xóa phần tử theo tính chất FIFO:
- Hiển thị số lượng phần tử có trong hàng đợi:
LEFT ROOT RIGHT
Code:
using System;

namespace dsc
{
class Program
{
class Node
{
public int element;
public Node left;
public Node right;
public Node(int e, Node l, Node r)
{
element = e;
left = l;
right = r;
}
}

class BinarySearchTree
{
private Node root;

public BinarySearchTree()
{
root = null;
}

public void Insert(int e)


{
if (root == null)
{
root = new Node(e, null, null);
return;
}

Node current = root;


Node parent = null;
while (current != null)
{
parent = current;
if (e < current.element)
{
current = current.left;
}
else if (e > current.element)
{
current = current.right;
}
else
{
// If e already exists in the tree, do nothing
return;
}
}

Node n = new Node(e, null, null);


if (e < parent.element)
{
parent.left = n;
}
else
{
parent.right = n;
}
}

public Node GetRoot()


{
return root;
}

public void Inorder(Node temproot)


{
if (temproot != null)
{
Inorder(temproot.left);
Console.Write(temproot.element + " ");
Inorder(temproot.right);
}
}
}

static void Main(string[] args)


{
BinarySearchTree bst = new BinarySearchTree();
bst.Insert(50);
bst.Insert(30);
bst.Insert(20);
bst.Insert(40);
bst.Insert(70);
bst.Insert(60);
bst.Insert(80);

Console.WriteLine("Inorder traversal of binary search tree:");


bst.Inorder(bst.GetRoot());
Console.ReadLine();
}
}
}

You might also like