Diễn Đàn » Hướng Dẫn Lập trình

Bài tập C - Cấu trúc dữ liệu

60 trả lời, 28.088 lượt xem — Trang 2 / 3
/* Bai tap 3_41 - Bai toan Josephus */
#include <stdio.h>
#include <conio.h>

#define MIN 5
#define MAX 26
int y;

int Josephus(int tongso, int start, int sodem)
{
int ds[MAX], i, oldtongso;

oldtongso = tongso;

for (i=0; i<tongso; i++)
ds = 1;
for (i=tongso; i<MAX; i++)
ds = 0;
do {
i = 1;
while (i<sodem)
{
i++;
start++;
if (start > oldtongso-1)
start = 0;
while (ds[start] == 0)
{
start++;
if (start > oldtongso-1)
start = 0;
}
}
ds[start++] = 0;
while(ds[start] == 0)
{
start++;
if (start > oldtongso-1)
start = 0;
}
tongso--;
} while (tongso > 1);
for (i=0; i<MAX; i++)
if (ds == 1)
return i;
return 0;
}

void main()
{
int n, m, k, nguoichon;

printf("\nCho biet so luong binh si (%d -> %d) : ", MIN, MAX);
do {
scanf("%d", &m);
} while (m<MIN || m>MAX);
printf("\nCho biet so thu tu binh si duoc chon : ");
scanf("%d", &k);
printf("\nCho biet gia tri dem : ");
scanf("%d", &n);
printf("\nCac binh si gom : \n");
for (nguoichon = 0; nguoichon < m; nguoichon++)
printf("%c ", nguoichon+'A');
y = wherey();
nguoichon = Josephus(m, n, k);
printf("\nNguoi duoc chon la binh si %c", nguoichon+'A');
getch();
}
Love mickey,
Đăng nhập để trả lời
0
/* Bai tap 3_42 - Bai toan Josephus nguoc */
#include <stdio.h>
#include <conio.h>

#define MIN 5
#define MAX 26
int y;

int Josephus(int tongso, int sodem, int vitri)
{
int ds[MAX], i, oldtongso, start = 0;

oldtongso = tongso;

for (i=0; i<tongso; i++)
ds = 1;
for (i=tongso; i<MAX; i++)
ds = 0;
do {
i = 1;
while (i<sodem)
{
i++;
start++;
if (start > oldtongso-1)
start = 0;
while (ds[start] == 0)
{
start++;
if (start > oldtongso-1)
start = 0;
}
}
ds[start++] = 0;
while(ds[start] == 0)
{
start++;
if (start > oldtongso-1)
start = 0;
}
tongso--;
} while (tongso > 1);
for (i=0; i<MAX; i++)
if (ds == 1)
break;
if (vitri >= i)
start = vitri - i;
else
start = oldtongso - i + vitri;
return start;
}

void main()
{
int n, m, k, nguoichon;

printf("\nCho biet so luong binh si (%d -> %d) : ", MIN, MAX);
do {
scanf("%d", &m);
} while (m<MIN || m>MAX);
printf("\nCho biet vi tri binh si muon di : ");
scanf("%d", &k);
printf("\nCho biet gia tri dem : ");
scanf("%d", &n);
printf("\nCac binh si gom : \n");
for (nguoichon = 0; nguoichon < m; nguoichon++)
printf("%c ", nguoichon+'A');
y = wherey();
nguoichon = Josephus(m, n, k);
printf("\nNguoi phai duoc dem dau tien la binh si %c", nguoichon+'A');
getch();
}
Love mickey,
Đăng nhập để trả lời
0
/* Bai tap 3_2 - Minh hoa giai thuat Knuth - Morris - Pratt */
#include <stdio.h>
#include <conio.h>
#include <string.h>

#define MAX 256

int KMP(char *source, char *find);

void main()
{
char source[MAX], find[MAX];
int f;

printf("\nNhap vao chuoi nguon : ");
gets(source);
printf("Nhap vao chuoi tim kiem : ");
gets(find);

if ((f = KMP(source, find)) >= 0)
printf("chuoi tim thay tai chi so %d", f);
getch();
}

int KMP(char *source, char *find)
{
int next[MAX], i = 0, len, j=-1, lensource;

len = strlen(find);
lensource = strlen(source);
next[0] = -1;
do {
if (j == -1 || find == find[j])
{
i++;
j++;
next = j;
}
else
j = next[j];
} while (i < len-1);
i = j = 0;
do {
if (j==0 || source == find[j])
{
i++;
j++;
}
else
j = next[j];
} while (j<len && i<lensource);
if (j>=len)
return i-len;
else
return -1;
}
Love mickey,
Đăng nhập để trả lời
0
/* Quan ly SV bang DSLK */
#include <stdio.h>
#include <ctype.h>
#include <mem.h>
#include <string.h>

#define MAX 100
#define TOAN 0
#define LY 1
#define HOA 2

struct sinhvien {
char mslop[5];
char hoten[35];
float diem[3];
struct sinhvien *next;
} *first, *last;

int n = 0;

void nhapmoi()
{
char mslop[5], tmp[3];
int i;
float diem[3];
struct sinhvien *svtmp;

do {
printf("\nCho biet ma so lop : ");
gets(mslop);
if (strlen(mslop))
{
svtmp = (struct sinhvien *) malloc(sizeof(struct sinhvien));
strcpy(svtmp->mslop, mslop);
printf("\nCho biet ho ten : ");
gets(svtmp->hoten);
printf("\nCho biet diem so : ");
for (i=0; i<3; i++)
{
scanf("%f", &diem);
svtmp->diem = diem;
}
gets(tmp);
n++;
svtmp->next = NULL;
if (first == NULL)
first = last = svtmp;
else
{
last->next = svtmp;
last = last->next;
}
}
} while (strlen(mslop));
}

void timkiem()
{
char mslop[5];
int found = 0;
struct sinhvien *svtmp;

if (first == NULL)
{
printf("\nDanh sach rong!!!");
return;
}
svtmp = first;
printf("\nCho biet ma so lop : ");
gets(mslop);
if (strlen(mslop))
while (svtmp != NULL)
if (stricmp(svtmp->mslop, mslop) == 0)
{
printf("\nMa so lop : %s", svtmp->mslop);
printf("\nHo va ten : %s", svtmp->hoten);
printf("\nDiem Toan : %f", svtmp->diem[TOAN]);
printf("\nDiem Ly : %f", svtmp->diem[LY]);
printf("\nDiem Hoa : %f", svtmp->diem[HOA]);
found = 1;
break;
}
else
svtmp = svtmp->next;
if (!found)
printf("\nKhong tim thay!!!");
}

void xoa()
{
char mslop[5], traloi;
int i = 0, j;
struct sinhvien *svtmp, *before = NULL;

if (first == NULL)
{
printf("\nDanh sach rong!!!");
return;
}
svtmp = first;
printf("\nCho biet ma so lop : ");
gets(mslop);
if (strlen(mslop))
while (i<n)
if (stricmp(svtmp->mslop, mslop) == 0)
{
printf("\nMa so lop : %s", svtmp->mslop);
printf("\nHo va ten : %s", svtmp->hoten);
printf("\nDiem Toan : %f", svtmp->diem[TOAN]);
printf("\nDiem Ly : %f", svtmp->diem[LY]);
printf("\nDiem Hoa : %f", svtmp->diem[HOA]);
printf("\nCo muon xoa khong (C/K)? ");
do {
traloi = toupper(getch());
} while (traloi != 'C' && traloi != 'K');
putc(traloi, stdout);
if (traloi == 'C')
{
n--;
if (before == NULL)
{
before = first;
first = first->next;
}
else
before->next = svtmp->next;
free(svtmp);
break;
}
}
else
{
before = svtmp;
svtmp = svtmp->next;
}
}

void menu()
{
printf("\n***************");
printf("\n* 1. Them *");
printf("\n* 2. Xoa *");
printf("\n* 3. Tim kiem *");
printf("\n* 0. Thoat *");
printf("\n***************");
printf("\nChon lua ? ");
}

void main()
{
char traloi;
first = last = NULL;
do {
menu();
do {
traloi = getch();
} while (traloi < '0' || traloi > '3');
putc(traloi, stdout);
switch (traloi)
{
case '1' : nhapmoi();
break;
case '2' : xoa();
break;
case '3' : timkiem();
break;
}
} while (traloi != '0');
}
Love mickey,
Đăng nhập để trả lời
0
/* Bai tap 3_61 - Quan ly QUEUE bang danh sach lien ket */
#include <stdio.h>
#include <conio.h>
#include <alloc.h>
#include <stdlib.h>

typedef struct tagqueue {
int value;
struct tagqueue *next;
} QUEUE;

QUEUE *first;

int isempty()
{
return (first->next == NULL);
}

void push(int value)
{
QUEUE *f;

f = (QUEUE *)malloc(sizeof(QUEUE));
f->value = value;
f->next = first->next;
first->next = f;
}

int pop()
{
QUEUE *f;
int value;
if (!isempty())
{
f = first->next;
first->next = first->next->next;
value = f->value;
}
else
value = -1;
return value;
}

void initqueue()
{
first = (QUEUE *)malloc(sizeof(QUEUE));
first->next = NULL;
}

void release(QUEUE *f)
{
if (f->next != NULL)
release(f->next);
free(f);
}

void cleanup()
{
release(first);
}

