面试题五 C/C++面试秘笈 之链表的正向排序--程序员面试题
2016-02-25 15:48
465 查看
/**
*面试题9
*单链表的正向排序
*/
typedef struct linkListSort{
int data;
linkListSort * next;
}linkListSort;
linkListSort * insert_sort(void)
{
structlinkListSort * head =
NULL,*New,*cur,*pre;
int data =0;
while (1) {
printf("please input the data:\n");
cin>>data;
if (data ==0) {//如果是0,结束
break;
}
New = (structlinkListSort*)malloc(sizeof(structlinkListSort));
New->data = data;
New->next =NULL;
if (head ==NULL) {
head = New;//在第一次循环时对head赋值
continue;
}
if (New->data <= head->data) {
//在head之前插入节点
New->next = head;
head = New;
continue;
}
cur = head;
if (New->data > cur->data && cur->next
!= NULL) { //找到需要插入的位置
pre = cur;
cur = cur->next;
}
if (cur->data >= New->data) {//把它插入到pre和cur之间
pre->next = New;
New->next = cur;
}else{
cur->next = New;
}
}
return head;
}
/**
*编程实现一个单链表的打印
*/
void linkListPrint(linkListSort *head)
{
int index =0;//总得个数
linkListSort *p;
if (head->next ==NULL) {//链表为空
cout<<"Link is empty!"<<endl;
return;
}
p = head->next;
while (p !=NULL) {//遍历链表
printf("the %dth is node:%d\n",++index,p->data);//打印链表元素
p = p->next;
}
}
如果有任何问题,欢迎下方留言谈论,或者加入QQ群83459374交流
*面试题9
*单链表的正向排序
*/
typedef struct linkListSort{
int data;
linkListSort * next;
}linkListSort;
linkListSort * insert_sort(void)
{
structlinkListSort * head =
NULL,*New,*cur,*pre;
int data =0;
while (1) {
printf("please input the data:\n");
cin>>data;
if (data ==0) {//如果是0,结束
break;
}
New = (structlinkListSort*)malloc(sizeof(structlinkListSort));
New->data = data;
New->next =NULL;
if (head ==NULL) {
head = New;//在第一次循环时对head赋值
continue;
}
if (New->data <= head->data) {
//在head之前插入节点
New->next = head;
head = New;
continue;
}
cur = head;
if (New->data > cur->data && cur->next
!= NULL) { //找到需要插入的位置
pre = cur;
cur = cur->next;
}
if (cur->data >= New->data) {//把它插入到pre和cur之间
pre->next = New;
New->next = cur;
}else{
cur->next = New;
}
}
return head;
}
/**
*编程实现一个单链表的打印
*/
void linkListPrint(linkListSort *head)
{
int index =0;//总得个数
linkListSort *p;
if (head->next ==NULL) {//链表为空
cout<<"Link is empty!"<<endl;
return;
}
p = head->next;
while (p !=NULL) {//遍历链表
printf("the %dth is node:%d\n",++index,p->data);//打印链表元素
p = p->next;
}
}
如果有任何问题,欢迎下方留言谈论,或者加入QQ群83459374交流
相关文章推荐
- 请问在VC++2010中如何连接用Access2010创建好的accdb数据库?
- [c++]string中删除相同字符
- C语言 链表的使用(链表的增删查改,链表逆转,链表排序)
- C++ template —— tuple(十三)
- 面试题四 C/C++面试秘笈 之判断链表是否存在环形链表问题-程序员面试题
- c++服务端用webservices【gsoap】做服务端与java后台对接信息的测试工具
- C++虚继承的概念
- 面试题三 C/C++面试秘笈 查找单链表的中间元素 --程序员面试题
- c语言入门之项目4.3——利用for循环求1-1/2*2+1/2*2*2...-1/2*2*2*2到2的8次方
- winodws平台C++共享内存实现
- C++基础知识易错点总结(2)
- C语言知识整理(二)
- [C++]智能指针
- 面试题二 C/C++面试秘笈 之单链表逆转/反转
- AVL树的插入删除操作
- 本科教育忽略的黄金C++<5> 泛型算法-特定容器算法
- 字符串匹配
- c语言入门之项目4.1——利用for循环求1!+2!+3!...12!
- [C++11]_[初级]_[左值引用声明和右值引用声明]
- [C++11]_[初级]_[左值引用声明和右值引用声明]