Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 34

Câu 1

#include<math.h>

void printOddNumbers(List L){

int i;

for(i=0; i<=L.Last-1; i++){

if( ((int) fabs(L.Elements[i]))%2==1)

printf("%d ",L.Elements[i]);

Câu 2

int member(int x, List L){

for(int i=0; i<=L.Last-1; i++){

if(x==L.Elements[i]){

return 1;

return 0;

Câu 3

void readList(List *L){

makenullList(*(&L));

int n, x;

scanf("%d",&n);

for(int i=0; i<n; i++){

scanf("%d",&x);

insertList(x,L->Last+1,&(*L));

Câu 4

#define Maxlength 100

typedef int ElementType;

typedef int Position;

typedef struct{
ElementType Elements[Maxlength];

Position Last;

}List;

List L;

Câu 5

void insertSet(ElementType x, List *pL){

pL->Last++;

pL->Elements[pL->Last-1]=x;

Câu 6

Position FirstList(List L){

return 1;

Position EndList(List L){

return L.Last+1;

ElementType Retrieve(Position P, List L){

return L.Elements[P-1];

Position Next(Position P, List L){

return P+1;

void unionSet(List L1, List L2, List *pL){

makenullList(pL);

Position P1,P2;

P1=FirstList(L1);

P2=FirstList(L2);

while(P1!=EndList(L1)){

insertSet(Retrieve(P1,L1),pL);

P1=Next(P1,L1);

while(P2!=EndList(L2)){

if(!member(Retrieve(P2,L2),L1)){
insertSet(Retrieve(P2,L2),pL);

P2=Next(P2,L2);

Câu 7

int locate(int x, List L){

int i;

for(i=0; i<L.Last; i++){

if(x==L.Elements[i])

return i+1;

return L.Last +1;

Câu 8

void intersection(List L1, List L2, List *pL){

makenullList(pL);

for(int i=0; i<=L1.Last-1; i++){

if(member(L1.Elements[i],L2)==1){

insertSet(L1.Elements[i],pL);

Câu 9

void normalize(List *pL){

Position p,q;

p=1;

while (p!=pL->Last+1){

q=p+1;

while (q!=pL->Last+1){

if (pL->Elements[p-1]==pL->Elements[q-1])

deleteList(q,pL);

else
q++;

p++;

Câu 10

void deleteList(Position P,List *L) {

if ((P<1) || (P>L->Last))

printf("Vi tri khong hop le\n");

else if (L->Last==0)

printf("Danh sach rong!");

else {

Position Q;

for(Q=P-1;Q<L->Last-1;Q++)

L->Elements[Q]=L->Elements[Q+1];

L->Last--;

Câu 11

void removeAll(ElementType X,List *L) {

Position P;

do {

P=locate(X,*L);

if (P!=L->Last+1)

deleteList(P,L);

} while (P!=L->Last+1);

Câu 12

void readSet(List *L) {

int i,N;

ElementType X;

makenullList(L);

scanf("%d",&N);
for(i=1;i<=N;i++) {

scanf("%d",&X);

if (!member(X,*L))

insertSet(X,L);

Câu 13

void copyEvenNumbers(List L1, List *L2){

makenullList(L2);

ElementType X;

Position p=1;

while (p!=L1.Last+1){

X=L1.Elements[p-1];

if (X%2==0)

insertList(X,L2->Last+1,L2);

p++;

Câu 14

void insertList(ElementType X, Position P, List*L){

if (L->Last==Maxlength)

printf("Danh sach day\n");

else if (P<1 || P>L->Last+1)

printf("Vi tri khong hop le/n");

else {

Position Q;

for(Q=L->Last; Q>=P;Q--)

L->Elements[Q]=L->Elements[Q-1];

L->Last++;

L->Elements[P-1]=X;

Câu 15
void erase(ElementType x,List *pL) {

Position P;

P=locate(x,*pL);

if (P!=pL->Last+1)

deleteList(P,pL);

Câu 16

void makenullList(List *L) {

L->Last=0;

Câu 17

void sort(List*pL){

Position p,q;

ElementType temp;

for(p=1;p<pL->Last;p++)

for(q=p+1;q<=pL->Last;q++)

if(pL->Elements[p-1]>pL->Elements[q-1]){

temp=pL->Elements[p-1];

pL->Elements[p-1]=pL->Elements[q-1];

pL->Elements[q-1]=temp;

Câu 18

void difference(List L1, List L2, List *pL){

makenullList(pL);

Position P;

P=1;

while(P!=L1.Last+1){

if(!member(L1.Elements[P-1],L2))

insertSet(L1.Elements[P-1],pL);

P++;

}
Câu 19

void readList(List *pL){

int i,n;

ElementType X;

makenullList(pL);

scanf("%d",&n);

for(i=1;i<=n;i++){

scanf("%d",&X);

insertList(X,pL->Last+1,pL);

20

float getAvg(List L){

int S=0;

Position P=1;

while (P!=L.Last+1){

S += L.Elements[P-1];

P++;

if(S!=0){

return (float)S/L.Last;

else{

return -10000.0000;

21

void printList(List L) {

Position P;

P = 1;

while (P != L.Last+1) {

printf("%d ",L.Elements[P-1]);

P = P+1;
}

printf("\n");

22

void insertSet(int x, List *pL){

pL->Last++;

pL->Elements[pL->Last-1]=x;

23

int locate1(int x, List L){

int i;

for(i=L.Last;i>=0;i--){

if(x==L.Elements[i])

return i+1;

return L.Last+1;

24

void intersection(List L1, List L2, List *pL){

makenullList(pL);

Position p=1;

while (p!=L1.Last+1){

if (member(L1.Elements[p-1],L2)){

insertSet(L1.Elements[p-1],pL);

p++;

25

void insertList(ElementType X,Position P, List *pL) {

if (pL->Last==Maxlength)

printf("Danh sach day");

else if ((P<1) || (P>pL->Last+1))


printf("Vi tri khong hop le");

else {

Position Q;

for(Q=pL->Last;Q>P-1;Q--)

pL->Elements[Q]=pL->Elements[Q-1];

pL->Elements[P-1]=X;

pL->Last++;

26

#include <stdio.h>

#include <stdlib.h>

#include <malloc.h>

typedef int ElementType;

struct Node

ElementType Element;

struct Node* Next;

};

typedef struct Node* Position;

typedef Position List;

void makenullList(List *pL){

(*pL)=(struct Node*)malloc(sizeof(struct Node));

(*pL)->Next=NULL;

void append(ElementType X, List *pL){

Position P=(*pL);

while (P->Next!=NULL)

P=P->Next;

struct Node* t=(struct Node*)malloc(sizeof(struct Node));

t->Element=X;
t->Next=P->Next;

P->Next=t;

void readList(List *pL){

int n;

ElementType X;

scanf("%d", &n);

makenullList(pL);

for(int i=1; i<=n; i++){

scanf("%d", &X);

append(X,pL);

void printList(List L){

Position P=L;

while (P->Next!=NULL){

printf("%d ",P->Next->Element);

P=P->Next;

printf("\n");

void deleteList(Position p, List *pL)

if (p->Next!=NULL)

Position t=p->Next;

p->Next = t->Next;

free(t);

Position locate(ElementType x, List L)

{
int Found=0;

Position p=L;

while(p->Next!=NULL && !Found)

if (x==p->Next->Element) Found=1;

else p=p->Next;

return p;

void removeAll(ElementType X, List *pL){

Position P=locate(X,*pL);

while (P->Next!=NULL){

deleteList(P, pL);

P=locate(X,*pL);

int main(){

List L;

ElementType X;

readList(&L);

printList(L);

scanf("%d",&X);

removeAll(X,&L);

printList(L);

return 0;

27

#include<stdio.h>

#include<stdlib.h>

typedef int ElementType;

struct Node{

ElementType Element;

struct Node* Next;

};
typedef struct Node* Position;

typedef Position List;

//ham

void makenullList(List *pL);

void append(ElementType x, List *pL);

void readList(List *pL);

void printList(List L);

float getAvg(List L);

//trien khai ham

void makenullList(List *pL){

(*pL)=(struct Node*)malloc(sizeof(struct Node));

(*pL)->Next=NULL;

void append(ElementType x, List *pL){

Position p=(*pL);

while(p->Next!=NULL){

p=p->Next;

Position t = (struct Node*)malloc(sizeof(struct Node));

t->Element = x;

t->Next = p->Next;

p->Next = t;

void readList(List *pL){

makenullList(&(*pL));

int i, n, x;

scanf("%d", &n);

i=1;

while(i<=n){

scanf("%d", &x);

Position p=(*pL);
while(p->Next!=NULL){

p=p->Next;

Position t=(struct Node*)malloc(sizeof(struct Node));

t->Element=x;

t->Next=p->Next;

p->Next=t;

i++;

void printList(List L){

Position p=L;

while(p->Next!=NULL){

printf("%d ", p->Next->Element);

p=p->Next;

float getAvg(List L){

float S=0, i=0;

Position p=(L);

while(p->Next!=NULL){

S+=(p->Next->Element);

p=p->Next;

i++;

if(i!=0){

return (S/i);

else{

return -10000.0;

int main(){
List L1;

makenullList(&L1);

readList(&L1);

printList(L1);

printf("\n%.3f", getAvg(L1));

return 0;

28

#include<stdio.h>

#define Maxlength 100

typedef struct{

int x, y;

}diem;

typedef struct{

int n;

diem A[Maxlength];

}Polygon;

29

#define Maxlength 100

struct DaThuc{

DonThuc A[Maxlength];

int so_luong;

};

30

#include <stdio.h>

typedef struct {

char MSSV[10];

char HoTen[50];

float DiemLT, DiemTH1, DiemTH2;

}SinhVien;

typedef struct{

SinhVien A[40];

int n;
}List;

List L;

31

DanhSach nhap(){

DanhSach L;

struct SinhVien sv;

L=dsRong();

int N,i;

char ms[10];

char ht[50];

scanf("%d ", &N);

for (i=1; i<=N; i++){

fgets(ms,10,stdin);

if (ms[strlen(ms)-1]=='\n')

ms[strlen(ms)-1]='\0';

strcpy(sv.MSSV,ms);

fgets(ht,50,stdin);

if (ht[strlen(ht)-1]=='\n')

ht[strlen(ht)-1]='\0';

strcpy(sv.HoTen,ht);

scanf("%f%f%f ",&sv.DiemLT,&sv.DiemTH1,&sv.DiemTH2);

if (tim(ms,L)==L.n+1){

chenCuoi(sv,&L);

return L;

32

int tim(char mssv[], DanhSach L){

int p=1;

int found=0;

while (p!=L.n+1 && !found){

if (strcmp(L.A[p-1].MSSV,mssv)==0)
found=1;

else

p++;

return p;

33

void hienthi(DanhSach L){

int p=1;

struct SinhVien s;

while(p!=L.n+1){

s=L.A[p-1];

printf("%s - %s - %.2f - %.2f - %.2f - %.2f\n",s.MSSV, s.HoTen, s.DiemLT, s.DiemTH1, s.DiemTH2,


s.DiemLT+s.DiemTH1+s.DiemTH2);

p++;

34

DanhSach dsRong(){

DanhSach L;

L.n=0;

return L;

35

void xoaTai(int p, DanhSach *pL){

if (pL->n==0)

printf("Danh sach rong");

else if (p<1 || p>pL->n)

printf("Vi tri khong hop le");

else {

int Q;

for(Q=p-1; Q<pL->n-1; Q++){

pL->A[Q] = pL->A[Q+1];
}

pL->n--;

36

void hienthiDat(DanhSach L){

int p=1;

float tdiem;

while (p!=L.n+1){

tdiem=L.A[p-1].DiemLT+L.A[p-1].DiemTH1+L.A[p-1].DiemTH2;

if (tdiem>=4){

printf("%s - %s - %.2f - %.2f - %.2f - %.2f\n", L.A[p-1].MSSV, L.A[p-1].HoTen, L.A[p-1].DiemLT, L.A[p-


1].DiemTH1, L.A[p-1].DiemTH2, tdiem);

p++;

37

void erase(ElementType X, List *pL){

Position P=locate(X,*pL);

if (P->Next!=NULL)

deleteList(P, pL);

else

printf("Not found %d\n",X);

38

void readList(List *pL){

makenullList(&(*pL));

int i, n, x;

scanf("%d", &n);

i=1;

while(i<=n){

scanf("%d", &x);
Position p=(*pL);

while(p->Next!=NULL){

p=p->Next;

Position t=(struct Node*)malloc(sizeof(struct Node));

t->Element=x;

t->Next=p->Next;

p->Next=t;

i++;

39

void append(ElementType X, List *pL){

Position P=(*pL);

while (P->Next!=NULL)

P=P->Next;

struct Node* t=(struct Node*)malloc(sizeof(struct Node));

t->Element=X;

t->Next=P->Next;

P->Next=t;

40

List unionSet(List L1, List L2){

List L;

makenullList(&L);

Position P=L1;

while (P->Next!=NULL){

append(P->Next->Element,&L);

P=P->Next;

P=L2;

while (P->Next!=NULL){

if (!member(P->Next->Element,L))
append(P->Next->Element,&L);

P=P->Next;

return L;

41

void copyEvenNumbers(List L1, List *pL){

makenullList(pL);

Position P=L1;

while (P->Next!=NULL){

if(P->Next->Element%2==0)

append(P->Next->Element,pL);

P=P->Next;

42

List intersection(List L1, List L2){

List L;

makenullList(&L);

Position P=L1;

while (P->Next!=NULL){

if (member(P->Next->Element,L2))

append(P->Next->Element,&L);

P=P->Next;

return L;

43

int member(ElementType X, List L){

int Found=0;

Position P=L;

while (P->Next!=NULL && !Found){

if (P->Next->Element==X)
Found=1;

else

P=P->Next;

return Found;

44

void erase(ElementType X, List *pL){

Position P=locate(X,*pL);

if (P->Next!=NULL)

deleteList(P, pL);

else

printf("Not found %d\n",X);

45

void makenullList(List *pL){

(*pL)=(struct Node*)malloc(sizeof(struct Node));

(*pL)->Next=NULL;

46

void sort(List *pL){

Position P,Q;

ElementType temp;

P=(*pL);

while (P->Next!=NULL){

Q=P->Next;

while(Q->Next!=NULL){

if (P->Next->Element>Q->Next->Element){

temp=P->Next->Element;

P->Next->Element=Q->Next->Element;

Q->Next->Element=temp;

Q=Q->Next;
}

P=P->Next;

47

void insertList(ElementType X, Position P, List *pL){

struct Node* t=(struct Node*)malloc(sizeof(struct Node));

//Position t=(struct Node*)malloc(sizeof(struct Node));

t->Element=X;

t->Next=P->Next;

P->Next=t;

48

#include<stdio.h>

#include<stdlib.h>

typedef int ElementType;

struct Node{

ElementType Element;

struct Node* Next;

};

typedef struct Node* Position;

typedef Position List;

//ham

void makenullList(List *pL);

int member(ElementType x, List L);

void addFirst(ElementType x, List *pL);

List readSet();

void printList(List L);

void append(ElementType x, List *pL);

List intersection(List L1, List L2);

//trien khai ham


void makenullList(List *pL){

(*pL)=(struct Node*)malloc(sizeof(struct Node));

(*pL)->Next=NULL;

int member(ElementType x, List L){

Position P=L;

while(P->Next!=NULL){

if(P->Next->Element==x){

return 1;

break;

else{

P=P->Next;

return 0;

void addFirst(ElementType x, List *pL){

Position p = (*pL);

Position t = (struct Node*)malloc(sizeof(struct Node));

t->Element = x;

t->Next = p->Next;

p->Next = t;

List readSet(){

List L;

makenullList(&L);

int i, n, x;

scanf("%d", &n);

for(i=1; i<=n; i++){

scanf("%d", &x);

if(member(x,L)==0){

addFirst(x,&L);
}

return L;

void printList(List L){

Position p=L;

while(p->Next!=NULL){

printf("%d ", p->Next->Element);

p=p->Next;

void append(ElementType x, List *pL){

Position p=(*pL);

while(p->Next!=NULL){

p=p->Next;

Position t = (struct Node*)malloc(sizeof(struct Node));

t->Element = x;

t->Next = p->Next;

p->Next = t;

List intersection(List L1, List L2){

List L;

makenullList(&L);

Position p;

p=L1;

while(p->Next!=NULL){

if(member(p->Next->Element,L2)!=0){

append(p->Next->Element,&L);

p=p->Next;

return L;
}

int main(){

List L1, L2, L;

makenullList(&L1);

makenullList(&L2);

L1 = readSet();

L2 = readSet();

printList(L1);

printf("\n");

printList(L2);

printf("\n");

L = intersection(L1,L2);

printList(L);

return 0;

Tập hợp giao

49

#include<stdio.h>

#include<stdlib.h>

typedef int ElementType;

struct Node{

ElementType Element;

struct Node* Next;

};

typedef struct Node* Position;

typedef Position List;

//ham

void makenullList(List *pL);

void append(ElementType x, List *pL);

void readList(List *pL);

void printList(List L);

float getAvg(List L);


//trien khai ham

void makenullList(List *pL){

(*pL)=(struct Node*)malloc(sizeof(struct Node));

(*pL)->Next=NULL;

void append(ElementType x, List *pL){

Position p=(*pL);

while(p->Next!=NULL){

p=p->Next;

Position t = (struct Node*)malloc(sizeof(struct Node));

t->Element = x;

t->Next = p->Next;

p->Next = t;

void readList(List *pL){

makenullList(&(*pL));

int i, n, x;

scanf("%d", &n);

i=1;

while(i<=n){

scanf("%d", &x);

Position p=(*pL);

while(p->Next!=NULL){

p=p->Next;

Position t=(struct Node*)malloc(sizeof(struct Node));

t->Element=x;

t->Next=p->Next;

p->Next=t;

i++;

}
}

void printList(List L){

Position p=L;

while(p->Next!=NULL){

printf("%d ", p->Next->Element);

p=p->Next;

float getAvg(List L){

float S=0, i=0;

Position p=(L);

while(p->Next!=NULL){

S+=(p->Next->Element);

p=p->Next;

i++;

if(i!=0){

return (S/i);

else{

return -10000.0;

int main(){

List L1;

makenullList(&L1);

readList(&L1);

printList(L1);

printf("\n%.3f", getAvg(L1));

return 0;

Trung bình cộng

50
#include<stdlib.h>

typedef int ElementType;

struct Node{

ElementType Element;

struct Node* Next;

};

typedef struct Node* Position;

typedef Position List;

//ham

void makenullList(List *pL);

void append(ElementType x, List *pL);

void readList(List *pL);

void printList(List L);

void printOddNumbers(List L);

void copyEvenNumbers(List L1, List *pL2);

//trien khai ham

void makenullList(List *pL){

(*pL)=(struct Node*)malloc(sizeof(struct Node));

(*pL)->Next=NULL;

void append(ElementType x, List *pL){

Position p=(*pL);

while(p->Next!=NULL){

p=p->Next;

Position t = (struct Node*)malloc(sizeof(struct Node));

t->Element = x;

t->Next = p->Next;

p->Next = t;

void readList(List *pL){


makenullList(&(*pL));

int i, n, x;

scanf("%d", &n);

i=1;

while(i<=n){

scanf("%d", &x);

Position p=(*pL);

while(p->Next!=NULL){

p=p->Next;

Position t=(struct Node*)malloc(sizeof(struct Node));

t->Element=x;

t->Next=p->Next;

p->Next=t;

i++;

void printList(List L){

Position p=L;

while(p->Next!=NULL){

printf("%d ", p->Next->Element);

p=p->Next;

in

void copyEvenNumbers(List L1, List *pL2){

makenullList(&(*pL2));

Position p=L1;

while(p->Next!=NULL){

if(p->Next->Element%2==0){

append(p->Next->Element,&(*pL2));

p=p->Next;
}

int main(){

List L1, L2;

makenullList(&L1);

makenullList(&L2);

readList(&L1);

printList(L1);

printf("\n");

printOddNumbers(L1);

copyEvenNumbers(L1,&L2);

printf("\n");

printList(L2);

return 0;

Số chẵn, lẻ

51

#include<stdio.h>

#include<stdlib.h>

typedef int ElementType;

struct Node{

ElementType Element;

struct Node* Next;

};

typedef struct Node* Position;

typedef Position List;

//ham

void makenullList(List *pL);

void append(ElementType x, List *pL);

void readList(List *pL);

void printList(List L);


//trien khai ham

void makenullList(List *pL){

(*pL)=(struct Node*)malloc(sizeof(struct Node));

(*pL)->Next=NULL;

void append(ElementType x, List *pL){

Position p=(*pL);

while(p->Next!=NULL){

p=p->Next;

Position t = (struct Node*)malloc(sizeof(struct Node));

t->Element = x;

t->Next = p->Next;

p->Next = t;

void readList(List *pL){

makenullList(&(*pL));

int i, n, x;

scanf("%d", &n);

i=1;

while(i<=n){

scanf("%d", &x);

Position p=(*pL);

while(p->Next!=NULL){

p=p->Next;

Position t=(struct Node*)malloc(sizeof(struct Node));

t->Element=x;

t->Next=p->Next;

p->Next=t;

i++;

}
void printList(List L){

Position p=L;

while(p->Next!=NULL){

printf("%d ", p->Next->Element);

p=p->Next;

int main(){

List L1;

makenullList(&L1);

readList(&L1);

printList(L1);

return 0;

Nhập, hiển thị

52

List getList(){

List L;

L=(struct Node*)malloc(sizeof(struct Node));

L->Next=NULL;

return L;

53

int append(Student s, List *pL){

struct Node *P, *temp;

P=locate(s.ID,(*pL));

if (P->Next!=NULL){

return 0;

else{

temp=(struct Node*)malloc(sizeof(struct Node));

temp->Element=s;
temp->Next=NULL;

P->Next=temp;

return 1;

54 viết khai báo

typedef struct

char ID[10];

char Name[50];

float R1, R2, R3;

}Student;

struct Node

Student Element;

struct Node* Next;

};

typedef struct Node* List;

55
Hãy HOÀN CHỈNH CHƯƠNG TRÌNH TRÊN bằng cách điền các lệnh cần thiết vào dấu ... (trong hàm
readList()) để tạo thành 1 chương trình có thể thực thi được.

struct Node *p=L;

while(p->Next!=NULL){

if(locate(s.ID, L) == p){

printf("%s exists\n", s.ID);

p=p->Next;

append(s,&L);

56

void inDaThuc(DaThuc d){


Position p=d;

while(p->Next!=NULL){

if (p->Next->Next==NULL)

printf("%.3fX^%d",p->Next->e.he_so,p->Next->e.bac);

else

printf("%.3fX^%d + ",p->Next->e.he_so,p->Next->e.bac);

p=p->Next;

printf("\n");

57

DaThuc tinhDaoHam(DaThuc D){

DaThuc dt;

DonThuc e;

dt=khoitao();

Position p=D;

while(p->Next!=NULL){

e=p->Next->e;

e.he_so *= e.bac;

e.bac--;

chenDonThuc(e,&dt);

p=p->Next;

return dt;

58

DaThuc congDaThuc(DaThuc D1, DaThuc D2){

DaThuc d;

d=khoitao();

Position p=D1;

while (p->Next!=NULL){

chenDonThuc(p->Next->e,&d);

p=p->Next;
}

p=D2;

while (p->Next!=NULL){

chenDonThuc(p->Next->e,&d);

p=p->Next;

return d;

You might also like