void main()
{
int i, n;
clrscr();
initqueue();
randomize();
for (i=0; i<20; i++)
{
n = random(20);
printf("\nPUSH %d vao queue", n);
push(n);
}
printf("\nLay cac phan tu tu QUEUE : ");
while (!isempty())
printf("%3d", pop());
getch();
}
Love mickey,
Đăng nhập để trả lời
0
#include <stdio.h>

typedef struct node *node_ptr;
typedef int element_type;

struct node {
element_type element;
node_ptr next;
};

typedef node_ptr STACK;
STACK S;

STACK khoi_tao_stack()
{
STACK S;
S = (STACK) malloc(sizeof(struct node));
if (S == NULL)
{
printf("\nKhong du bo nho!!!");
exit(1);
}
else
S->next = NULL;
return S;
}

int is_empty(STACK S)
{
return (S->next == NULL);
}

node_ptr push(element_type e, STACK S)
{
node_ptr tmp_cell;

tmp_cell = (node_ptr) malloc(sizeof(struct node));
if (tmp_cell != NULL)
{
tmp_cell->element = e;
tmp_cell->next = S->next;
S->next = tmp_cell;
}
return tmp_cell;
}

node_ptr pop(STACK S)
{
node_ptr cell;

if (is_empty(S))
return NULL;
else
{
cell = S->next;
S->next = S->next->next;
return cell;
}
}

void main()
{
element_type k;
node_ptr cell;

S = khoi_tao_stack();
printf("\nNhap cac phan tu vao stack (-1 de ket thuc) : ");
do {
scanf("%d", &k);
if (k != -1)
push(k, S);
} while (k != -1);
printf("\n\nLay cac phan tu tu stack ra : ");
while (!is_empty(S))
if ((cell = pop(S)) != NULL)
printf("%d ", cell->element);
getch();
}
Love mickey,
Đăng nhập để trả lời
0
/* Bai tap 3_84 - Khao sat tinh lien thong cua mot do thi */
#include <dos.h>
#include <graphics.h>
#include <alloc.h>
#include "mouse.inc"

#pragma warn -sus

/* Toi da 100 nut */
#define MAX 100

int gr_drive=DETECT, gr_mode;
unsigned char lbutton, rbutton;
int xmouse, ymouse;

int sonut = 0;
typedef struct tagnode {
int x, y; /* Vi tri tren man hinh */
} NODE;

NODE nut[MAX];
int weight[MAX][MAX];
int themduoc = 1;

void DFS();

void initialize()
{
char s1[] = "Nhap nut phai chuot de them nut";
char s2[] = "Nhap nut trai chuot va re de them duong noi";
char s3[] = "Nhan phim Q de thoat - S de bat dau minh hoa giai thuat";
int i, j;

initgraph(&gr_drive, &gr_mode, "");
reset_mouse();
setcolor(YELLOW);
rectangle(0, 0, getmaxx(), getmaxy());
outtextxy((getmaxx()-textwidth(s1))/2, 5, s1);
outtextxy((getmaxx()-textwidth(s2))/2, 15, s2);
outtextxy((getmaxx()-textwidth(s3))/2, 25, s3);
line(0, 35, getmaxx(), 35);
set_mouse_hlimits(5, getmaxx()-6);
set_mouse_vlimits(40, getmaxy()-6);
for (i=0; i<MAX; i++)
for (j=0; j<MAX; j++)
weight[j] = -1;
show_mouse();
}

int index(int x, int y, int heso)
{
int i, OK = 0;
for (i=0; i<sonut; i++)
if (abs(nut.x - x) < 4*heso && abs(nut.y - y) < 4*heso)
{
OK = 1;
break;
}
if (OK)
return i;
else
return -1;
}

void get_mouse()
{
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
} while (lbutton == 0 && rbutton == 0 && !kbhit());
}

void clear_mouse()
{
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
} while (lbutton == 1 || rbutton == 1);
}

void get_weight()
{
int x, y, oldx, oldy, i, OK = 0, start, end;
char s[5];
setwritemode(XOR_PUT);
setcolor(GREEN);
for (i=0; i<sonut; i++)
if (abs(nut.x - xmouse) < 4 && abs(nut.y - ymouse) < 4)
{
start = i;
oldx = x = nut.x;
oldy = y = nut.y;
OK = 1;
break;
}
if (!OK)
return;
hide_mouse();
line(x, y, oldx, oldy);
clear_mouse();
show_mouse();
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
if (oldx != xmouse || oldy != ymouse)
{
hide_mouse();
line(x, y, oldx, oldy);
oldx = xmouse;
oldy = ymouse;
line(x, y, oldx, oldy);
show_mouse();
}
} while (lbutton == 0);
OK = 0;
hide_mouse();
line(x, y, oldx, oldy);
for (i=0; i<sonut; i++)
if (abs(nut.x - xmouse) < 4 && abs(nut.y - ymouse) < 4)
{
end = i;
OK = 1;
break;
}
if (OK && end != start)
{
weight[start][end] = 100;
weight[end][start] = 100;
setcolor(GREEN);
line(x, y, nut[end].x, nut[end].y);
}
setwritemode(COPY_PUT);
show_mouse();
}

void main()
{
int done = 0;
char c, s[4];

initialize();
do {
get_mouse();
if (kbhit())
{
c = toupper(getch());
switch(c)
{
case 'Q' : done = 1; break;
case 'S' : DFS(); break;
}
}
if (rbutton == 1 && index(xmouse, ymouse, 10) == -1)
{
hide_mouse();
if (themduoc)
{
setcolor(WHITE);
circle(xmouse, ymouse, 4);
itoa(sonut, s, 10);
setcolor(CYAN);
outtextxy(xmouse+6, ymouse-6, s);
nut[sonut].x = xmouse;
nut[sonut].y = ymouse;
sonut++;
if (sonut>=MAX)
themduoc = 0;
}
clear_mouse();
show_mouse();
}
if (lbutton == 1)
{
get_weight();
clear_mouse();
}
} while (!done);
closegraph();
}

void out(char *s)
{
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
bar(1, 36, getmaxx()-1, 46);
setcolor(YELLOW);
outtextxy(4, 38, s);
}

int visited[MAX];
int top, stack[MAX];
int visited[MAX];

void push(int value)
{
stack[++top] = value;
visited[value] = 1;
}

void pop(int *value)
{
*value = stack[top--];
}

int get()
{
if (!isempty())
return stack[top];
else
return -1;
}

int isempty(void)
{
return (top == -1);
}


void visit(int nutxet)
{
int i, OK;
char s[3];

push(nutxet);
visited[nutxet] = 1;
for (i=0; i<sonut; i++)
if (weight[nutxet] != -1 && visited == 0)
visit(i);
pop(&nutxet);
}

void DFS()
{
int size, i, OK = 0;
void far *buf;
char *s[] = {"Do thi lien thong", "Do thi khong lien thong."};

size = imagesize(1, 36, getmaxx()-1, 46);
buf = malloc(size);
hide_mouse();
getimage(1, 36, getmaxx()-1, 46, buf);
/* Bat dau giai thuat */
for (i=0; i<sonut; i++)
visited = 0;
visit(0);
for (i=0; i<sonut; i++)
/* Neu co toi thieu mot nut khong the tham duoc thi do thi khong lien thong */
if (visited == 0)
{
OK = 1;
break;
}
/* Ket thuc giai thuat */
out(s[OK]);
getch();
putimage(1, 36, buf, COPY_PUT);
free(buf);
clear_mouse();
show_mouse();
}
Love mickey,
Đăng nhập để trả lời
0
/* Bai tap 3_80 - Tim cay bao trum toi dai Maximum Spanning Tree */
#include <dos.h>
#include <graphics.h>
#include <alloc.h>
#include "mouse.inc"

#pragma warn -sus

/* Toi da 100 nut */
#define MAX 100

int gr_drive=DETECT, gr_mode;
unsigned char lbutton, rbutton;
int xmouse, ymouse;

int sonut = 0;
typedef struct tagnode {
int x, y; /* Vi tri tren man hinh */
} NODE;

NODE nut[MAX];
int weight[MAX][MAX];
int themduoc = 1;

void MaxST();

void initialize()
{
char s1[] = "Nhap nut phai chuot de them nut";
char s2[] = "Nhap nut trai chuot va re de them duong noi";
char s3[] = "Nhan phim Q de thoat - S de bat dau tim MST";
int i, j;

initgraph(&gr_drive, &gr_mode, "");
reset_mouse();
setcolor(YELLOW);
rectangle(0, 0, getmaxx(), getmaxy());
outtextxy((getmaxx()-textwidth(s1))/2, 5, s1);
outtextxy((getmaxx()-textwidth(s2))/2, 15, s2);
outtextxy((getmaxx()-textwidth(s3))/2, 25, s3);
line(0, 35, getmaxx(), 35);
set_mouse_hlimits(5, getmaxx()-6);
set_mouse_vlimits(40, getmaxy()-6);
for (i=0; i<MAX; i++)
for (j=0; j<MAX; j++)
weight[j] = -1;
show_mouse();
}

int index(int x, int y, int heso)
{
int i, OK = 0;
for (i=0; i<sonut; i++)
if (abs(nut.x - x) < 4*heso && abs(nut.y - y) < 4*heso)
{
OK = 1;
break;
}
if (OK)
return i;
else
return -1;
}

void get_mouse()
{
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
} while (lbutton == 0 && rbutton == 0 && !kbhit());
}

