Professional Documents
Culture Documents
#include <stdio.h>
#include <conio.h>
struct Node{
int data;
Node *Next;
};
struct DSLK{
Node *pHead;
Node *pTail;
};
void KhoiTao(DSLK &l){
l.pHead = l.pTail = NULL;
}
int KiemTraRong (DSLK &l){
if (l.pHead==NULL && l.pTail==NULL)
return 1;
return 0;
}
Node* Get_Node(int x){
Node *p ;
p = new Node;
if (p==NULL){
printf("Khong du bo nho!");
return NULL;
}
printf("Nut duoc tao tai dia chi:%d\n", p);
p->data = x;
p->Next = NULL;
return p;
}
void ChenCuoi(DSLK &l, Node *p){
if (l.pHead==NULL){
l.pHead = l.pTail = p;
}
else{
l.pTail->Next = p;
l.pTail = p;
}
}
void DuyetDS(DSLK l){
Node *p;
if (KiemTraRong(l)==1)
printf("Danh sach da rong!");
p = l.pHead;
while (p!=NULL){
printf("%d ",p->data);
p = p->Next;
}
printf("\n");
}
void XoaDau(DSLK &l){
Node *p;
p = l.pHead;
l.pHead = l.pHead->Next;
p->Next = NULL;
delete p;
if (l.pHead == NULL)
l.pTail = NULL;
}
void HuyDS(DSLK &l){
while (KiemTraRong(l)==0)
XoaDau(l);
}
int main(){
int n, x, k;
DSLK l;
KhoiTao(l);
printf("So phan tu trong danh sach: ");
scanf("%d",&n);
for (int i=0; i<n; i++){
printf("Nhap gia tri: "); scanf("%d",&x);
Node *p = Get_Node(x);
ChenCuoi (l, p);
}
printf("Danh sach da tao:\n");
DuyetDS(l);
XoaDau(l);
printf("Sau khi xoa:\n");
DuyetDS(l);
HuyDS(l);
getch();
}
Cu 16:
#include <stdio.h>
#include <conio.h>
struct Node{
int data;
Node *Next;
};
struct DSLK{
Node *pHead;
Node *pTail;
};
void KhoiTao(DSLK &l){
l.pHead = l.pTail = NULL;
}
int KiemTraRong (DSLK &l){
if (l.pHead==NULL && l.pTail==NULL)
return 1;
return 0;
}
Node* Get_Node(int x){
Node *p ;
//Cap phat bo nho
p = new Node;
if (p==NULL){
printf("Khong du bo nho!");
return NULL;
}
printf("Nut duoc tao tai dia chi:%d\n", p);
p->data = x;
p->Next = NULL;
return p;
}
void ChenDau(DSLK &l, Node *p){
//kiem tra truong hop danh sach rong
if (l.pHead==NULL){
l.pHead = l.pTail = p;
}
else{
p->Next = l.pHead;
l.pHead = p;
}
}
void DuyetDS(DSLK l){
Node *p;
if (KiemTraRong(l)==1)
printf("Danh sach da rong!");
p = l.pHead;
while (p!=NULL){
printf("%d ",p->data);
p = p->Next;
}
printf("\n");
}
Node* TimKiem(DSLK l, int x){
Node *p;
p = l.pHead;
while (p!=NULL){
if (p->data !=x){
p = p->Next;
DuyetDS (l);
}
else
return p;
}
return NULL;
}
void XoaPhanTu(DSLK &l, int k){
//Tim nut
Node*p, *q;
p = l.pHead;
q = NULL;
while (p!=NULL && p->data!=k){
q = p;
p = p->Next;
}
if (p==NULL){
printf("Khong co phan tu can xoa!");
}
else if (p==l.pHead){
l.pHead = l.pHead->Next;
p->Next = NULL;
delete p;
if (l.pHead==NULL)
l.pTail = NULL;
}
else{
q->Next = p->Next;
if (p==l.pTail)
l.pTail = q;
p->Next = NULL;
delete p;
}
}
int main(){
int n, x, k, h;
DSLK l;
KhoiTao(l);
printf("So phan tu trong danh sach: ");
scanf("%d",&n);
for (int i=0; i<n; i++){
printf("Nhap gia tri: "); scanf("%d",&x);
Node *p = Get_Node(x);
ChenDau (l, p);
}
printf("Danh sach da tao:\n");
DuyetDS(l);
printf ("\n nhap phan tu bat ky: ");
scanf ("%d", &h);
if (TimKiem (l,h)!=NULL){
printf ("\ co phan tu trong danh sach.");
XoaPhanTu (l,h);
printf ("\n danh sach sau xoa la: ");
DuyetDS(l);
}
else
}
else{
l.pTail->Next = p;
l.pTail = p;
}
}
void DuyetDS(DSLK l){
Node *p;
if (KiemTraRong(l)==1)
printf("Danh sach da rong!");
p = l.pHead;
while (p!=NULL){
printf("%d ",p->data);
p = p->Next;
}
printf("\n");
}
Node* TimKiem(DSLK l, int x){
Node *p;
p = l.pHead;
while (p!=NULL){
if (p->data !=x){
p = p->Next;
DuyetDS (l);
}
else
return p;
}
return NULL;
}
void XoaDau(DSLK &l){
Node *p;
p = l.pHead;
l.pHead = l.pHead->Next;
p->Next = NULL;
delete p;
if (l.pHead == NULL)
l.pTail = NULL;
}
void HuyDS(DSLK &l){
while (KiemTraRong(l)==0)
XoaDau(l);
}
int main(){
int n, x, k, h;
DSLK l;
KhoiTao(l);
printf("So phan tu trong danh sach: ");
scanf("%d",&n);
for (int i=0; i<n; i++){
printf("Nhap gia tri: "); scanf("%d",&x);
Node *p = Get_Node(x);
ChenDau (l, p);
}
printf("Danh sach da tao:\n");
DuyetDS(l);
printf ("\n nhap phan tu bat ky: ");
scanf ("%d", &h);
if (TimKiem (l,h)!=NULL)
printf ("\ co phan tu trong danh sach.");
else
printf ("\ khong co ");
HuyDS(l);
getch();
}
Cu 3,4,5,6:
#include "stdio.h"
#include "conio.h"
int main (){
int i, j, n, tg;
int A[100];
printf ("nhap so phan tu:");
scanf ("%d", &n);
for (int i=0; i<n; i++){
printf ("nhap phan tu thu %d cua day la:",i+1);
scanf ("%d", &A[i]);
}
for (i=0; i<n; i++){
for (j=i+1; j<n;j++){
if (A[i]>A[j]){
tg=A[i];
A[i]=A[j];
A[j]=tg;
}
}
}
printf ("\nso nguyen duong nho nhat la: %d", A[0]);
printf ("\nso nguyen duong lon nhat la: %d", A[n-1]);
for (i=0; i<n; i++){
if (A[i]%2 == 0)
break;
}
printf ("\n so chan nho nhat la: %d", A[i]);
for (i=n-1; i>0; i--){
if (A[i]%2 != 0)
break;
}
printf ("\n so le lon nhat la: %d",A[i]);
}
Cu 23:
#include <stdio.h>
#include <conio.h>
#define N 100
//dinh nghia cau truc stack
struct Stack{
int top;
char A[N];
};
void KhoiTao(Stack &s){
s.top = -1;
}
int KiemTraRong(Stack s){
if (s.top==-1)
return 1;
return 0;
}
int KiemTraDay(Stack s){
if (s.top==N-1)
return 1;
return 0;
}
void Push(Stack &s, char x){
if (KiemTraDay(s)==1){
printf("Stack da day!");
return;
}
s.top += 1;
s.A[s.top] = x;
}
int Pop(Stack &s){
char x;
if (KiemTraRong(s)==0){
x = s.A[s.top];
s.top -= 1;
}
return x;
}
int main(){
int n;
char x;
Stack s;
KhoiTao(s);
printf("Nhap n: "); scanf("%d",&n);
for (int i=0; i<n; i++){
fflush(stdin);
printf("Nhap gia tri: ");
scanf("%c",&x);
Push(s,x);
}
printf("\nXau dao nguoc la: ");
for (int i=0; i<n; i++){
x=Pop(s);
printf("%c",x);
}
}
Cu 22:
#include <stdio.h>
#include <conio.h>
#define N 100
struct Queue{
float A[N];
int front, rear;
};