//A -> B -> C -> D
//[0] -> [1] -> [2] -> [3]
#include <iostream>
using namespace std;
//2 1
struct Node{
int data;
Node *next;
};
Node * dau;
Node * cuoi;
//chen vao cuoi danh sach lien ket
void insertCuoi(Node * node){
cuoi -> next = node;
cuoi = node;
}
void inputChenVaoCuoi(){
//1-10
dau = new Node();
dau->data = 1;
dau->next = NULL;
cuoi = dau;
for(int i = 2; i <= 10; i++){
Node *b = new Node();
b->data = i;
b->next = NULL;
insertCuoi(b);
}
}
void insertDau(Node * node){
//2 -> 1
// a = b;
node -> next = dau;
dau = node;
}
void inputChenVaoDau(){
//1-10
dau = new Node();
dau->data = 1;
dau->next = NULL;
cuoi = dau;
for(int i = 2; i <= 10; i++){
Node *b = new Node();
b->data = i;
b->next = NULL;
insertDau(b);
}
}
// 1 -> 2 -> 4 -> 5
// 3
//1 -> 2 -> 3 -> 4 ->5
void insertBatKy(int x, Node * node){
int index = 1;
Node * p = dau;
while(p != NULL){
if(index == x){
//vi tri can chen
Node * tmp = p->next;
p -> next = node;
node -> next = tmp;
break;
}
p = p -> next;
index++;
}
}
void xoadau(){
Node *p = dau;
dau = dau -> next;
p = NULL;
}
//1 -> 2 -> 3
void xoacuoi(){
for(Node *i = dau; i != cuoi; i = i -> next){
if( i -> next == cuoi){
cuoi = i;
i -> next = NULL;
break;
}
}
}
// 2-> 3-> 5-> 7
xoabatky(int x){
int index = 1;
for(Node *i = dau; i != cuoi; i = i -> next){
if(index == x){
i -> next = i -> next -> next;
break;
}
index++;
}
}
void print(){
Node * p = dau;
cout << endl;
while(p != NULL){
cout << p -> data << " ";
p = p -> next;
}
}
int main(){
insertBatKy(3, p);
print();
// xoadau();
// xoacuoi();
xoabatky(2);
print();
return 0;
}
Nhận xét
Đăng nhận xét