Chương trình cài đặt danh sách liên kết vòng, nội dung của nút là số nguyên, chương trình có các chức năng:
- Thêm một nút: nếu nội dung nhập vào là chẵn thì thêm nút mới vào đầu danh sách, nếu nội dung là lẻ thì thêm nút mới vào cuối danh sách.
- Duyệt danh sách.
Code C#:
using System;
namespace DanhsachlienketvongDon
{
class Program
{
static void insert(ref Node plist, int x)
{
if(plist.next==null) //Nếu danh sách hiện tại rỗng
{
plist.info = x;
plist.next = plist;
}
else //Danh sách có phần tử
{
if(x%2==0) //x là số chẳng thì thêm vào đầu danh sách
{
Node newnode = new Node();
newnode.info = x;
newnode.next = plist.next;
plist.next = newnode;
}
else //x là số lẻ thì thêm vào cuối danh sách
{
Node newnode = new Node();
newnode.info = x;
newnode.next = plist.next;
plist.next = newnode;
plist = newnode;
}
}
}
static void printlist(Node plist)
{
Node p = plist.next;
while(p!=plist)
{
Console.Write(p.info+"; ");
p = p.next;
}
Console.Write(plist.info+".");
}
static void Main(string[] args)
{
Node Plist=new Node();
while(1==1)
{
Console.WriteLine();
Console.WriteLine("==========MENU==========");
Console.WriteLine("1. Nhap danh sach");
Console.WriteLine("2. Duyet danh sach");
Console.WriteLine("3. Thoat");
Console.WriteLine("========================");
Console.Write("Ban chon menu so: ");
string chon = Console.ReadLine();
if (chon == "1")
{
int x = 0;
while(1==1)
{
Console.Write("Hay nhap so roi bam enter (nhap -1 de ket thuc viec nhap): ");
x = int.Parse(Console.ReadLine());
if (x != -1)
{
insert(ref Plist, x);
}
else
{
break;
}
}
}
else if (chon == "2")
{
Console.WriteLine();
Console.Write("Ket qua Duyet danh sach: ");
printlist(Plist);
}
else
{
break;
}
}
}
}
class Node
{
public int info;
public Node next;
public Node()
{
this.info = 0;
this.next = null;
}
}
}
Code C++:
//Viết chương trình có cài đặt danh sách liên kết vòng, nội dung của nút là số nguyên, chương trình có các chức năng:
//- Thêm một nút: nếu nội dung nhập vào là chẵn thì thêm nút mới vào đầu danh sách, nếu nội dung là lẻ thì thêm nút mới vào cuối danh sách.
//- Duyệt danh sách.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream.h>
//Khai báo kiểu dữ liệu Node
struct Node {
int info;
struct Node * next;
};
void insert(Node* &plist, int x)
{
if(plist->next==NULL) //Nếu danh sách hiện tại rỗng
{
plist->info = x;
plist->next = plist;
}
else //Danh sách có phần tử
{
if(x%2==0) //x là số chẳng thì thêm vào đầu danh sách
{
Node* newnode = (Node *) malloc (sizeof(Node));
newnode->info = x;
newnode->next = plist->next;
plist->next = newnode;
}
else //x là số lẻ thì thêm vào cuối danh sách
{
Node* newnode = (Node *) malloc (sizeof(Node));
newnode->info = x;
newnode->next = plist->next;
plist->next = newnode;
plist = newnode;
}
}
}
void printlist(Node* &plist)
{
Node p = plist->next;
while(p!=plist)
{
cout<<p->info<<"; ";
p = p->next;
}
cout<<plist->info<<".";
}
int main()
{
Node* Plist=(Node *) malloc (sizeof(Node));
while(1==1)
{
cout<<"\n==========MENU==========";
cout<<"\n1. Nhap danh sach";
cout<<"\n2. Duyet danh sach";
cout<<"\n3. Thoat";
cout<<"\n========================";
cout<<"\nBan chon menu so: ";
int chon=0;
scanf("%d", &chon);
if (chon == 1)
{
int x = 0;
while(1==1)
{
cout<<"Hay nhap so roi bam enter (nhap -1 de ket thuc viec nhap): ";
int x = -1;
scanf("%d", &x);
if (x != -1)
{
insert(Plist, x);
}
else
{
break;
}
}
}
else if (chon == 2)
{
cout<<"\nKet qua Duyet danh sach: ";
printlist(Plist);
}
else
{
break;
}
}
return 0;
}
Hy vọng hữu ích với bạn!
Nosomovo