Từ Cấu trúc dữ liệu và thuật toán 1-1-24 (N07.TH1)
Thông tin
//123
<string.h> strcpy(destination, source); strcat(destination, source);
//FILE *f; f=fopen("input.txt","r"); fscanf(f,"%d%d",&n,&dc);fgetc(f); // fclose(f); f=fopen("output.txt","w"); fprintf(f,"Tong so thi sinh trung tuyen: %d\n",t); fclose(f);
// DSLK
include<stdio.h>
include<stdlib.h>
typedef struct Node { int data; struct Node next; } Node; typedef struct List { int size; Node head,tail; } List; Node newNode(int value) { Node temp=(Node)malloc(sizeof(Node)); temp->data=value; temp->next=NULL; return temp; } List* newList() { List temp=(List)malloc(sizeof(List)); temp->size=0; temp->head=NULL; temp->tail=NULL; return temp; } Node* findNode(List* li,int index) { Node temp=li->head; int i=0; while(temp->next!=NULL&&i<index-1) { temp=temp->next; i++; } return temp; } void addHead(List li,int value) { if(li->head==NULL) { li->head=newNode(value); li->tail=li->head; } else { Node temp=newNode(value); temp->next=li->head; li->head=temp; } li->size++; } void addTail(List li,int value) { if(li->tail==NULL) addHead(li,value); else { Node temp=newNode(value); li->tail->next=temp; li->tail=temp; li->size++; } } void addList(List li,int index,int value) { if(li->head==NULL) addHead(li,value); else { if(!index) addHead(li,value); else { Node temp=findNode(li,index); if(temp->next==NULL) addTail(li,value); else { Node *newNode1=newNode(value); newNode1->next=temp->next; temp->next=newNode1; li->size++; } } } } int topList(List li) { return li->head->data; } int removeHead(List* li) { if(li->head!=NULL) { Node temp=li->head; li->head=temp->next; if(li->head==NULL) li->tail=NULL; int n=temp->data; li->size--; free(temp); return n; } } void removeList(List li,int index) { if(li->head!=NULL) { if(!index) removeHead(li); else { Node cur=findNode(li,index),temp=cur->next;; cur->next=temp->next; li->size--; free(temp); } } } int maxList(List* li) { Node temp=li->head; int M=temp->data; while(temp!=NULL) { if(M<temp->data) M=temp->data; temp=temp->next; } return M; } void printfList(List li) { Node temp=li->head; while(temp!=NULL) { printf("%d ",temp->data); temp=temp->next; } printf("\n"); } /// // Kiểm tra dấu ngoặc int main() { List *li=newList(); char ch; while((ch=getchar())!='\n') { if(ch=='{'||ch=='['||ch=='(') { addHead(li,(int)ch); } else if(ch=='}'||ch==']'||ch==')') { if(li->head==NULL) { printf("0\n"); return 0; } if(ch=='}') ch='{'; else if(ch==']') ch='['; else if(ch==')') ch='('; if(!((char)topList(li)==ch)) { printf("0\n"); return 0; } removeHead(li); } } if(li->head==NULL) printf("1\n"); else printf("0\n"); return 0; } //// //Liệt Kê Dấu Ngoặc int main() { List *li=newList(),vt=newList(); char ch; int i=0; while((ch=getchar())!='\n') { if(ch=='{'||ch=='['||ch=='(') { addHead(li,(int)ch); addHead(vt,i+1); } else if(ch=='}'||ch==']'||ch==')') { if(ch=='}') ch='{'; else if(ch==']') ch='['; else if(ch==')') ch='('; if((char)topList(li)==ch) { printf("%d %d\n",topList(vt),i+1); } removeHead(li); removeHead(vt); } i++; } return 0; } //// //Sinh nhi phan
include<stdio.h>
include<stdlib.h>
include<string.h>
typedef struct Node { char data[100]; struct Node next; } Node; typedef struct List { int size; Node head,tail; } List; Node newNode(char* value) { Node temp=(Node)malloc(sizeof(Node)); strcpy(temp->data,value); temp->next=NULL; return temp; } List* newList() { List temp=(List)malloc(sizeof(List)); temp->size=0; temp->head=NULL; temp->tail=NULL; return temp; } void addHead(List* li,char* value) { if(li->head==NULL) { li->head=newNode(value); li->tail=li->head; } else { Node temp=newNode(value); temp->next=li->head; li->head=temp; } li->size++; } void addTail(List li,char* value) { if(li->tail==NULL) addHead(li,value); else { Node temp=newNode(value); li->tail->next=temp; li->tail=temp; li->size++; } } void removeHead(List li) { if(li->head!=NULL) { Node *temp=li->head; li->head=temp->next; if(li->head==NULL) li->tail=NULL; li->size--; free(temp); } } int main() { List *li=newList(); int n;scanf("%d",&n); char str[100];
addHead(li,"1");
for(int i=0;i<n;i+=2)
{
printf("%s\n",li->head->data);
strcpy(str,li->head->data);
strcat(str,"0");
addTail(li,str);
strcpy(str,li->head->data);
strcat(str,"1");
addTail(li,str);
removeHead(li);
}
int m=n/2,i=0;
while(li->head!=NULL&&i<m)
{
printf("%s\n",li->head->data);
removeHead(li);
i++;
}
return 0;
} //// // Trung thứ tự
include <stdio.h>
void inOrderTraversal(int tree[], int index, int n) { // Kiểm tra nếu chỉ số không hợp lệ hoặc node là NULL if (index < 0 || index >= n || tree[index] == -1) { return; }
// Duyệt cây con bên trái
inOrderTraversal(tree, 2 * index + 1, n);
// In ra giá trị của node
printf("%d ", tree[index]);
// Duyệt cây con bên phải
inOrderTraversal(tree, 2 * index + 2, n);
}
int main() { int n; scanf("%d", &n); // Nhập số lượng node int tree[n];
for (int i = 0; i < n; i++) {
scanf("%d", &tree[i]); // Nhập giá trị của từng node
}
// Gọi hàm duyệt cây theo thứ tự trung tứ từ node gốc (index 0)
inOrderTraversal(tree, 0, n);
return 0;
} //// //tìm BST
include<stdio.h>
include<stdlib.h>
typedef struct Tree { int data; struct Tree left,right; }Tree; Tree* newNode(int value) { Tree temp=(Tree)malloc(sizeof(Tree)); temp->data=value; temp->left=NULL; temp->right=NULL; return temp; } void insertNode(Tree *t,int value) { if(t==NULL) { t=newNode(value); } else { if(value<(*t)->data) insertNode(&((t)->left),value); else if(value>(t)->data) insertNode(&((t)->right),value); } } Tree findNode(Tree t,int value) { if(t==NULL) return NULL; if(value<t->data) findNode(t->left,value); else if(value>t->data) findNode(t->right,value); else return t; } void end(Tree *t) { if(t==NULL) return; end(t->left); printf("%d ",t->data); end(t->right); } int main() { Tree *t=NULL; int n,value,f;scanf("%d%d",&n,&f); for(int i=0;i<n;i++) { scanf("%d",&value); insertNode(&t,value); }
Tree *temp=findNode(t,f);
if(temp!=NULL) printf("1");
else printf("0");
} //// //Chèn và in tiền
include<stdio.h>
include<stdlib.h>
typedef struct Tree { int data; struct Tree left,right; }Tree; Tree* newNode(int value) { Tree temp=(Tree)malloc(sizeof(Tree)); temp->data=value; temp->left=NULL; temp->right=NULL; return temp; } void insertNode(Tree *t,int value) { if(t==NULL) { t=newNode(value); } else { if(value<(*t)->data) insertNode(&((t)->left),value); else if(value>(t)->data) insertNode(&((t)->right),value); } } Tree findNode(Tree t,int value) { if(t==NULL) return NULL; if(value<t->data) findNode(t->left,value); else if(value>t->data) findNode(t->right,value); else return t; } void end(Tree *t) { if(t==NULL) return; printf("%d ",t->data); end(t->left); // if(t->data!=-1) end(t->right); } int main() { Tree *t=NULL; int n,value;scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&value); insertNode(&t,value); } end(t); } //// //xóa BST
include<stdio.h>
include<stdlib.h>
typedef struct Tree { int data; struct Tree left,right; }Tree; Tree* newNode(int value) { Tree temp=(Tree)malloc(sizeof(Tree)); temp->data=value; temp->left=NULL; temp->right=NULL; return temp; } void insertNode(Tree *t,int value) { if(t==NULL) { t=newNode(value); } else { if(value<(*t)->data) insertNode(&((t)->left),value); else if(value>(t)->data) insertNode(&((t)->right),value); } } Tree* findMin(Tree* t) { while (t->left != NULL) { t = t->left; } return t; }
Tree* deleteNode(Tree* t, int value) { if (t == NULL) { return t; }
if (value < t->data) {
t->left = deleteNode(t->left, value);
} else if (value > t->data) {
t->right = deleteNode(t->right, value);
} else {
if (t->left == NULL) {
Tree* temp = t->right;
free(t);
return temp;
} else if (t->right == NULL) {
Tree* temp = t->left;
free(t);
return temp;
}
Tree* temp = findMin(t->right);
t->data = temp->data;
t->right = deleteNode(t->right, temp->data);
}
return t;
} void end(Tree *t) { if(t==NULL) return; printf("%d ",t->data); end(t->left); end(t->right); } int main() { Tree *t=NULL; int n,k,value;scanf("%d %d",&n,&k); for(int i=0;i<n;i++) { scanf("%d",&value); insertNode(&t,value); } t=deleteNode(t,k); end(t); return 0; } //// //sort
include <stdio.h>
// Hàm hoán đổi hai phần tử void swap(int* a, int* b) { int t = *a; *a = *b; *b = t; }
// Hàm chia mảng và chọn pivot int partition(int arr[], int low, int high) { int pivot = arr[high]; // Chọn phần tử cuối cùng làm pivot int i = (low - 1); // Chỉ số của phần tử nhỏ hơn pivot
for (int j = low; j <= high - 1; j++) {
// Nếu phần tử hiện tại nhỏ hơn hoặc bằng pivot
if (arr[j] <= pivot) {
i++; // Tăng chỉ số của phần tử nhỏ hơn
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
// Hàm thực hiện quicksort void quickSort(int arr[], int low, int high) { if (low < high) { // Pi là chỉ số nơi pivot đã được đặt đúng vị trí int pi = partition(arr, low, high);
// Sắp xếp các phần tử trước và sau phần tử pivot
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
// Hàm in mảng void printArray(int arr[], int size) { for (int i = 0; i < size; i++) printf("%d ", arr[i]); printf("\n"); } int arr[10000000]; int main() { int n;scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",arr+i);
quickSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
} //// //thi sinh trung tuyen 1
include <stdio.h>
include <stdlib.h>
include <string.h>
typedef struct Node { char *msv, *ht, *qq; int d; struct Node *next; } Node;
Node* createNode(char msv, int d, char *ht, char *qq) { Node *newNode = (Node)malloc(sizeof(Node)); if (newNode == NULL) { exit(1); } newNode->msv = msv; newNode->d = d; newNode->ht = ht; newNode->qq = qq; newNode->next = NULL; return newNode; }
char* readString(FILE f) { char *s = (char)malloc(30 * sizeof(char)); fscanf(f, "%s", s); return s; }
void appendNode(Node *head, Node *tail, char msv, int d, char *ht, char *qq) { Node *newNode = createNode(msv, d, ht, qq); if (head == NULL) { head = newNode; *tail = newNode; } else { (tail)->next = newNode; *tail = newNode; } }
void printNodes(FILE *f, Node *head) { while (head != NULL) { fprintf(f, "%s %d %s %s\n", head->msv, head->d, head->ht, head->qq); head = head->next; } }
void freeList(Node *head) { Node *temp; while (head != NULL) { temp = head; head = head->next; free(temp->msv); free(temp->ht); free(temp->qq); free(temp); } }
int main() { FILE *f = fopen("bai4.inp", "r"); if (f == NULL) { return 1; }
int n, dc;
fscanf(f, "%d%d", &n, &dc);
fgetc(f);
Node *head = NULL, *tail = NULL;
int d, sl = 0;
char *msv, *ht, *qq;
for (int i = 0; i < n; i++) {
msv = readString(f);
fscanf(f, "%d", &d);
ht = readString(f);
qq = readString(f);
if (d >= dc) {
appendNode(&head, &tail, msv, d, ht, qq);
sl++;
} else {
free(msv);
free(ht);
free(qq);
}
}
fclose(f);
f = fopen("bai4.out", "w");
if (f == NULL) {
freeList(head);
return 1;
}
fprintf(f, "Tong so thi sinh trung tuyen: %d\n", sl);
printNodes(f, head);
fclose(f);
freeList(head);
return 0;
} //// //TSCT p2
include<stdio.h>
include<string.h>
typedef struct SV{ char ma[51],name[51],que[51]; int gpa; }SV; int main() { int n,dc; char q[51]; FILE *f; f=fopen("bai5.inp","r"); fscanf(f,"%d%d%s",&n,&dc,q); fgetc(f); SV sv[n],s[n]; for(int i=0;i<n;i++) { fscanf(f,"%s%d%s%s",(sv+i)->ma,&(sv+i)->gpa,(sv+i)->name,(sv+i)->que); fgetc(f); } fclose(f); int d=0; for(int i=0;i<n;i++) if(sv[i].gpa>=dc&&strcmp(q,sv[i].que)==0) s[d++]=sv[i]; f=fopen("bai5.out","w"); fprintf(f,"Tinh: %s\n",q); fprintf(f,"Tong so thi sinh trung tuyen: %d\n",d);
for(int i=0;i<d;i++)
fprintf(f,"%s %d %s\n",s[i].ma,s[i].gpa,s[i].name);
fclose(f);
} //// //TSCT p2 dslk
include<stdio.h>
include<stdlib.h>
include<string.h>
typedef struct NO { char msv,ht,qq; int d; struct NO *nx; } NO; NO newNO() { NO temp = (NO)malloc(sizeof(NO)); temp->nx=NULL; return temp; } char* schar(FILE f) { char *s=(char)malloc(100*sizeof(char)); fscanf(f,"%s",s); return s; } void heNO(NO *he,NO *fo,int n) { //Chém thêm gì thì chém } void foNO(NO *he,NO *fo,char *msv,int d,char *ht,char *qq) { NO *temp = newNO(); temp->msv=msv; temp->d=d; temp->ht=ht; temp->qq=qq; if(he->nx == NULL) { he->nx = temp; fo->nx = temp; return; } fo->nx->nx =temp; fo->nx=fo->nx->nx; } void pNO(FILE *f,NO *he) { while(he->nx!=NULL) { he=he->nx; fprintf(f,"%s %d %s %s\n",he->msv,he->d,he->ht,he->qq); } } int main() { FILE *f; f=fopen("bai5.inp","r"); int n,dc; fscanf(f,"%d%d",&n,&dc); char *tinh; tinh=schar(f);
int d,sl=0;
char *msv,*ht,*qq;
NO *he=newNO(),*fo=newNO();
for(int i=0; i<n; i++)
{
msv=schar(f);
fscanf(f,"%d",&d);
ht=schar(f);
qq=schar(f);
if(d>=dc&&strcmp(qq,tinh)==0)
{
foNO(he,fo,msv,d,ht,qq);
sl++;
}
// else // { // free(msv); // free(ht); // free(qq); // } } fclose(f);
f=fopen("bai5.out","w");
fprintf(f,"Tinh: %s\n",tinh);
fprintf(f,"Tong so thi sinh trung tuyen: %d\n",sl);
pNO(f,he);
fclose(f);
} //trung-hau
include <stdio.h>
include <stdlib.h>
include <ctype.h> // For isdigit() and isalpha()
include <string.h> // For strlen()
define MAX 100
// Stack structure struct Stack { int top; char items[MAX]; };
// Initialize the stack void initStack(struct Stack *s) { s->top = -1; }
// Check if the stack is empty int isEmpty(struct Stack *s) { return s->top == -1; }
// Check if the stack is full int isFull(struct Stack *s) { return s->top == MAX - 1; }
// Push element into the stack void push(struct Stack *s, char value) { if (isFull(s)) { printf("Stack Overflow\n"); return; } s->items[++(s->top)] = value; }
// Pop element from the stack char pop(struct Stack *s) { if (isEmpty(s)) { printf("Stack Underflow\n"); return -1; } return s->items[(s->top)--]; }
// Peek the top element of the stack char peek(struct Stack *s) { if (isEmpty(s)) { return -1; } return s->items[s->top]; }
// Function to determine precedence of operators int precedence(char op) { if (op == '+' || op == '-') { return 1; } if (op == '*' || op == '/') { return 2; } return 0; }
// Function to convert infix to postfix void infixToPostfix(char* infix, char* postfix) { struct Stack stack; initStack(&stack);
int i, j = 0;
for (i = 0; i < strlen(infix); i++) {
char token = infix[i];
// If the token is an operand, add it to output
if (isalnum(token)) {
postfix[j++] = token;
}
// If the token is '(', push it to the stack
else if (token == '(') {
push(&stack, token);
}
// If the token is ')', pop and output from the stack until '(' is found
else if (token == ')') {
while (!isEmpty(&stack) && peek(&stack) != '(') {
postfix[j++] = pop(&stack);
}
pop(&stack); // Pop '(' from the stack
}
// If the token is an operator
else {
while (!isEmpty(&stack) && precedence(peek(&stack)) >= precedence(token)) {
postfix[j++] = pop(&stack);
}
push(&stack, token);
}
}
// Pop all remaining operators from the stack
while (!isEmpty(&stack)) {
postfix[j++] = pop(&stack);
}
postfix[j] = '\0'; // Null-terminate the postfix expression
}
int main() { char infix[MAX], postfix[MAX];
printf("Enter an infix expression: ");
scanf("%s", infix);
infixToPostfix(infix, postfix);
printf("Postfix expression: %s\n", postfix);
return 0;
} ////