You are on page 1of 7

using System;

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)
{

private void btnNhap_mang_Click(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;

txtHT.Text += "\r\nMảng sau khi nhập:\r\n";

for (int i = 0; i < top; i++)


{
txtHT.Text += myarray[i] + " ";
}
}
txtNhap_Ptu.Clear();
}
//Hien thi
private void txtHT_TextChanged(object sender, EventArgs e)
{

}
//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];
}

// Chèn giá trị mới vào vị trí cần chèn


myarray[insertPos] = insertValue;
top++;

txtHT.Text = "\r\nmang sau khi chèn:\r\n";

for (int i = 0; i < top; i++)


{
txtHT.Text += myarray[i] + " ";
}
}
txtNhap_Ptu.Clear();
txtVT.Clear();

}
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;

txtHT.Text = "\r\nmang sau khi them:\r\n";

for (int i = 0; i < top; i++)


{
txtHT.Text += myarray[i] + " ";
}
}
txtNhap_Ptu.Clear();

}
//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 (deletePos >= top)


{
MessageBox.Show("Chỉ số không phù hợp", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
// Dịch chuyển các phần tử từ vị trí xóa về sau một vị trí
for (int i = deletePos; i < top - 1; i++)
{
myarray[i] = myarray[i + 1];
}

// Giảm số phần tử trong mảng đi một đơn vị


top--;

txtHT.Text = "\r\nMang sau khi xóar\n";

for (int i = 0; i < top; i++)


{
txtHT.Text += myarray[i] + " ";
}
}
txtNhap_Ptu.Clear();
txtVT.Clear();
}
private void btnXoa_Click(object sender, EventArgs e)
{
txtHT.Clear();
txtNhap_Ptu.Clear();
txtNhap_mang.Clear();
txtVT.Clear();
size = 0;
myarray = new int[0];
top = 0;
int i = 0;
int Count = 1;
}

private void btnSearch_Click(object sender, EventArgs e)


{
int searchValue = int.Parse(txtNhap_Ptu.Text);

int pos = Array.IndexOf(myarray, searchValue);

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 :

Xóa phần tử tại vị trí 0 và tiếp tục xóa vị trí 1:


Xóa phần tử bất kỳ:

Thêm số 9 vào đầu mảng:

Chèn số 3 vào vị trí thứ 3:


Tìm kiếm phần tử chứa giá trị 5:

You might also like