void clear_mouse()
{
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
} while (lbutton == 1 || rbutton == 1);
}

int input_weight(int start, int end)
{
int size, i;
void far *buf;
char c, s[]="Nhap trong so", s1[3]="";
size = imagesize(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10);
buf = malloc(size);
getimage(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10, buf);
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
bar(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10);
setcolor(WHITE);
rectangle(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10);
line(getmaxx()/2 - 70, getmaxy()/2 - 5,getmaxx()/2 + 70, getmaxy()/2 - 5);
outtextxy((getmaxx()-textwidth(s))/2 - 4, getmaxy()/2 - 16, s);
i = 0;
do {
do {
c = getch();
} while ((c < '0' || c > '9') && c != 13 && c != 27 && c != 8);
if (c>='0' && c <= '9' && i<2)
{
s1 = c;
s1[i+1] = 0;
i++;
}
if (c == 8 && i>0)
{
i--;
s1 = 0;
}
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
bar(getmaxx()/2 - 69, getmaxy()/2 - 3,getmaxx()/2 + 69, getmaxy()/2 + 9);
setcolor(YELLOW);
outtextxy((getmaxx()-textwidth(s))/2 - 4, getmaxy()/2 - 1, s1);
} while(c != 13 && c != 27);
putimage(getmaxx()/2 - 70, getmaxy()/2 - 20, buf, COPY_PUT);
free(buf);
if (c == 13)
{
i = atoi(s1);
weight[start][end] = i;
weight[end][start] = i;
return i;
}
else
return -1;
}

void get_weight()
{
int x, y, oldx, oldy, i, OK = 0, start, end;
char s[5];
setwritemode(XOR_PUT);
setcolor(GREEN);
for (i=0; i<sonut; i++)
if (abs(nut.x - xmouse) < 4 && abs(nut.y - ymouse) < 4)
{
start = i;
oldx = x = nut.x;
oldy = y = nut.y;
OK = 1;
break;
}
if (!OK)
return;
hide_mouse();
line(x, y, oldx, oldy);
clear_mouse();
show_mouse();
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
if (oldx != xmouse || oldy != ymouse)
{
hide_mouse();
line(x, y, oldx, oldy);
oldx = xmouse;
oldy = ymouse;
line(x, y, oldx, oldy);
show_mouse();
}
} while (lbutton == 0);
OK = 0;
hide_mouse();
line(x, y, oldx, oldy);
for (i=0; i<sonut; i++)
if (abs(nut.x - xmouse) < 4 && abs(nut.y - ymouse) < 4)
{
end = i;
OK = 1;
break;
}
if (OK && end != start)
{
if ((i = input_weight(start, end)) != -1)
{
setcolor(GREEN);
line(x, y, nut[end].x, nut[end].y);
itoa(i, s, 10);
setcolor(RED);
outtextxy(x + (nut[end].x - x) / 2 + 4, y + (nut[end].y - y) / 2 + 4, s);
}
}
setwritemode(COPY_PUT);
show_mouse();
}

void main()
{
int done = 0;
char c, s[4];

initialize();
do {
get_mouse();
if (kbhit())
{
c = toupper(getch());
switch(c)
{
case 'Q' : done = 1; break;
case 'S' : MaxST(); break;
}
}
if (rbutton == 1 && index(xmouse, ymouse, 10) == -1)
{
hide_mouse();
if (themduoc)
{
setcolor(WHITE);
circle(xmouse, ymouse, 4);
itoa(sonut, s, 10);
setcolor(CYAN);
outtextxy(xmouse+6, ymouse-6, s);
nut[sonut].x = xmouse;
nut[sonut].y = ymouse;
sonut++;
if (sonut>=MAX)
themduoc = 0;
}
clear_mouse();
show_mouse();
}
if (lbutton == 1)
{
get_weight();
clear_mouse();
}
} while (!done);
closegraph();
}

int visited[MAX];

void out(char *s)
{
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
bar(1, 36, getmaxx()-1, 46);
setcolor(YELLOW);
outtextxy(4, 38, s);
}

void MaxST()
{
int size, i, j, max, vert1 = 0, vert2, numnut = 0;
void far *buf;
char s[] = "Nhan phim bat ky de ket thuc";
hide_mouse();
size = imagesize(1, 36, getmaxx()-1, 46);
buf = malloc(size);
getimage(1, 36, getmaxx()-1, 46, buf);
/* Bat dau giai thuat */
for (i=0; i<sonut; i++)
visited = 0;
/* Bat dau tu dinh 0 */
setcolor(RED);
do {
max = 0;
visited[vert1] = 1; /* Danh dau no*/
numnut++;
/* Tim canh nho nhat di qua 1 dinh da tham
va 1 dinh chua tham */
for (i=0; i<sonut; i++)
for (j=0; j<sonut; j++)
if (visited==1 && weight[j] > max && weight[j]>-1 && visited[j]==0)
{
max = weight[j];
vert2 = i;
vert1 = j;
}
if (max > 0)
line(nut[vert1].x, nut[vert1].y, nut[vert2].x, nut[vert2].y);
delay(2000);
} while (numnut < sonut);
/* Ket thuc giai thuat */
show_mouse();
out(s);
getch();
putimage(1, 36, buf, COPY_PUT);
free(buf);
}
Love mickey,
Đăng nhập để trả lời
0
/* Bai tap 3_77 - Tim cay bao trum toi tieu MST - Minimal Spanning Tree */
#include <dos.h>
#include <graphics.h>
#include <alloc.h>
#include "mouse.inc"

#pragma warn -sus

/* Toi da 100 nut */
#define MAX 100

int gr_drive=DETECT, gr_mode;
unsigned char lbutton, rbutton;
int xmouse, ymouse;

int sonut = 0;
typedef struct tagnode {
int x, y; /* Vi tri tren man hinh */
} NODE;

NODE nut[MAX];
int weight[MAX][MAX];
int themduoc = 1;

void MST();

void initialize()
{
char s1[] = "Nhap nut phai chuot de them nut";
char s2[] = "Nhap nut trai chuot va re de them duong noi";
char s3[] = "Nhan phim Q de thoat - S de bat dau tim MST";
int i, j;

initgraph(&gr_drive, &gr_mode, "");
reset_mouse();
setcolor(YELLOW);
rectangle(0, 0, getmaxx(), getmaxy());
outtextxy((getmaxx()-textwidth(s1))/2, 5, s1);
outtextxy((getmaxx()-textwidth(s2))/2, 15, s2);
outtextxy((getmaxx()-textwidth(s3))/2, 25, s3);
line(0, 35, getmaxx(), 35);
set_mouse_hlimits(5, getmaxx()-6);
set_mouse_vlimits(40, getmaxy()-6);
for (i=0; i<MAX; i++)
for (j=0; j<MAX; j++)
weight[j] = -1;
show_mouse();
}

int index(int x, int y, int heso)
{
int i, OK = 0;
for (i=0; i<sonut; i++)
if (abs(nut.x - x) < 4*heso && abs(nut.y - y) < 4*heso)
{
OK = 1;
break;
}
if (OK)
return i;
else
return -1;
}

void get_mouse()
{
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
} while (lbutton == 0 && rbutton == 0 && !kbhit());
}

void clear_mouse()
{
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
} while (lbutton == 1 || rbutton == 1);
}

int input_weight(int start, int end)
{
int size, i;
void far *buf;
char c, s[]="Nhap trong so", s1[3]="";
size = imagesize(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10);
buf = malloc(size);
getimage(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10, buf);
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
bar(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10);
setcolor(WHITE);
rectangle(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10);
line(getmaxx()/2 - 70, getmaxy()/2 - 5,getmaxx()/2 + 70, getmaxy()/2 - 5);
outtextxy((getmaxx()-textwidth(s))/2 - 4, getmaxy()/2 - 16, s);
i = 0;
do {
do {
c = getch();
} while ((c < '0' || c > '9') && c != 13 && c != 27 && c != 8);
if (c>='0' && c <= '9' && i<2)
{
s1 = c;
s1[i+1] = 0;
i++;
}
if (c == 8 && i>0)
{
i--;
s1 = 0;
}
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
bar(getmaxx()/2 - 69, getmaxy()/2 - 3,getmaxx()/2 + 69, getmaxy()/2 + 9);
setcolor(YELLOW);
outtextxy((getmaxx()-textwidth(s))/2 - 4, getmaxy()/2 - 1, s1);
} while(c != 13 && c != 27);
putimage(getmaxx()/2 - 70, getmaxy()/2 - 20, buf, COPY_PUT);
free(buf);
if (c == 13)
{
i = atoi(s1);
weight[start][end] = i;
weight[end][start] = i;
return i;
}
else
return -1;
}

void get_weight()
{
int x, y, oldx, oldy, i, OK = 0, start, end;
char s[5];
setwritemode(XOR_PUT);
setcolor(GREEN);
for (i=0; i<sonut; i++)
if (abs(nut.x - xmouse) < 4 && abs(nut.y - ymouse) < 4)
{
start = i;
oldx = x = nut.x;
oldy = y = nut.y;
OK = 1;
break;
}
if (!OK)
return;
hide_mouse();
line(x, y, oldx, oldy);
clear_mouse();
show_mouse();
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
if (oldx != xmouse || oldy != ymouse)
{
hide_mouse();
line(x, y, oldx, oldy);
oldx = xmouse;
oldy = ymouse;
line(x, y, oldx, oldy);
show_mouse();
}
} while (lbutton == 0);
OK = 0;
hide_mouse();
line(x, y, oldx, oldy);
for (i=0; i<sonut; i++)
if (abs(nut.x - xmouse) < 4 && abs(nut.y - ymouse) < 4)
{
end = i;
OK = 1;
break;
}
if (OK && end != start)
{
if ((i = input_weight(start, end)) != -1)
{
setcolor(GREEN);
line(x, y, nut[end].x, nut[end].y);
itoa(i, s, 10);
setcolor(RED);
outtextxy(x + (nut[end].x - x) / 2 + 4, y + (nut[end].y - y) / 2 + 4, s);
}
}
setwritemode(COPY_PUT);
show_mouse();
}

