Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct sinhvien {
- string name ;
- int age ;
- double gpa ;
- };
- struct Node {
- sinhvien s ;
- Node* next ;
- };
- #define node Node*
- int size (node head){
- int ans =0 ;
- while (head!= NULL ){
- ans++ ;
- head= head-> next ;
- }
- return ans ;
- }
- node makeNode (){
- sinhvien s ;
- cin.ignore();
- cout<<"Nhap ten sinh vien "<<endl;
- getline ( cin ,s.name );
- cout<<"Nhap tuoi : "<<endl;
- cin >> s.age;
- cout<<"Nhap gpa : "<<endl;
- cin >> s.gpa;
- node tmp = NULL;
- tmp = new Node ();
- tmp->s=s ;
- tmp->next= NULL;
- return tmp ;
- }
- void addFirst (node & head ){
- node tmp = makeNode();
- if ( head == NULL ){
- head= tmp ;
- return ;
- }
- tmp->next = head;
- head= tmp;
- }
- void addLast (node& head){
- node tmp = makeNode();
- if ( head== NULL){
- head= tmp;
- return ;
- }
- node p= head ;
- while ( p->next != NULL ){
- p=p->next ;
- }
- p->next = tmp;
- }
- void addMiddle ( node& head , int pos ){
- int n= size(head);
- if (head == NULL){
- node tmp = makeNode();
- head=tmp;
- return ;
- }
- if ( pos <=0 || pos > n+1 ){
- return ;
- }
- else if ( pos == 1 ){
- addFirst (head);
- }
- else if (pos== n+1 ){
- addLast(head);
- }
- else {
- node tmp = makeNode();
- node p= head;
- for (int i=1;i<pos-1;i++){
- p=p->next;
- }
- tmp->next = p->next ;
- p-> next = tmp;
- }
- }
- void inSV(sinhvien s ){
- cout<<"--------------------------"<<endl;
- cout<< "Ho ten sinh vien "<<s.name <<endl;
- cout<<"Tuoi sinh vien : "<< s.age <<endl;
- cout<<"Gpa : "<<s.gpa <<endl;
- cout<<"--------------------------"<<endl;
- }
- void inDSLK(node head ){
- while ( head!= NULL ){
- // insv
- inSV(head->s);
- head=head->next ;
- }
- }
- void removeFirst (node & head){
- head= head->next ;
- }
- void removeLast( node & head)
- {
- node p = head ;
- node truoc = p;
- while ( p->next != NULL ){
- truoc = p;
- p=p->next ;
- }
- truoc->next = NULL;
- }
- void removeMiddle (node & head, int pos ){
- int n= size(head);
- if ( head == NULL) return ;
- if ( pos == 1 ) {
- removeFirst(head);
- }
- else if ( pos == n ) {
- removeLast(head);
- }
- else {
- node p = head;
- for (int i=1;i<pos-1;i++){
- p=p->next ;
- }
- p->next= (p->next)->next ;
- }
- }
- void sapXep (node & head ){
- for (node p= head;p->next!=NULL;p=p->next ){
- node q= p;
- for (node m = q ; m->next!=NULL;m=m->next ){
- if ( q->s.gpa > m->s.gpa ){
- q=m;
- }
- }
- sinhvien tmp = p->s ;
- p->s=q->s;
- q->s= tmp;
- }
- }
- int main(){
- node head= NULL;
- // dslk ban dau la rong (null)
- while ( 1 ){
- for (int i=1;i<=50;i++){
- cout<<" "<<endl;
- }
- cout<<"---------------------------------"<<endl;
- cout <<"1.Them SV vao dau danh sach "<<endl;
- cout<<"2.Them SV vao cuoi danh sach "<<endl;
- cout<<"3.Them SV vao giua danh sach "<<endl;
- cout<<"4.Xoa SV o dau danh sach "<<endl;
- cout<<"5.Xoa SV o cuoi danh sach "<<endl;
- cout<<"6.Xoa SV o giua danh sach "<<endl;
- cout<<"7.In DSLK "<<endl;
- cout<<"8.Thoat "<<endl;
- cout<<"9.Sap xep theo GPA "<<endl;
- cout<<"Nhap lua chon "<<endl;
- cout<<"---------------------------------"<<endl;
- int lc ;
- cin >> lc ;
- if ( lc== 1) {
- addFirst (head);
- }
- else if (lc == 2 ){
- addLast(head);
- }
- else if ( lc== 3 ){
- int pos ;
- cout<<"Nhap vi tri can them "<<endl;
- cin.ignore();
- cin >> pos ;
- addMiddle(head,pos);
- }
- else if ( lc == 4 ){
- removeFirst ( head) ;
- }
- else if ( lc == 5 ){
- removeLast(head);
- }
- else if ( lc==6 ){
- int pos ;
- cout<<"Nhap vi tri can xoa "<<endl;
- cin >> pos ;
- removeMiddle(head,pos );
- }
- else if ( lc==7) {
- inDSLK(head);
- }
- else if ( lc == 8 ){
- break;
- }
- else if ( lc == 9){
- sapXep(head);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment