Các dạng bài cơ bản của danh sách nối đơn

Quyết tâm học

Latest topics

» Chúc Mừng Sinh Nhật h20 kute
by Admin Tue Aug 02, 2011 1:06 pm

» Hello my best friends
by moonngoc Mon Jun 27, 2011 8:28 pm

» Chúa Tể Của Những Chiếc Nhẫn II: Hai ngọn tháp
by nghiepanthi Tue Jun 14, 2011 7:06 am

» Bài cây nhị phân
by Admin Sat Jun 04, 2011 10:15 am

» Tắc kè hoa - Rango
by nghiepanthi Fri Jun 03, 2011 6:53 am

» Mong ước kỷ niệm xưa
by Admin Tue May 31, 2011 10:05 pm

» Giáo trình bài tập thực hành
by Admin Tue May 31, 2011 7:03 pm

» Sẵn sàng cho kỳ thi tốt nghiệp THPT 2011
by Admin Tue May 31, 2011 6:49 pm

» Thi tốt nghiệp năm học 2010-2011
by Admin Tue May 31, 2011 6:43 pm

» Nhập thêm phần tử mà không làm thay thứ tự phần tử không giảm trong danh sách
by Admin Tue May 31, 2011 5:20 pm

» Mẫu gửi quà tặng
by Admin Tue May 31, 2011 11:21 am

» Bài tập 101
by Admin Tue May 31, 2011 9:50 am

» Bài tập 97
by Admin Tue May 31, 2011 9:50 am

» Bài tập 93
by Admin Tue May 31, 2011 9:49 am

» Bài tập 92
by Admin Tue May 31, 2011 9:49 am

» Bài tập 91
by Admin Tue May 31, 2011 9:48 am

» Bài tập 90
by Admin Tue May 31, 2011 9:48 am

» Bài tập 85
by Admin Tue May 31, 2011 9:47 am


You are not connected. Please login or register

Các dạng bài cơ bản của danh sách nối đơn

Go down  Thông điệp [Trang 1 trong tổng số 1 trang]

Admin

Admin
Admin

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(Cool;
Console.ReadKey();
}
}
}

http://lecongphuc92.ohmylife.net

Về Đầu Trang  Thông điệp [Trang 1 trong tổng số 1 trang]

Permissions in this forum:
Bạn không có quyền trả lời bài viết