void main()
{
int done = 0;
char c, s[4];

initialize();
do {
get_mouse();
if (kbhit())
{
c = toupper(getch());
switch(c)
{
case 'Q' : done = 1; break;
case 'S' : MST(); break;
}
}
if (rbutton == 1 && index(xmouse, ymouse, 10) == -1)
{
hide_mouse();
if (themduoc)
{
setcolor(WHITE);
circle(xmouse, ymouse, 4);
itoa(sonut, s, 10);
setcolor(CYAN);
outtextxy(xmouse+6, ymouse-6, s);
nut[sonut].x = xmouse;
nut[sonut].y = ymouse;
sonut++;
if (sonut>=MAX)
themduoc = 0;
}
clear_mouse();
show_mouse();
}
if (lbutton == 1)
{
get_weight();
clear_mouse();
}
} while (!done);
closegraph();
}

int visited[MAX];

void out(char *s)
{
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
bar(1, 36, getmaxx()-1, 46);
setcolor(YELLOW);
outtextxy(4, 38, s);
}

void MST()
{
int size, i, j, min, vert1 = 0, vert2, numnut = 0;
void far *buf;
char s[] = "Nhan phim bat ky de ket thuc";
hide_mouse();
size = imagesize(1, 36, getmaxx()-1, 46);
buf = malloc(size);
getimage(1, 36, getmaxx()-1, 46, buf);
/* Bat dau giai thuat */
for (i=0; i<sonut; i++)
visited = 0;
/* Bat dau tu dinh 0 */
setcolor(RED);
do {
min = 100;
visited[vert1] = 1; /* Danh dau no*/
numnut++;
/* Tim canh nho nhat di qua 1 dinh da tham
va 1 dinh chua tham */
for (i=0; i<sonut; i++)
for (j=0; j<sonut; j++)
if (visited==1 && weight[j] < min && weight[j]>-1 && visited[j]==0)
{
min = weight[j];
vert2 = i;
vert1 = j;
}
if (min < 100)
line(nut[vert1].x, nut[vert1].y, nut[vert2].x, nut[vert2].y);
delay(2000);
} while (numnut < sonut);
/* Ket thuc giai thuat */
show_mouse();
out(s);
getch();
putimage(1, 36, buf, COPY_PUT);
free(buf);
}
Love mickey,
Đăng nhập để trả lời
0
/* Bai tap 3_76 - Cai dat cau truc do thi bang ma tran ke */
#include <dos.h>
#include <graphics.h>
#include <alloc.h>
#include "mouse.inc"

#pragma warn -sus

/* Toi da 100 nut */
#define MAX 100

int gr_drive=DETECT, gr_mode;
unsigned char lbutton, rbutton;
int xmouse, ymouse;

int sonut = 0;
typedef struct tagnode {
int x, y; /* Vi tri tren man hinh */
} NODE;
NODE nut[MAX];
int weight[MAX][MAX];
int themduoc = 1;

void initialize()
{
char s1[] = "Nhap nut phai chuot de them nut";
char s2[] = "Nhap nut trai chuot va re de them duong noi";
char s3[] = "Nhan phim Q de thoat";
initgraph(&gr_drive, &gr_mode, "");
reset_mouse();
setcolor(YELLOW);
rectangle(0, 0, getmaxx(), getmaxy());
outtextxy((getmaxx()-textwidth(s1))/2, 5, s1);
outtextxy((getmaxx()-textwidth(s2))/2, 15, s2);
outtextxy((getmaxx()-textwidth(s3))/2, 25, s3);
line(0, 35, getmaxx(), 35);
set_mouse_hlimits(5, getmaxx()-6);
set_mouse_vlimits(40, getmaxy()-6);
show_mouse();
}

int index(int x, int y, int heso)
{
int i, OK = 0;
for (i=0; i<sonut; i++)
if (abs(nut.x - x) < 4*heso && abs(nut.y - y) < 4*heso)
{
OK = 1;
break;
}
if (OK)
return i;
else
return -1;
}

void get_mouse()
{
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
} while (lbutton == 0 && rbutton == 0 && !kbhit());
}

void clear_mouse()
{
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
} while (lbutton == 1 || rbutton == 1);
}

int input_weight(int start, int end)
{
int size, i;
void far *buf;
char c, s[]="Nhap trong so", s1[3]="";
size = imagesize(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10);
buf = malloc(size);
getimage(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10, buf);
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
bar(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10);
setcolor(WHITE);
rectangle(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10);
line(getmaxx()/2 - 70, getmaxy()/2 - 5,getmaxx()/2 + 70, getmaxy()/2 - 5);
outtextxy((getmaxx()-textwidth(s))/2 - 4, getmaxy()/2 - 16, s);
i = 0;
do {
do {
c = getch();
} while ((c < '0' || c > '9') && c != 13 && c != 27 && c != 8);
if (c>='0' && c <= '9' && i<2)
{
s1 = c;
s1[i+1] = 0;
i++;
}
if (c == 8 && i>0)
{
i--;
s1 = 0;
}
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
bar(getmaxx()/2 - 69, getmaxy()/2 - 3,getmaxx()/2 + 69, getmaxy()/2 + 9);
setcolor(YELLOW);
outtextxy((getmaxx()-textwidth(s))/2 - 4, getmaxy()/2 - 1, s1);
} while(c != 13 && c != 27);
putimage(getmaxx()/2 - 70, getmaxy()/2 - 20, buf, COPY_PUT);
free(buf);
if (c == 13)
{
i = atoi(s1);
weight[start][end] = i;
weight[end][start] = i;
return i;
}
else
return -1;
}

void get_weight()
{
int x, y, oldx, oldy, i, OK = 0, start, end;
char s[5];
setwritemode(XOR_PUT);
setcolor(GREEN);
for (i=0; i<sonut; i++)
if (abs(nut.x - xmouse) < 4 && abs(nut.y - ymouse) < 4)
{
start = i;
oldx = x = nut.x;
oldy = y = nut.y;
OK = 1;
break;
}
if (!OK)
return;
hide_mouse();
line(x, y, oldx, oldy);
clear_mouse();
show_mouse();
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
if (oldx != xmouse || oldy != ymouse)
{
hide_mouse();
line(x, y, oldx, oldy);
oldx = xmouse;
oldy = ymouse;
line(x, y, oldx, oldy);
show_mouse();
}
} while (lbutton == 0);
OK = 0;
hide_mouse();
line(x, y, oldx, oldy);
for (i=0; i<sonut; i++)
if (abs(nut.x - xmouse) < 4 && abs(nut.y - ymouse) < 4)
{
end = i;
OK = 1;
break;
}
if (OK && end != start)
{
setcolor(GREEN);
if ((i = input_weight(start, end)) != -1)
{
line(x, y, nut[end].x, nut[end].y);
itoa(i, s, 10);
setcolor(RED);
outtextxy(x + (nut[end].x - x) / 2 + 4, y + (nut[end].y - y) / 2 + 4, s);
}
}
setwritemode(COPY_PUT);
show_mouse();
}

void main()
{
int done = 0;
char c, s[4];

initialize();
do {
get_mouse();
if (kbhit())
{
c = toupper(getch());
if (c == 'Q')
done = 1;
}
if (rbutton == 1 && index(xmouse, ymouse, 10) == -1)
{
hide_mouse();
if (themduoc)
{
setcolor(WHITE);
circle(xmouse, ymouse, 4);
itoa(sonut, s, 10);
setcolor(CYAN);
outtextxy(xmouse+6, ymouse-6, s);
nut[sonut].x = xmouse;
nut[sonut].y = ymouse;
sonut++;
if (sonut>=MAX)
themduoc = 0;
}
clear_mouse();
show_mouse();
}
if (lbutton == 1)
{
get_weight();
clear_mouse();
}
} while (!done);
closegraph();
}
Love mickey,
Đăng nhập để trả lời
0
#include <stdio.h>

typedef int element_type;
struct node {
element_type element;
struct node *next;
} *first = NULL;

void insert(element_type e)
{
struct node *tmp, *t;
tmp = (struct node*) malloc(sizeof(struct node));
tmp->element = e;
tmp->next = NULL;
if (first == NULL)
first = tmp;
else
{
t = first;
while (t->next != NULL)
t = t->next;
t->next = tmp;
}
}

struct node *nutcuoi(struct node *first)
{
struct node *tmp;
tmp = first;
while (tmp != NULL && tmp->next != NULL)
tmp = tmp->next;
return tmp;
}

