danh sách liên kết đơn***


#include<iostream>
#include <string>
#include <iomanip>
using namespace std;

struct sinhvien{
 string ten;
 int tuoi;
 string diachi;
};
struct Node{
 sinhvien *data;
 Node *next;
};
struct Danhsach{
 Node * dau;
 Node * cuoi;

};

Danhsach * ds;

void khoitao(){
 ds = new Danhsach();
 ds -> dau = ds -> cuoi = NULL;
}

void ktrarong(){
 if(ds -> dau = ds -> cuoi = NULL)
  cout << "danh sach nay rong ";
 else 
  cout <<"k rong ";
}

void themcuoi(Node * node){
 if(ds -> dau == NULL){
  ds -> dau = ds -> cuoi = node;
 }
 else{
  ds -> cuoi -> next = node;
  ds -> cuoi = node;
 }
}

Node *getNode(string ten, int tuoi, string diachi){
 Node *p = new Node();
 p -> data = new sinhvien();
 p -> data -> ten = ten;
 p -> data -> tuoi = tuoi;
 p -> data -> diachi = diachi;
 p -> next = NULL;
 return p;
}

void input(){
 int n;
 cout << "nhap so sinhvien ";
 cin >> n;
 cin.ignore();
 khoitao();
 for(int i = 1; i <= n; i++){
  string ten;
  int tuoi;
  string diachi;
  cout << "nhap vao ten sinh vien: [" << i << "] = ";
  getline(cin, ten);
  cout << "nhap vao tuoi: ";
  cin >> tuoi;
  cin.ignore();
  cout << "nhap vao dia chi: ";
  getline(cin, diachi);
  Node *p = getNode(ten, tuoi, diachi);
  themcuoi(p);
 }
}

void in(){
 Node *p =ds -> dau;
 cout << endl;
 while( p != NULL){
  cout <<setw(20)<< p -> data -> ten << setw(15) << p->data->tuoi << setw(20) << p->data->diachi << endl;
  p = p -> next;
 }
}



// 1 3 2 4
// 1 2 3 4

//void sapxep(){
// for( Node *p = dau ; p != cuoi; p = p -> next){
//  for(Node *q = p ->next; q != NULL; q = q -> next){
//   if(p -> data > q -> data){
//    hoanvi(p->data, q->data);
//   }
//  }
// }
//}


// dau = 1 -> 2 -> 3
//dau = 2 -> 3
void xoadau(){
 Node *p = ds -> dau;
 ds -> dau = ds ->dau ->next;
 p = NULL;
}

void xoacuoi(){
 for(Node *i = ds ->dau; i != ds-> cuoi; i = i -> next){
  if(i -> next == ds ->cuoi){
   ds-> cuoi = i;
   i -> next = NULL;
   break;
  }
 }
}


//1 2 -> 3 -> 4 5 6
//1 -> 2 -> 4 -> 5 -> 6
void xoa(int k){
 int index = 1;
 for(Node *i = ds->dau; i != ds->cuoi; i = i -> next){
  if(index == k){
   i -> next =  i -> next -> next;
   break;
  }
  index ++;
 }
}


/**
tao menu nhap vao danh sach sinh vien.
-> chen vao dau danh sach
->chen vao cuoi danh sach
-> chen vao 1 vi tri bat ky

-> xoa sinh vien o dau
-> xoa sinh vien o cuoi
-> xoa sinh vien o 1 vi tri bat ky

-> sap xep sinh vien theo ten a.compare(b) > 0 -> a > b, = 0 -> a = b, < 0 -> a < b
*/

int main(){

 //inputChenVaoCuoi();
 input();
 cout << "danh sach lien ket la: ";
 in();
 //cout << endl << "danh sach sau sap xep la: ";
 //sapxep();

 
}

Nhận xét

Bài đăng phổ biến từ blog này

Đổi chỗ chữ số đầu tiên và chữ số cuối cùng của một số

Chuyển đơn vị đo nhiệt độ C và F

Tìm số đẹp (lộc phát)