Bài làm của anh Đặng Thế Phúc làm. Các bạn tham khảo nha.
[code]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bai1._2._4DSnoidon
{
class Node
{
public int x;
public Node next;
}
class Program
{
static Node l;
static void Nhap()
{
l = null; Node tg; int i = 1; int kt;
Console.WriteLine("Nhap danh sach cac so nguyen:");
do
{
tg = new Node();
Console.WriteLine("nhap so thu {0}:", i++);
tg.x = int.Parse(Console.ReadLine()); tg.next = null;
if (l == null) { l = tg; }
else { tg.next = l; l = tg; }
Console.WriteLine("Ban muon nhap tiep chu?dong y nhan 1 nguoc lai nhan 0");
kt = int.Parse(Console.ReadLine());
} while (kt != 0);
}
static void Hien()
{
Console.WriteLine("Cac phan tu trong danh sach:");
Node tg = l;
while (tg != null)
{
Console.Write(tg.x + "\t");
tg = tg.next;
}
Console.WriteLine();
}
static void Timmax()
{
int max = int.MinValue;
Node tg = l;
while (tg != null)
{
if (max < tg.x) max = tg.x;
tg = tg.next;
}
Console.WriteLine("Phan tu lon nhat la" + max);
}
static void Tong()
{
int t = 0;
Node tg = l;
while (tg != null)
{
t = t + tg.x;
tg = tg.next;
}
Console.WriteLine("Tong cac phan tu la" + t);
}
static bool KTNT(int x)
{
bool ok = true;
for (int i = 2; i <= x / 2; i++)
{
if (x % i == 0) { ok = false; break; }
}
return ok;
}
static void Demnt()
{
int d = 0; Node tg = l;
while (tg != null)
{
if (KTNT(tg.x) == true) d++;
tg = tg.next;
}
Console.WriteLine("Trong danh sach co {0} so nguyen to", d);
}
static void Dem()
{
int dem0 = 0; int demduong = 0; int demam = 0; Node tg = l;
while (tg != null)
{
if (tg.x == 0) dem0++;
else if (tg.x > 0) demduong++;
else demam++;
tg = tg.next;
}
Console.WriteLine("Trong danh sach co {0} so 0;co {1} so duong va co {2} so am", dem0, demduong, demam);
}
static void Demso(int x)
{
int d = 0; Node tg = l;
while (tg != null)
{
if (tg.x == x) d++;
tg = tg.next;
}
Console.WriteLine("Trong danh sach co {0} so bang {1}", d, x);
}
static void Soduongmin()
{
int minduong = int.MaxValue; int d = 0;
Node tg = l;
while (tg != null)
{
if (tg.x < minduong && tg.x > 0) { minduong = tg.x; d++; }
tg = tg.next;
}
if (d == 0) Console.WriteLine("khong co phan tu duong nao trong danh sach");
else Console.WriteLine("Phan tu duong nho nhat la" + minduong);
}
static void Xoa(int vitri)
{
Node tg = l; int d = 1; Node t = null;
while (tg != null && d != vitri)
{
t = tg; tg = tg.next; d++;//t=q
}
if (tg == null) Console.WriteLine("loi vi tri");
else
{
if (tg == l) l = tg.next;// xoa dau
else if (tg.next == null) t.next = null;//xoa cuoi
else { t.next = tg.next; }//xoa tai vi tri ko phai dau hay cuoi
Console.WriteLine("DS sau khi xoa vi tri {0} la :",vitri);
Hien();
}
}
static void Xoatruoc(int vitri)
{
Node tg = l; int d = 1; Node q = l; Node t = null;
while(q!=null&&d!=vitri)
{
q=q.next;d++;//tim nut q
}
while (tg != null && tg.next != q)
{
t = tg; tg = tg.next;//t tro vao nut can xoa
}
if (q==null) Console.WriteLine("loi vi tri");
else
{
if (q== l.next) l = q;// xoa dau
else { t.next=tg.next; }//xoa truoc vi tri ko phai l.next(xoa nut truoc nut q tro den)
Console.WriteLine("DS sau khi xoa truoc vi tri {0} la :", vitri);
Hien();
}
}
static void Xoagiatri(int x)
{
Node p = l;
Node t = p;
while (p != null && p.x != x)
{
t = p;
p = p.next;
}
if (p == null) Console.WriteLine("x khong ton tai trong danh sach");
else
{
if (p == l) l = l.next;
else if (p.next == null) t.next = null;
else t.next = p.next;
Console.WriteLine("DS sau khi xoa gia tri {0} la :", x);
Hien();
}
}
static void Main(string[] args)
{
Nhap();
Hien();
Timmax();
Tong();
Demnt();
Dem();
Demso(6);
Soduongmin();
Xoa(1);
Xoatruoc(4);
Xoagiatri(;
Console.ReadKey();
}
}
}
[code]using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bai1._2._4DSnoidon
{
class Node
{
public int x;
public Node next;
}
class Program
{
static Node l;
static void Nhap()
{
l = null; Node tg; int i = 1; int kt;
Console.WriteLine("Nhap danh sach cac so nguyen:");
do
{
tg = new Node();
Console.WriteLine("nhap so thu {0}:", i++);
tg.x = int.Parse(Console.ReadLine()); tg.next = null;
if (l == null) { l = tg; }
else { tg.next = l; l = tg; }
Console.WriteLine("Ban muon nhap tiep chu?dong y nhan 1 nguoc lai nhan 0");
kt = int.Parse(Console.ReadLine());
} while (kt != 0);
}
static void Hien()
{
Console.WriteLine("Cac phan tu trong danh sach:");
Node tg = l;
while (tg != null)
{
Console.Write(tg.x + "\t");
tg = tg.next;
}
Console.WriteLine();
}
static void Timmax()
{
int max = int.MinValue;
Node tg = l;
while (tg != null)
{
if (max < tg.x) max = tg.x;
tg = tg.next;
}
Console.WriteLine("Phan tu lon nhat la" + max);
}
static void Tong()
{
int t = 0;
Node tg = l;
while (tg != null)
{
t = t + tg.x;
tg = tg.next;
}
Console.WriteLine("Tong cac phan tu la" + t);
}
static bool KTNT(int x)
{
bool ok = true;
for (int i = 2; i <= x / 2; i++)
{
if (x % i == 0) { ok = false; break; }
}
return ok;
}
static void Demnt()
{
int d = 0; Node tg = l;
while (tg != null)
{
if (KTNT(tg.x) == true) d++;
tg = tg.next;
}
Console.WriteLine("Trong danh sach co {0} so nguyen to", d);
}
static void Dem()
{
int dem0 = 0; int demduong = 0; int demam = 0; Node tg = l;
while (tg != null)
{
if (tg.x == 0) dem0++;
else if (tg.x > 0) demduong++;
else demam++;
tg = tg.next;
}
Console.WriteLine("Trong danh sach co {0} so 0;co {1} so duong va co {2} so am", dem0, demduong, demam);
}
static void Demso(int x)
{
int d = 0; Node tg = l;
while (tg != null)
{
if (tg.x == x) d++;
tg = tg.next;
}
Console.WriteLine("Trong danh sach co {0} so bang {1}", d, x);
}
static void Soduongmin()
{
int minduong = int.MaxValue; int d = 0;
Node tg = l;
while (tg != null)
{
if (tg.x < minduong && tg.x > 0) { minduong = tg.x; d++; }
tg = tg.next;
}
if (d == 0) Console.WriteLine("khong co phan tu duong nao trong danh sach");
else Console.WriteLine("Phan tu duong nho nhat la" + minduong);
}
static void Xoa(int vitri)
{
Node tg = l; int d = 1; Node t = null;
while (tg != null && d != vitri)
{
t = tg; tg = tg.next; d++;//t=q
}
if (tg == null) Console.WriteLine("loi vi tri");
else
{
if (tg == l) l = tg.next;// xoa dau
else if (tg.next == null) t.next = null;//xoa cuoi
else { t.next = tg.next; }//xoa tai vi tri ko phai dau hay cuoi
Console.WriteLine("DS sau khi xoa vi tri {0} la :",vitri);
Hien();
}
}
static void Xoatruoc(int vitri)
{
Node tg = l; int d = 1; Node q = l; Node t = null;
while(q!=null&&d!=vitri)
{
q=q.next;d++;//tim nut q
}
while (tg != null && tg.next != q)
{
t = tg; tg = tg.next;//t tro vao nut can xoa
}
if (q==null) Console.WriteLine("loi vi tri");
else
{
if (q== l.next) l = q;// xoa dau
else { t.next=tg.next; }//xoa truoc vi tri ko phai l.next(xoa nut truoc nut q tro den)
Console.WriteLine("DS sau khi xoa truoc vi tri {0} la :", vitri);
Hien();
}
}
static void Xoagiatri(int x)
{
Node p = l;
Node t = p;
while (p != null && p.x != x)
{
t = p;
p = p.next;
}
if (p == null) Console.WriteLine("x khong ton tai trong danh sach");
else
{
if (p == l) l = l.next;
else if (p.next == null) t.next = null;
else t.next = p.next;
Console.WriteLine("DS sau khi xoa gia tri {0} la :", x);
Hien();
}
}
static void Main(string[] args)
{
Nhap();
Hien();
Timmax();
Tong();
Demnt();
Dem();
Demso(6);
Soduongmin();
Xoa(1);
Xoatruoc(4);
Xoagiatri(;
Console.ReadKey();
}
}
}