void main()
{
element_type e;
struct node *tmp;

printf("\nNhap cac gia tri so (-1) de ket thuc : ");
do {
scanf("%d", &e);
if (e != -1)
insert(e);
} while (e != -1);

tmp = nutcuoi(first);
if (tmp)
printf("Nut cuoi co gia tri = %d", tmp->element);
getch();

tmp = first;
while (tmp != NULL)
{
tmp = first->next;
free(first);
first = tmp;
}
}
Love mickey,
Đăng nhập để trả lời
0
#include <stdio.h>
#include <alloc.h>

typedef int element_type;
typedef struct node {
element_type element;
struct node *left, *right;
} NODE;

NODE *root;

void khoi_tao_cay(NODE ** root)
{
*root = NULL;
}

void insert(NODE *tmp, NODE **root)
{

if (tmp->element < (*root)->element)
if ((*root)->left)
insert(tmp, &(*root)->left);
else
(*root)->left = tmp;
else
if ((*root)->right)
insert(tmp, &(*root)->right);
else
(*root)->right = tmp;
}

void insert_node(element_type e, NODE **root)
{
NODE *tmp;

tmp = (NODE *)malloc(sizeof(NODE));
tmp->element = e;
tmp->left = NULL;
tmp->right = NULL;
if (*root == NULL)
*root = tmp;
else
insert(tmp, root);
}

void nhap_cay(NODE **root)
{
element_type e;
do {
printf("\nNhap element (-1 de ket thuc) : ");
scanf("%d", &e);
if (e != -1)
insert_node(e, root);
} while (e != -1);
}

int dem_nut_la(NODE *root)
{
if (root == NULL)
return 0;
else
if (root->left != NULL || root->right != NULL)
return dem_nut_la(root->left) + dem_nut_la(root->right);
else
return 1;
}

void main()
{
int tong_nut_la;
khoi_tao_cay(&root);
nhap_cay(&root);
tong_nut_la = dem_nut_la(root);
printf("\nTong so nut la = %d", tong_nut_la);
getch();
}
Love mickey,
Đăng nhập để trả lời
0
#include <stdio.h>
#include <stdlib.h>

#define MAX 10
int mang[MAX];

void in_mang(int *mang)
{
int i;
for (i=0; i<MAX; i++)
printf("%d ", mang);
}

void sort(int l, int r)
{
int i, j, x, w;
i = l;
j = r;
x = mang[(l+r)/2];
do {
while (mang < x)
i++;
while (x < mang[j])
j--;
if (i <= j)
{
w = mang;
mang = mang[j];
mang[j] = w;
i++;
j--;
}
} while (i<=j);
if (l < j)
sort(l, j);
if (i < r)
sort(i, r);
}

void quicksort()
{
sort(0, MAX-1);
}

void main()
{
int i;

randomize();
for (i=0; i<MAX; i++)
mang = random(100);
printf("\nTruoc khi sap : ");
in_mang(mang);
quicksort();
printf("\nSau khi sap : ");
in_mang(mang);
getch();
}
Love mickey,
Đăng nhập để trả lời
0
/* Bai tap 3_17 - Rut cac run tu nhien tu mot danh sach lien ket */
#include <conio.h>
#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>

#define MAX 25

typedef struct tag {
int key;
struct tag *next;
} SL;

SL *first;

typedef struct tagds {
SL *s;
struct tagds *next;
} SL2;

SL2 *firstSL;

void tachrun()
{
SL *a, *b;
SL2 *fa, *fb;

if (first != NULL)
{
a = first;
do {
fa = (SL2 *)malloc(sizeof(SL2));
fa->next = NULL;
fa->s = a;
b = a->next;
while (b != NULL)
{
if (b->key < a->key)
break;
a = b;
b = b->next;
}
a->next = NULL;
a = b;
if (firstSL == NULL)
{
firstSL = fa;
fb = fa;
}
else
{
fb->next = fa;
fb = fa;
}
} while (b != NULL);
}
}

void insert(int key)
{
SL *s, *b;

s = (SL *)malloc(sizeof(SL));
s->key = key;
s->next = NULL;
if (first == NULL)
first = s;
else
{
b = first;
while (b->next != NULL)
b = b->next;
b->next = s;
}
}

void initialize()
{
int i;
randomize();
first = NULL;
for (i=0; i<MAX; i++)
insert(random(100));
firstSL = NULL;
}

void release(SL *s)
{
if (s->next != NULL)
release(s->next);
free(s);
}

void cleanup()
{
release(first);
}

void in_ds(SL *first)
{
SL *s;
printf("\n");
s = first;
while (s != NULL)
{
printf("%3d", s->key);
s = s->next;
}
}

void inds(SL2 *f)
{
in_ds(f->s);
}

void inrun()
{
SL2 *f;
if (firstSL != NULL)
{
f = firstSL;
while (f != NULL)
{
inds(f);
f = f->next;
}
}
}

void main()
{
initialize();
in_ds(first);
tachrun();
inrun();
getch();
cleanup();
}
Love mickey,
Đăng nhập để trả lời
0
#include <stdio.h>
#include <string.h>

void main()
{
char source[250], substr[250], *found;
int count = 0;

printf("\nNhap chuoi nguon : ");
gets(source);
printf("\nNhap chuoi tim kiem : ");
gets(substr);
found = source;
while ((found = strstr(found, substr)) != NULL)
{
count++;
found++;
}
printf("\nSo lan xuat hien = %d", count);
getch();
}
Love mickey,
Đăng nhập để trả lời
0
/* Sap bang phuong phap chon lua truc tiep */
#include <stdio.h>
#include <stdlib.h>

#define MAX 10
int mang[MAX];

void in_mang(int *mang)
{
int i;
for (i=0; i<MAX; i++)
printf("%d ", mang);
}

void sssort()
{
int i, j, k, x, mx;
for (i=0; i<MAX-1; i++)
{
mx = mang;
k = i;
for (j=i+1; j<MAX; j++)
if (mang[j] < mx)
{
mx = mang[j];
k = j;
}
x = mang[k];
mang[k] = mang;
mang = x;
}
}

void main()
{
int i;

randomize();
for (i=0; i<MAX; i++)
mang = random(100);
printf("\nTruoc khi sap : ");
in_mang(mang);
sssort();
printf("\nSau khi sap : ");
in_mang(mang);
getch();
}
Love mickey,
Đăng nhập để trả lời
0
#include <stdio.h>
#include <string.h>
#include <alloc.h>

void timchuoi(char *source, char *substr, int *k)
{
char *stmp, *dtmp, *found;

stmp = strupr(strdup(source));
dtmp = strupr(strdup(substr));
found = strstr(stmp, dtmp);
if (found)
*k = found - stmp;
else
*k = -1;
free(stmp);
free(dtmp);
}

void main()
{
char source[255], substr[50];
int k;

printf("\nNhap chuoi nguon : ");
gets(source);
printf("\nNhap chuoi tim kiem : ");
gets(substr);
timchuoi(source, substr, &k);
if (k != -1)
printf("\nChuoi tim thay tai vi tri : %d", k);
else
printf("\nKhong tim thay chuoi da cho");

getch();
}
Love mickey,
Đăng nhập để trả lời
0
/* Tim mot phan tu trong mot DSLK bang de quy */
#include <stdio.h>

typedef int element_type;
struct node {
element_type element;
struct node *next;
};

void khoi_tao_ds(struct node **first, struct node **last)
{
*first = *last = NULL;
}

void insert(element_type e, struct node **first, struct node **last)
{
struct node *tmp;
tmp = (struct node*) malloc(sizeof(struct node));
tmp->element = e;
tmp->next = NULL;
if (*first == NULL)
*first = *last = tmp;
else
{
(*last)->next = tmp;
(*last) = (*last)->next;
}
}

void xoa_ds(struct node **first, struct node **last)
{
struct node *tmp;
tmp = *first;
while (tmp != NULL)
{
tmp = (*first)->next;
free(*first);
*first = tmp;
}
*first = *last = NULL;
}

void nhap_ds(struct node **first, struct node **last)
{
element_type e;
printf("\nNhap cac gia tri so (-1) de ket thuc : ");
do {
scanf("%d", &e);
if (e != -1)
insert(e, first, last);
} while (e != -1);
}

void print_ds(struct node *first)
{
struct node *tmp;

tmp = first;
printf("Cac gia tri cua danh sach : ");
while (tmp != NULL)
{
printf("%d ", tmp->element);
tmp = tmp->next;
}
}

struct node *timdequy(struct node *first, element_type e)
{
if (first == NULL)
return NULL;
else
if (first->element == e)
return first;
else
return timdequy(first->next, e);
}

void main()
{
struct node *first, *last, *tim;
element_type e;

khoi_tao_ds(&first, &last);
nhap_ds(&first, &last);
print_ds(first);
printf("\nNhap gia tri can tim : ");
scanf("%d", &e);
tim = timdequy(first, e);
if (tim)
printf("Tim thay");
else
printf("Khong tim thay");
getch();
}
Love mickey,
Đăng nhập để trả lời
0
#include <stdio.h>

#define MAX 10

int tim_nhi_phan(int mang[], int x)
{
int i, start = 0, end = MAX;

do {
i = (end - start) / 2;
if (mang < x)
start = i;
else if (mang > x)
end = i;
} while (mang != x && end > start);
return i;
}

void main()
{
int mang[MAX], i, x, n;

printf("\nNhap vao 10 phan tu tang dan cua mang");
for (i=0; i<MAX; i++)
scanf("%d", &mang);
printf("\nNhap vao gia tri can tim : ");
scanf("%d", &x);
n = tim_nhi_phan(mang, x);
if (n)
printf("\nGia tri tim thay tai chi so %d", n);
else
printf("\nKhong tim thay trong mang");
getch();
}
Love mickey,
Đăng nhập để trả lời
0
/* Tim mot phan tu trong mot DSLK khong de quy */
#include <stdio.h>

