Mọi người có thể xem hộ code của em phần quickSort bị lỗi gì ạ. Em cảm ơn
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef int ElementType;
struct node {
ElementType data;
struct node* next;
};
typedef struct node* NODE;
struct list {
NODE head;
NODE tail;
};
typedef struct list* LIST;
void creatList(LIST &l) {
l->head = NULL;
l->tail = NULL;
}
NODE creatNode(ElementType x) {
NODE p = new node;
p->data = x;
p->next = NULL;
return p;
}
NODE search(LIST l, ElementType x) {
NODE p = l->head;
//NODE temp = new node;
while (p != NULL) {
if (p->next->data >= x)
return p;
p = p->next;
}
}
void addHead(LIST &l, NODE p) {
if (l->head == NULL) {
l->head = l->tail = p;
}
else {
p->next = l->head;
l->head = p;
}
}
void addTail(LIST &l, NODE p) {
if (l->head == NULL) {
l->head = l->tail = p;
}
else {
l->tail->next = p;
l->tail = p;
}
}
void addNode (NODE q, NODE p) {
p->next = q->next;
q->next = p;
}
void quickSort(LIST &l) {
LIST l1;
LIST l2;
NODE tag;
NODE p;
if (l->head == l->tail) return;
creatList(l1);
creatList(l2);
tag = l->head;
l->head = l->head->next;
tag->next = NULL;
while (l->head != NULL) {
p = l->head;
l->head = l->head->next;
p->next = NULL;
if (p->data <= tag->data) addHead(l1,p);
else addHead(l2, p);
}
quickSort(l1);
quickSort(l2);
if (l1->head != NULL) {
l->head = l1->head;
l1->tail->next = tag;
}
else l->head = tag;
tag->next = l2->head;
if (l2->head != NULL) l->tail = l2->tail;
else l->tail = tag;
}
void printList(LIST l) {
while (l->head != NULL) {
printf("\t%d", l->head->data);
l->head = l->head->next;
}
}
int main() {
int i, n, choice;
ElementType x;
LIST l = new list ;
creatList(l);
printf("Nhap so phan tu cua danh sach: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("Nhap phan tu muon them: ");
scanf("%d", &x);
NODE p = creatNode(x);
addHead(l, p);
}
quickSort(l);
printList(l);
}