链表排序
来源:互联网 发布:呼伦贝尔盟616事件知乎 编辑:程序博客网 时间:2024/05/22 04:35
准备面试中,准备一些面试题。
其中比较经典的如:链表排序
代码如下:
#include <iostream>using namespace std;
/*创建节点类*/class node{public :int data;node* next;public :node(int data = -1){this->data = data;next = NULL;}};
/*创建列表*/class List{private :node* head;public :List(){head = new node(-1);if (head == NULL){printf("构造失败\n");}}void insertList(int data);void display();void sort();};/*交换节点数据(如果想交换节点,只需修改部分代码)*/void List::sort(){node* p = NULL;node* q = NULL;int temp = 0;for (p = head->next;p != NULL;p=p->next){/*有人知道下面两句的区别吗?运行看看,完全不同的结果*/ //for (q=head->next;q!=NULL;q=q->next)for (q = p->next;q != NULL;q=q->next){if (p->data > q->data){temp = q->data;q->data = p->data;p->data = temp;}}}}
/*插入链表数据*/void List::insertList(int data){if (data < 0 ){return ;}node* temp = new node(data);if (temp == NULL){printf("分配空间失败\n");return ;}temp->next = head->next;head->next = temp;}
/*链表的打印*/void List::display(){if (head == NULL){printf("List is empty!");}node* temp = head->next;while (temp != NULL){cout<<temp->data;temp = temp->next;}}int main(void){List a;a.insertList(6);a.insertList(7);a.insertList(3);a.insertList(4);a.insertList(1);a.insertList(0);a.insertList(2);a.insertList(5);a.insertList(8);a.insertList(9);a.sort();a.display();return 0;}
ps:欢迎大家指教!
- 链表排序 归并排序
- 链表排序--选择排序
- 链表排序--快速排序
- 链表排序--归并排序
- 排序3:插入排序(普通插入排序 + 折半插入排序 + 链表插入排序 + 希尔排序)
- LIST排序, Struct排序, qsort排序, STL::LIST sort排序, 链表排序
- 单向链表排序:快速排序和归并排序
- 单向链表排序-归并排序
- 链表排序,使用插入排序,
- 链表排序-直接插入排序
- 链表的一种排序---冒泡排序
- c 链表排序 - 选择排序
- 链表排序之插入排序
- 插入排序进行链表排序
- 优化冒泡排序 & 链表选择排序
- 链表排序之选择排序
- 链表排序之冒泡排序
- 链表排序(归并排序)
- android 双缓冲机制
- C++ 指针这一块的。
- 获取URL
- (*)hibernate映射文件配置
- Oracle设计表、批处理方式快速导入到数据库示例
- 链表排序
- mongodb查询的语法
- border的使用
- buffer cache 的学习
- Linux怎么进行Yum软件仓库配置
- Imperfect C++ 编译期约束 must_have_base
- 常用代码
- C# 匿名方法和拉姆达表达式
- ORA-00257