typedef int element_type;
struct node {
element_type element;
struct node *next;
};

void khoi_tao_ds(struct node **first, struct node **last)
{
*first = *last = NULL;
}

void insert(element_type e, struct node **first, struct node **last)
{
struct node *tmp;
tmp = (struct node*) malloc(sizeof(struct node));
tmp->element = e;
tmp->next = NULL;
if (*first == NULL)
*first = *last = tmp;
else
{
(*last)->next = tmp;
(*last) = (*last)->next;
}
}

void xoa_ds(struct node **first, struct node **last)
{
struct node *tmp;
tmp = *first;
while (tmp != NULL)
{
tmp = (*first)->next;
free(*first);
*first = tmp;
}
*first = *last = NULL;
}

void nhap_ds(struct node **first, struct node **last)
{
element_type e;
printf("\nNhap cac gia tri so (-1) de ket thuc : ");
do {
scanf("%d", &e);
if (e != -1)
insert(e, first, last);
} while (e != -1);
}

void print_ds(struct node *first)
{
struct node *tmp;

tmp = first;
printf("Cac gia tri cua danh sach : ");
while (tmp != NULL)
{
printf("%d ", tmp->element);
tmp = tmp->next;
}
}

struct node *tim(struct node *first, element_type e)
{
if (first == NULL)
return NULL;
else
{
while (first->element != e && first != NULL)
first = first->next;
return first;
}
}

void main()
{
struct node *first, *last, *tmp;
element_type e;

khoi_tao_ds(&first, &last);
nhap_ds(&first, &last);
print_ds(first);
printf("\nNhap gia tri can tim : ");
scanf("%d", &e);
tmp = tim(first, e);
if (tmp)
printf("Tim thay");
else
printf("Khong tim thay");
getch();
}
Love mickey,
Đăng nhập để trả lời
0
/* Tim mot phan tu trong mot DSLK */
#include <stdio.h>

typedef int element_type;
struct node {
element_type element;
struct node *next;
};

void khoi_tao_ds(struct node **first, struct node **last)
{
*first = *last = NULL;
}

void insert(element_type e, struct node **first, struct node **last)
{
struct node *tmp;
tmp = (struct node*) malloc(sizeof(struct node));
tmp->element = e;
tmp->next = NULL;
if (*first == NULL)
*first = *last = tmp;
else
{
(*last)->next = tmp;
(*last) = (*last)->next;
}
}

void xoa_ds(struct node **first, struct node **last)
{
struct node *tmp;
tmp = *first;
while (tmp != NULL)
{
tmp = (*first)->next;
free(*first);
*first = tmp;
}
*first = *last = NULL;
}

void nhap_ds(struct node **first, struct node **last)
{
element_type e;
printf("\nNhap cac gia tri so (-1) de ket thuc : ");
do {
scanf("%d", &e);
if (e != -1)
insert(e, first, last);
} while (e != -1);
}

void print_ds(struct node *first)
{
struct node *tmp;

tmp = first;
printf("Cac gia tri cua danh sach (tang dan) : ");
while (tmp != NULL)
{
printf("%d ", tmp->element);
tmp = tmp->next;
}
}

int tim(struct node *first, int x)
{
int n = 0;
while (first != NULL && first->element < x)
{
n++;
first = first->next;
}
if (first->x != x)
return -1;
return n;
}

void main()
{
struct node *first, *last;
int n, x;

khoi_tao_ds(&first, &last);
nhap_ds(&first, &last);
print_ds(first);
printf("\nNhap vao gia tri can tim : ");
scanf("%d", &x);
n = tim(first, x);
if (n!=-1)
printf("\nGia tri %d tim thay tai nut thu %d", x, n);
else
printf("\nKhong tim thay gia tri %d", x);
getch();
}
Love mickey,
Đăng nhập để trả lời
0
/* Bai tap 3_94 - Bai toan tron tim */
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <ctype.h>

#define RADIUS 3
#define DELAY 10

int gr_drive=DETECT, gr_mode;
int xvat, yvat, xnguoi, ynguoi;
int dx, dy, left, right, top, bottom;

void initialize()
{
char s1[] = "Nhan phim bat ky de bat dau";
char s2[] = "Nhan phim ESCAPE de ket thuc";

initgraph(&gr_drive, &gr_mode, "");
setcolor(YELLOW);
rectangle(0, 0, getmaxx(), getmaxy());
outtextxy((getmaxx()-textwidth(s1))/2, 4, s1);
line(0, 16, getmaxx(), 16);
getch();
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
bar(1, 1, getmaxx(), 15);
setcolor(YELLOW);
outtextxy((getmaxx()-textwidth(s1))/2, 4, s2);
setviewport(1, 17, getmaxx()-1, getmaxy()-1, 1);
left = top = 0;
right = getmaxx() - 2;
bottom = getmaxy() - 18;
randomize();
xvat = random(getmaxx()/2);
xnguoi = random(getmaxx()/2)+getmaxx()/2;
yvat = random(getmaxy()/2);
ynguoi = random(getmaxy()/2)+getmaxy()/2;
}

void drawvat(int color)
{
setcolor(color);
circle(xvat, yvat, RADIUS);
}

void drawnguoi(int color)
{
setcolor(color);
rectangle(xnguoi-3, ynguoi-2, xnguoi+3, ynguoi+2);
}

void tinhvat()
{
if (xnguoi >= xvat)
xvat += 2;
if (xnguoi < xvat)
xvat -= 2;
/*
if (xvat < 2)
xvat += 2;
if (xvat > right-2)
xvat = right-2;
*/
if (ynguoi >= yvat)
yvat += 2;
if (ynguoi < yvat)
yvat -= 2;
/*
if (yvat < 2)
yvat += 2;
if (yvat >bottom - 2)
yvat = bottom - 2;
*/
}

void tinhnguoi()
{
xnguoi += dx;
ynguoi += dy;
if (xnguoi < left)
xnguoi = left;
if (xnguoi > right)
xnguoi = right;
if (ynguoi < top)
ynguoi = top;
if (ynguoi > bottom)
ynguoi = bottom;
}

void main()
{
char c, s[] = "Oh! Ban da thua roi";
initialize();
drawvat(YELLOW);
drawnguoi(WHITE);
dx = dy = 0;
do {
if (kbhit())
{
c = toupper(getch());
if (c == 0)
c = getch();
switch(c)
{
case 'A' : dx = 0; break;
case 'S' : dy = 0; break;
case 75 : dx = -2; break;
case 77 : dx = 2; break;
case 72 : dy = -2; break;
case 80 : dy = 2; break;
}
}
delay(DELAY);
drawvat(BLACK);
drawnguoi(BLACK);
tinhnguoi();
tinhvat();
drawvat(YELLOW);
drawnguoi(WHITE);
if (abs(xnguoi-xvat) < 4 && abs(ynguoi-yvat) < 4)
{
setviewport(0, 0, getmaxx(), getmaxy(), 1);
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
bar(1, 1, getmaxx(), 15);
setcolor(YELLOW);
outtextxy((getmaxx()-textwidth(s))/2, 4, s);
getch();
c = 27;
}
} while (c != 27);
closegraph();
}
Love mickey,
Đăng nhập để trả lời
0
/* Kiem tra xem hai cay co dang cau nhau khong */
#include <stdio.h>
#include <alloc.h>

typedef int element_type;
typedef struct node {
element_type element;
struct node *left, *right;
} NODE;

NODE *root1, *root2;

void khoi_tao_cay(NODE **root)
{
*root = NULL;
}

void insert(NODE *tmp, NODE **root)
{

if (tmp->element < (*root)->element)
if ((*root)->left)
insert(tmp, &(*root)->left);
else
(*root)->left = tmp;
else
if ((*root)->right)
insert(tmp, &(*root)->right);
else
(*root)->right = tmp;
}

void insert_node(element_type e, NODE **root)
{
NODE *tmp;

tmp = (NODE *)malloc(sizeof(NODE));
tmp->element = e;
tmp->left = NULL;
tmp->right = NULL;
if (*root == NULL)
*root = tmp;
else
insert(tmp, root);
}

void nhap_cay(NODE **root)
{
element_type e;
do {
printf("\nNhap element (-1 de ket thuc) : ");
scanf("%d%*c", &e);
if (e != -1)
insert_node(e, root);
} while (e != -1);
}

int kiemtra(NODE *root1, NODE *root2)
{
if (root1 != NULL && root2 != NULL)
if (kiemtra(root1->left, root2->right) &&
kiemtra(root1->right, root2->left))
return 1;
else
return 0;
else
if (root1 == NULL && root2 == NULL)
return 1;
else
return 0;
}

void main()
{
khoi_tao_cay(&root1);
nhap_cay(&root1);
khoi_tao_cay(&root2);
nhap_cay(&root2);
if (kiemtra(root1, root2))
printf("\nHai cay da cho tuong tu cau truc");
else
printf("\nHai cay da cho khac cau truc");
getch();
}
Love mickey,
Đăng nhập để trả lời
0
/* Kiem tra xem hai cay co tuong tu ve cau truc nhau khong */
#include <stdio.h>
#include <alloc.h>

