Bài viết bao gồm Video + Code
Video:
Code:
Video:
Code:
#include <iostream>
#define MaxLength 50
using namespace std;
typedef int ElementType;
typedef int Position;
class List{
ElementType Elements[MaxLength];
Position Last;
public:
void MakeNull(){
Last = 0;
}
void Insert(ElementType , Position );
void Delete(Position);
void Print();
void Sort();
};
void List::Print(){
for(int i=0;i<Last;i++)
cout<<Elements[i]<<" ";
}
void List::Insert(ElementType X, Position P){
for (int i=Last;i>=P;i--)
Elements[i]=Elements[i-1];
Elements[P-1]=X;
Last++;
}
void List::Delete(Position P){
for(int i=P-1;i<Last-1;i++)
Elements[i]=Elements[i+1];
Last--;
}
void List::Sort(){
for(int i=0;i<Last-1;i++)
for(int j=i+1;j<Last;j++)
if (Elements[i]>Elements[j]){
ElementType t = Elements[i];
Elements[i] = Elements[j];
Elements[j] = t;
}
}
int main(){
List L;
L.MakeNull();
int n;
cout<<"Danh sach co may phan tu? ";
cin>>n;
ElementType t;
for(int i=1;i<=n;i++){
cout<<"nhap phan tu thu "<<i<<": ";
cin>>t;
L.Insert(t,i);
}
cout<<"---------------\n";
cout<<"L: ";
L.Print();
cout<<"\n---------------\n";
cout<<"Nhap vi tri cua phan tu muon xoa? ";
cin>>n;
L.Delete(n);
cout<<"\n---------------\n";
cout<<"L sau khi xoa: ";
L.Print();
cout<<"\n---------------\n";
cout<<"L sau sap xep: ";
L.Sort();
L.Print();
return 0;
}