Professional Documents
Culture Documents
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
namespace BAITAP_5
{
public partial class Form1 : Form
{
private int[] myarray;
private int size = 0;
private int top = 0;
public Form1()
{
InitializeComponent();
myarray = new int[0];
}
int i = 0;
int Count = 1;
//Nhap mang
private void txtNhap_mang_TextChanged(object sender, EventArgs e)
{
size = int.Parse(txtNhap_mang.Text);
myarray = new int[size];
txtHT.Text = "So luong phan tu " + size;
txtNhap_mang.Clear();
}
//Phan tu
private void txtNhap_Ptu_TextChanged(object sender, EventArgs e)
{
}
public void btnNhap_Ptu_Click(object sender, EventArgs e)
{
if (top == size)
{
MessageBox.Show("Overflow", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
int element = int.Parse(txtNhap_Ptu.Text);
myarray[top++] = element;
}
//chèn vị trí bất kỳ
private void btn_SAP_Click(object sender, EventArgs e)
{
int insertPos = int.Parse(txtVT.Text);
int insertValue = int.Parse(txtNhap_Ptu.Text);
if (top == size)
{
MessageBox.Show("Overflow", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (insertPos >= top)
{
MessageBox.Show("Invalid index", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
// Dịch chuyển các phần tử từ vị trí chèn về sau một vị trí
for (int i = top; i > insertPos; i--)
{
myarray[i] = myarray[i - 1];
}
}
private void txtVT_TextChanged(object sender, EventArgs e)
{
}
//Them vao dau mang
public void btnAdd_Click(object sender, EventArgs e)
{
if (top == size)
{
MessageBox.Show("Overflow", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
int AddHead = int.Parse(txtNhap_Ptu.Text);
// sao chep chỉ số bắt đầu của mảng array chỉ số vị trí cần chèn của mảng đc sao chép
Array.Copy(myarray, 0, myarray, 1, top++);
myarray[0] = AddHead;
}
//Xóa Phần tử tại ví trí bất kỳ
private void btnXoa_Ptu_BK_Click(object sender, EventArgs e)
{
int deletePos = int.Parse(txtVT.Text);
if (pos>= 0)
{
MessageBox.Show($"Gia tri {searchValue} duoc tim thay tai vi tri {pos}");
}
else
{
MessageBox.Show($"Gia tri {searchValue} khong duoc tim thay trong mang");
}
txtNhap_Ptu.Clear();
txtVT.Clear();
}
}
}
KẾT QUẢ
Nhập kích thước của Stack là 6 :
Sau đó thêm lần lượt các phần tử 1, 2, 3, 4, 5,6 vào trong Stack :