typedef int element_type;
typedef struct node {
element_type element;
struct node *left, *right;
} NODE;

NODE *root1, *root2;

void khoi_tao_cay(NODE **root)
{
*root = NULL;
}

void insert(NODE *tmp, NODE **root)
{

if (tmp->element < (*root)->element)
if ((*root)->left)
insert(tmp, &(*root)->left);
else
(*root)->left = tmp;
else
if ((*root)->right)
insert(tmp, &(*root)->right);
else
(*root)->right = tmp;
}

void insert_node(element_type e, NODE **root)
{
NODE *tmp;

tmp = (NODE *)malloc(sizeof(NODE));
tmp->element = e;
tmp->left = NULL;
tmp->right = NULL;
if (*root == NULL)
*root = tmp;
else
insert(tmp, root);
}

void nhap_cay(NODE **root)
{
element_type e;
do {
printf("\nNhap element (-1 de ket thuc) : ");
scanf("%d%*c", &e);
if (e != -1)
insert_node(e, root);
} while (e != -1);
}

int kiemtra(NODE *root1, NODE *root2)
{
if (root1 != NULL && root2 != NULL)
if (kiemtra(root1->left, root2->left) &&
kiemtra(root1->right, root2->right))
return 1;
else
return 0;
else
if (root1 == NULL && root2 == NULL)
return 1;
else
return 0;
}

void main()
{
khoi_tao_cay(&root1);
nhap_cay(&root1);
khoi_tao_cay(&root2);
nhap_cay(&root2);
if (kiemtra(root1, root2))
printf("\nHai cay da cho tuong tu cau truc");
else
printf("\nHai cay da cho khac cau truc");
getch();
}
Love mickey,
Đăng nhập để trả lời
0
/* Thay the chuoi con trong mot chuoi bang mot chuoi con khac */
#include <stdio.h>
#include <string.h>
#include <alloc.h>

char *tim_thay(char *source, char *substr, char *replace)
{
char *found, *temp, *stemp;
int pos = 0;

stemp = strdup(source);
found = strstr(stemp + pos, substr);
while (found)
{
pos = found - stemp + strlen(replace) - strlen(substr) + 1;
temp = (char *) malloc(sizeof(stemp) + strlen(replace) - strlen(substr) + 1);
strncpy(temp, stemp, found - stemp);
temp[found-stemp] = 0;
strcat(temp, replace);
strcat(temp, found + strlen(substr));
free(stemp);
stemp = (char *)malloc(sizeof(temp) + 1);
strcpy(stemp, temp);
free(temp);
found = strstr(stemp + pos, substr);
}
return stemp;
}

void main()
{
char source[255], substr[50], replace[50], *result;

printf("\nNhap chuoi nguon : ");
gets(source);
printf("\nNhap chuoi tim kiem : ");
gets(substr);
printf("\nNhap chuoi thay the : ");
gets(replace);
result = tim_thay(source, substr, replace);
printf("\nKet qua = %s", result);
getch();
}
Love mickey,
Đăng nhập để trả lời
0
/* Bai tap 3_94 - Bai toan tron tim */
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#include <ctype.h>

#define RADIUS 3
#define DELAY 10

int gr_drive=DETECT, gr_mode;
int xvat, yvat, xnguoi, ynguoi;
int dx, dy, left, right, top, bottom;

void initialize()
{
char s1[] = "Nhan phim bat ky de bat dau";
char s2[] = "Nhan phim ESCAPE de ket thuc";

initgraph(&gr_drive, &gr_mode, "");
setcolor(YELLOW);
rectangle(0, 0, getmaxx(), getmaxy());
outtextxy((getmaxx()-textwidth(s1))/2, 4, s1);
line(0, 16, getmaxx(), 16);
getch();
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
bar(1, 1, getmaxx(), 15);
setcolor(YELLOW);
outtextxy((getmaxx()-textwidth(s1))/2, 4, s2);
setviewport(1, 17, getmaxx()-1, getmaxy()-1, 1);
left = top = 0;
right = getmaxx() - 2;
bottom = getmaxy() - 18;
randomize();
xvat = random(getmaxx()/2);
xnguoi = random(getmaxx()/2)+getmaxx()/2;
yvat = random(getmaxy()/2);
ynguoi = random(getmaxy()/2)+getmaxy()/2;
}

void drawvat(int color)
{
setcolor(color);
circle(xvat, yvat, RADIUS);
}

void drawnguoi(int color)
{
setcolor(color);
rectangle(xnguoi-3, ynguoi-2, xnguoi+3, ynguoi+2);
}

void tinhvat()
{
if (xnguoi >= xvat)
xvat -= 2;
if (xnguoi < xvat)
xvat += 2;
if (xvat < 2)
xvat += 2;
if (xvat > right-2)
xvat = right-2;
if (ynguoi >= yvat)
yvat -= 2;
if (ynguoi < yvat)
yvat += 2;
if (yvat < 2)
yvat += 2;
if (yvat >bottom - 2)
yvat = bottom - 2;
}

void tinhnguoi()
{
xnguoi += dx;
ynguoi += dy;
if (xnguoi < left)
xnguoi = left;
if (xnguoi > right)
xnguoi = right;
if (ynguoi < top)
ynguoi = top;
if (ynguoi > bottom)
ynguoi = bottom;
}

void main()
{
char c, s[] = "Hoan ho! Ban da chien thang";
initialize();
drawvat(YELLOW);
drawnguoi(WHITE);
dx = dy = 0;
do {
if (kbhit())
{
c = toupper(getch());
if (c == 0)
c = getch();
switch(c)
{
case 'A' : dx = 0; break;
case 'S' : dy = 0; break;
case 75 : dx = -2; break;
case 77 : dx = 2; break;
case 72 : dy = -2; break;
case 80 : dy = 2; break;
}
}
delay(DELAY);
drawvat(BLACK);
drawnguoi(BLACK);
tinhnguoi();
tinhvat();
drawvat(YELLOW);
drawnguoi(WHITE);
if (abs(xnguoi-xvat) < 4 && abs(ynguoi-yvat) < 4)
{
setviewport(0, 0, getmaxx(), getmaxy(), 1);
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
bar(1, 1, getmaxx(), 15);
setcolor(YELLOW);
outtextxy((getmaxx()-textwidth(s))/2, 4, s);
getch();
c = 27;
}
} while (c != 27);
closegraph();
}
Love mickey,
Đăng nhập để trả lời
0
/* Bai tap 3_89 - Tim bao dong chuyen tiep (Transitive Closure) cua mot do thi */
#include <dos.h>
#include <graphics.h>
#include <alloc.h>
#include "mouse.inc"

#pragma warn -sus

/* Toi da 100 nut */
#define MAX 100

int gr_drive=DETECT, gr_mode;
unsigned char lbutton, rbutton;
int xmouse, ymouse;

int sonut = 0;
typedef struct tagnode {
int x, y; /* Vi tri tren man hinh */
} NODE;

NODE nut[MAX];
int weight[MAX][MAX];
int themduoc = 1;

void DFS();

void initialize()
{
char s1[] = "Nhap nut phai chuot de them nut";
char s2[] = "Nhap nut trai chuot va re de them duong noi";
char s3[] = "Nhan phim Q de thoat - S de bat dau minh hoa giai thuat";
int i, j;

initgraph(&gr_drive, &gr_mode, "");
reset_mouse();
setcolor(YELLOW);
rectangle(0, 0, getmaxx(), getmaxy());
outtextxy((getmaxx()-textwidth(s1))/2, 5, s1);
outtextxy((getmaxx()-textwidth(s2))/2, 15, s2);
outtextxy((getmaxx()-textwidth(s3))/2, 25, s3);
line(0, 35, getmaxx(), 35);
set_mouse_hlimits(5, getmaxx()-6);
set_mouse_vlimits(40, getmaxy()-6);
for (i=0; i<MAX; i++)
for (j=0; j<MAX; j++)
weight[j] = -1;
show_mouse();
}

int index(int x, int y, int heso)
{
int i, OK = 0;
for (i=0; i<sonut; i++)
if (abs(nut.x - x) < 4*heso && abs(nut.y - y) < 4*heso)
{
OK = 1;
break;
}
if (OK)
return i;
else
return -1;
}

void get_mouse()
{
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
} while (lbutton == 0 && rbutton == 0 && !kbhit());
}

void clear_mouse()
{
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
} while (lbutton == 1 || rbutton == 1);
}

int input_weight(int start, int end)
{
int size, i;
void far *buf;
char c, s[]="Nhap trong so", s1[3]="";
size = imagesize(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10);
buf = malloc(size);
getimage(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10, buf);
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
bar(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10);
setcolor(WHITE);
rectangle(getmaxx()/2 - 70, getmaxy()/2 - 20,getmaxx()/2 + 70, getmaxy()/2 + 10);
line(getmaxx()/2 - 70, getmaxy()/2 - 5,getmaxx()/2 + 70, getmaxy()/2 - 5);
outtextxy((getmaxx()-textwidth(s))/2 - 4, getmaxy()/2 - 16, s);
i = 0;
do {
do {
c = getch();
} while ((c < '0' || c > '9') && c != 13 && c != 27 && c != 8);
if (c>='0' && c <= '9' && i<2)
{
s1 = c;
s1[i+1] = 0;
i++;
}
if (c == 8 && i>0)
{
i--;
s1 = 0;
}
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
bar(getmaxx()/2 - 69, getmaxy()/2 - 3,getmaxx()/2 + 69, getmaxy()/2 + 9);
setcolor(YELLOW);
outtextxy((getmaxx()-textwidth(s))/2 - 4, getmaxy()/2 - 1, s1);
} while(c != 13 && c != 27);
putimage(getmaxx()/2 - 70, getmaxy()/2 - 20, buf, COPY_PUT);
free(buf);
if (c == 13)
{
i = atoi(s1);
weight[start][end] = i;
weight[end][start] = i;
return i;
}
else
return -1;
}

