带头结点的单链表
实现了一些操作及快速排序,效率不高,需要改进。
单链表的快速排序的实现。
见博文:http://blog.csdn.net/hackbuteer1/article/details/6666475(懒得复制,请戳)
1、首次思路:首元素作为中轴,对后续节点一次遍历,小于者插入到中轴前,大于者,保持不动。操作对象:指针。
2、参考上面的博文,指针不动,进行值交换。操作对象:数值。
CODE1:
见CODE最后
CODE2:
void qSortSList(ListNode *pbeg, ListNode *pend) {
// 无元素
if (pbeg->next == pend) {
return ;
}
ListNode *pivot = pbeg->next;
// 仅一个元素
if (pivot->next == pend) {
return ;
}
ListNode *slow = pivot;
ListNode *fast = slow->next;
while (fast != pend) {
if (fast->data < pivot->data) {
slow = slow->next;
swap(fast, slow);
}
fast = fast->next;
}
swap(pivot, slow);
pivot = slow;
qSortSList(pbeg, pivot);
qSortSList(pivot, pend);
}
void swap(ListNode *p, ListNode *q) {
ElemType t = p->data;
p->data = q->data;
q->data = t;
}
CODE3:
CODE:C语言编译,不涉及C++
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct _ListNode{
ElemType data;
struct ListNode* next;
}ListNode;
ListNode * initSList();
int isEmpty(ListNode *head);
void printSList(ListNode *head);
int insertAtHead(ListNode *head, ElemType e);
int addToTail(ListNode *head, ElemType e);
void insertToOrderlySL(ListNode *head, ElemType e);
ListNode * getPtr(ListNode *head, int n);
int getSize(ListNode *head);
ListNode *findElem(ListNode *head, ElemType e);
ListNode *getTailPtr(ListNode *head);
void clearSList(ListNode *head);
ElemType rmTail(ListNode *head);
int delElemX(ListNode *head, ElemType e);
void delAllX(ListNode *head, ElemType e);
void destroySL(ListNode *head);
/* 申请一个节点 */
ListNode *mallocNode(ElemType e) {
ListNode *p = (ListNode *)malloc(sizeof(ListNode));
memset(p, 0, sizeof(ListNode));
p->data = e;
return p;
}
/* 新建并初始化一个链表 */
ListNode * initSList() {
// ListNode *head = (ListNode *)malloc(sizeof(ListNode));
// memset(head, 0, sizeof(ListNode));
return mallocNode(0);
}
/* 是否空 */
int isEmpty(ListNode *head) {
if (head->next == NULL) {
return 0;
} else {
return 1;
}
}
/* 头插法 */
int insertAtHead(ListNode *head, ElemType e) {
ListNode *p = mallocNode(e);
if (!p) {
return 0;
}
p->next = head->next;
head->next = p;
return 1;
}
/* 尾部插入一个元素 */
int addToTail(ListNode *head, ElemType e) {
ListNode *p = mallocNode(e);
if (!p) {
return 0;
}
ListNode *tail = head;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = p;
p->next = NULL;
return 1;
}
/* 输出元素 */
void printSList(ListNode *head) {
if (head->next == NULL) {
printf("List is Empty.\n");
}
ListNode *p = head->next;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
/* 在第n个位置插入元素 */
int insertAtN(ListNode *head, int n, ElemType e){
ListNode *pre = getPtr(head, n-1);
if (pre == NULL) {
return 0;
}
ListNode *p = mallocNode(e);
p->next = pre->next;
pre->next = p;
return 1;
}
/* 获得第n给元素的指针 */
ListNode * getPtr(ListNode *head, int n) {
if (n <= 0 ) {
return NULL;
}
ListNode *p = head->next;
int cur = 1;
while (p && cur < n) {
p = p->next;
++cur;
}
// if (p) {
// return p;
// } else {
// return NULL;
// }
return p;
}
/* 获取大小 */
int getSize(ListNode *head) {
head = head->next;
int size = 0;
while (head) {
++size;
head = head->next;
}
return size;
}
/* 清空链表 */
void clearSList(ListNode *head) {
ListNode *p = head->next, *q;
head->next = NULL;
while (p) {
q = p->next;
free(p);
p = q;
}
}
/* 查找元素 */
ListNode *findElem(ListNode *head, ElemType e) {
ListNode *p = head->next;
while (p) {
if (p->data == e) {
return p;
}
p = p->next;
}
return NULL;
}
/* 向有序单链表中插入元素x结点,使得插入后仍然有序 */
void insertToOrderlySL(ListNode *head, ElemType e) {
// default: from small to large
ListNode *p = head;
ListNode *q = p->next;
while (q && q->data < e) {
p = q;
q = p->next;
}
ListNode *t = mallocNode(e);
t->next = p->next;
p->next = t;
}
/* 获取尾元素指针 */
ListNode *getTailPtr(ListNode *head) {
ListNode *tail = head;
while (tail->next != NULL) {
tail = tail->next;
}
return tail;
}
/* 从单链表中删除表尾结点并返回它的值 */
ElemType rmTail(ListNode *head) {
ListNode *pretail = head;
ListNode *tail = pretail->next;
while (tail->next) {
pretail = pretail->next;
tail = tail->next;
}
ElemType tailval = tail->data;
pretail->next = NULL;
free(tail);
return tailval;
}
/* 从单链表中删除值为x的第一个结点 */
int delElemX(ListNode *head, ElemType e) {
ListNode *pre = head;
ListNode *p = pre->next;
while (p) {
if (p->data == e) {
pre->next = p->next;
free(p);
return 1;
}
pre = pre->next;
p = pre->next;
}
return 0;
}
/* 从单链表中删除值为x的所有结点 */
void delAllX(ListNode *head, ElemType e) {
ListNode *pre = head;
ListNode *p = pre->next;
while (p) {
if (p->data == e) {
pre->next = p->next;
free(p);
} else {
pre = pre->next;
}
p = pre->next;
}
}
/* 快速排序 */
void qSortSList(ListNode *pbeg, ListNode *pend) {
// 无元素
if (pbeg->next == pend) {
return ;
}
ListNode *pivot = pbeg->next;
// 仅一个元素
if (pivot->next == pend) {
return ;
}
ListNode *pre = pivot;
ListNode *p = pre->next;
while(p != pend) {
if (p->data < pivot->data) {
pre->next = p->next;
p->next = pbeg->next;
pbeg->next = p;
} else {
pre = pre->next;
}
p = pre->next;
}
// printSList(pbeg);
qSortSList(pbeg, pivot);
qSortSList(pivot, pend);
}
/* 销毁链表 */
void destroySL(ListNode *head) {
clearSList(head);
free(head);
}
int main()
{
ListNode *head = initSList();
// test qsort
insertAtHead(head, 4);
insertAtHead(head, 1);
insertAtHead(head, 8);
insertAtHead(head, 3);
insertAtHead(head, 2);
insertAtHead(head, 5);
qSortSList(head, NULL);
printSList(head);
// insertAtHead(head, 2);
// insertAtHead(head, 1);
// printSList(head);
// addToTail(head, 3);
// addToTail(head, 4);
// printSList(head);
// printf("size = %d\n", getSize(head));
// ListNode *p = getPtr(head, 3);
// printf("%d\n", p->data);
// p = findElem(head, 4);
// printf("%d\n", p->data);
clearSList(head);
// insertToOrderlySL(head, 5);
// insertToOrderlySL(head, 2);
// printSList(head);
// p = getTailPtr(head);
// printf("%d\n", p->data);
// printf("%d\n", rmTail(head));
// delElemX(head, 3);
// printSList(head);
// delAllX(head, 2);
// printSList(head);
//
// clearSList(head);
// printSList(head);
destroySL(head);
return 0;
}