Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

#include <windows.

h>
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct TTSV
{ char ten[30];
float diem;
};
typedef struct Node
{ TTSV Data;
Node *Next;
};

typedef Node *LinkList;


LinkList L;
void MakeNull_List(LinkList *Header){
(*Header)=(Node*)malloc(sizeof(Node));
(*Header)->Next= NULL;
}
//nhap danh sach
void Nhap()
{TTSV a;
char ch;
MakeNull_List(&L);
Node *p,*N;
do{
cout<<"ho va ten: ";
fflush(stdin);
gets(a.ten);
cout<<"Diem: ";
fflush(stdin);
cin>>a.diem;
N=(Node*)malloc(sizeof(Node));
N->Data=a;
N->Next=NULL;
if(L->Next==NULL)
{
L->Next=N;
p=L->Next;
}
else {
p->Next=N;
p=p->Next;
}
cout<<"nhap nua ko?C/K";
cin>>ch;
}while ((ch=='c')||(ch=='C'));
}
//xuat danh sach
void Xuat_DS()
{Node *p;
p=L->Next;
while (p!=NULL)
{ cout<<p->Data.ten<<"\t"<<p->Data.diem<<"\n";
p=p->Next;
}
}

//Tim den vi tri k


Node* Locate(int k, LinkList L)
{ Node* P;
int i=1;
P = L->Next;
while ((P!= NULL) && (i != k))
{i++;
P = P->Next;
}
return P;
}
//xoa phan tu sau vi tri k
void Xoa_PT(LinkList &L,int k)
{
Node *P,*T;
P=Locate(k,L);
if (P->Next!=NULL){
T=P->Next;
P->Next=T->Next;
free(T);
}
}
int main()
{
Nhap();
Xuat_DS();
Node *t;
int k;
cout<<"Nhap vi tri can xoa:";
cin>>k;
t=Locate(k,L);
Xoa_PT(L,k);
cout<<"\n DS sau khi xoa:\n";
Xuat_DS();
}

You might also like