void get_weight()
{
int x, y, oldx, oldy, i, OK = 0, start, end;
char s[5];
setwritemode(XOR_PUT);
setcolor(GREEN);
for (i=0; i<sonut; i++)
if (abs(nut.x - xmouse) < 4 && abs(nut.y - ymouse) < 4)
{
start = i;
oldx = x = nut.x;
oldy = y = nut.y;
OK = 1;
break;
}
if (!OK)
return;
hide_mouse();
line(x, y, oldx, oldy);
clear_mouse();
show_mouse();
do {
get_mouse_button(&lbutton, &rbutton, &xmouse, &ymouse);
if (oldx != xmouse || oldy != ymouse)
{
hide_mouse();
line(x, y, oldx, oldy);
oldx = xmouse;
oldy = ymouse;
line(x, y, oldx, oldy);
show_mouse();
}
} while (lbutton == 0);
OK = 0;
hide_mouse();
line(x, y, oldx, oldy);
for (i=0; i<sonut; i++)
if (abs(nut.x - xmouse) < 4 && abs(nut.y - ymouse) < 4)
{
end = i;
OK = 1;
break;
}
if (OK && end != start)
{
if ((i = input_weight(start, end)) != -1)
{
setcolor(GREEN);
line(x, y, nut[end].x, nut[end].y);
itoa(i, s, 10);
setcolor(RED);
outtextxy(x + (nut[end].x - x) / 2 + 4, y + (nut[end].y - y) / 2 + 4, s);
}
}
setwritemode(COPY_PUT);
show_mouse();
}

void main()
{
int done = 0;
char c, s[4];

initialize();
do {
get_mouse();
if (kbhit())
{
c = toupper(getch());
switch(c)
{
case 'Q' : done = 1; break;
case 'S' : DFS(); break;
}
}
if (rbutton == 1 && index(xmouse, ymouse, 10) == -1)
{
hide_mouse();
if (themduoc)
{
setcolor(WHITE);
circle(xmouse, ymouse, 4);
itoa(sonut, s, 10);
setcolor(CYAN);
outtextxy(xmouse+6, ymouse-6, s);
nut[sonut].x = xmouse;
nut[sonut].y = ymouse;
sonut++;
if (sonut>=MAX)
themduoc = 0;
}
clear_mouse();
show_mouse();
}
if (lbutton == 1)
{
get_weight();
clear_mouse();
}
} while (!done);
closegraph();
}

void out(char *s)
{
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
bar(1, 36, getmaxx()-1, 46);
setcolor(YELLOW);
outtextxy(4, 38, s);
}

int top, stack[MAX];
int visited[MAX];

void push(int value)
{
stack[++top] = value;
visited[value] = 1;
}

void pop(int *value)
{
*value = stack[top--];
}

int get()
{
if (!isempty())
return stack[top];
else
return -1;
}

int isempty(void)
{
return (top == -1);
}

int xpos = 1;
int ypos = 10;

void visit(int nutxet)
{
int i, OK;
char s[3];

push(nutxet);
setcolor(YELLOW); itoa(nutxet, s, 10); outtextxy(xpos++*20, 38+ypos, s);
circle(nut[nutxet].x, nut[nutxet].y, 4); delay(1000);
visited[nutxet] = 1;
for (i=0; i<sonut; i++)
if (weight[nutxet] != -1 && visited == 0)
visit(i);
pop(&nutxet);
xpos = 1;
ypos += 10;
}

void DFS()
{
unsigned size, i, n;
void far *buf;
char s[] = "Nhan phim bat ky de tiep tuc";

hide_mouse();
size = imagesize(1, 36, 150, 300);
buf = malloc(size);
getimage(1, 36, 150, 300, buf);
/* Bat dau giai thuat */
for (n=0; n<sonut; n++)
{
top = -1;
for (i=0; i<sonut; i++)
visited = 0;
visit(n);
}
out(s);
getch();
setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
bar(1, 36, getmaxx()-1, 46);
/* Ket thuc giai thuat */
putimage(1, 36, buf, COPY_PUT);
free(buf);
show_mouse();
}
Love mickey,
Đăng nhập để trả lời
0
/* Tim vi tri xuat hien cuoi cung cua mot chuoi con trong mot chuoi */
#include <stdio.h>
#include <string.h>

int xhcuoi (char *source, char *substr)
{
char *found, *tmpstr;
tmpstr = found = source;
while ((found = strstr(found, substr)) != NULL)
{
tmpstr = found;
found++;
}
return tmpstr - source;
}
void main()
{
char source[250], substr[250];

printf("\nNhap chuoi nguon : ");
gets(source);
printf("\nNhap chuoi tim kiem : ");
gets(substr);
printf("\nVi tri xuat hien cuoi cung = %d", xhcuoi(source, substr));
getch();
}
Love mickey,
Đăng nhập để trả lời
0
/* Bai tap 3_31 - Xoa phan tu thu N trong mot danh sach lien ket */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <alloc.h>

typedef struct tagsl {
int key;
struct tagsl *next;
} SL;

SL *first, *last;

void initialize()
{
first = last = NULL;
}

void cleanup()
{
SL *f;

while (first != NULL)
{
f = first;
first = first->next;
free(f);
}
first = last = NULL;
}

void insert(int key)
{
SL *s;

s = (SL *)malloc(sizeof(SL));
s->key = key;
s->next = NULL;
if (first == NULL)
first = last = s;
else
{
last->next = s;
last = last->next;
}
}

void inds()
{
SL *f;
printf("\nDanh sach : ");
f = first;
while (f != NULL)
{
printf("%3d", f->key);
f = f->next;
}
}

int xoa_n(int n)
{
SL *f, *a;
int i = 0;
a = NULL;
f = first;
while (i<n && f!= NULL)
{
i++;
a = f;
f = f->next;
}
if (f == NULL)
return 0;
else
{
if (a == NULL)
{
first = f->next;
free(f);
}
else if (f->next == NULL)
{
last = a;
last->next = NULL;
free(f);
}
else
{
a->next = f->next;
free(f);
}
return 1;
}
}

void main()
{
int i, n;
initialize();
randomize();
for (i=0; i<20; i++)
insert(random(20));
inds();
do {
printf("\nXoa phan tu thu (<0 de thoat) : ");
scanf("%d", &n);
if (n>-1)
{
if (xoa_n(n))
inds();
else
printf("Danh sach co it hon %d phan tu", n+1);
}
} while (n > -1);
cleanup();
}
Love mickey,
Đăng nhập để trả lời
0
/* Xoa tam 1 phan tu trong DSLK */
#include <stdio.h>

typedef int element_type;
struct node {
element_type element;
struct node *next;
int signature;
};

void khoi_tao_ds(struct node **first, struct node **last)
{
*first = *last = NULL;
}

void insert(element_type e, struct node **first, struct node **last)
{
struct node *tmp;
tmp = (struct node*) malloc(sizeof(struct node));
tmp->element = e;
tmp->signature = 0;
tmp->next = NULL;
if (*first == NULL)
*first = *last = tmp;
else
{
(*last)->next = tmp;
(*last) = (*last)->next;
}
}

void delete_node(element_type e, struct node *first)
{
struct node *tmp;
tmp = first;
while (tmp != NULL && tmp->element != e)
tmp = tmp->next;
if (tmp != NULL)
tmp->signature = 1;
}

void recover_node(element_type e, struct node *first)
{
struct node *tmp;
tmp = first;
while (tmp != NULL && tmp->element != e)
tmp = tmp->next;
if (tmp != NULL)
tmp->signature = 0;
}

void xoa_ds(struct node **first, struct node **last)
{
struct node *tmp;
tmp = *first;
while (tmp != NULL)
{
tmp = (*first)->next;
free(*first);
*first = tmp;
}
*first = *last = NULL;
}

void nhap_ds(struct node **first, struct node **last)
{
element_type e;
printf("\nNhap cac gia tri so (-1) de ket thuc : ");
do {
scanf("%d", &e);
if (e != -1)
insert(e, first, last);
} while (e != -1);
}

void print_ds(struct node *first)
{
struct node *tmp;

tmp = first;
printf("\nCac gia tri cua danh sach : ");
while (tmp != NULL)
{
if (tmp->signature == 0)
printf("%d ", tmp->element);
tmp = tmp->next;
}
}

void main()
{
struct node *first, *last;
element_type e;

khoi_tao_ds(&first, &last);
nhap_ds(&first, &last);
print_ds(first);
do {
printf("\nNhap cac gia tri can xoa tam (-1 de ket thuc) : ");
scanf("%d", &e);
if (e != -1)
delete_node(e, first);
} while (e != -1);
print_ds(first);
do {
printf("\nNhap cac gia tri can phuc hoi (-1 de ket thuc) : ");
scanf("%d", &e);
if (e != -1)
recover_node(e, first);
} while (e != -1);
print_ds(first);
getch();
}
Love mickey,
Đăng nhập để trả lời
0