链表小结
来源:互联网 发布:新顶级域名 注册 编辑:程序博客网 时间:2024/06/04 00:43
一:顺序表和链表的优缺点,他们各自在什么环境下使用
顺序表优点:
1:顺序表支持随机访问 2:顺序表在不增容的情况下,尾插的效率相对单链表效率要高 3:顺序表的cpu高速缓存利用率高于链表
顺序表缺点:
1:增容时代价很大:2:头插中间插入效率教低
链表的缺点:
不支持随机访问
二:从尾到头打印单链表:
void PrintTailToHead(Node *p)
{
if (p == NULL)
{
return ;
}
PrintTailToHead(p->next);
printf("%d",p->data);
}
三:删除一个无头单链表的非尾节点
四.在无头单链表的一个节点前插入一个节点
五.单链表实现约瑟夫环
Node* Josephus(Node** p , size_t k)
{
Node* cur, *next;
assert(*p);
cur = *p;
while ((*p)->next != *p)
{
size_t count = k;
while (--count)
{
cur = cur->next;
}
next = cur->next;
cur->data = next->data;
cur->next = next->next;
free(next);
}
return *p;
}
六.逆置/反转单链表
逆置/反转单链表
void reverse (Node** pp)
{
Node* cur = *p;
Node* newhead = NULL;
if (*p ==NULL ) || ((*p)->next == NUL)
{
return 0;
}
else
{
while (cur)
{
Node* next = cur->next;
cur->next = newhead;
newhead = cur;
cur = next;
}
*pp = newhead;
}
}
void reverse (Node** pp)
{
Node* n0 = NULL;
Node* n1 = *pp;
Node* n2 = n1->next;
if (*p ==NULL ) || ((*p)->next == NULL)
{
return 0;
}
else
{
while (n1)
{
n1->next = n0;
n0 = n1;
n1 = n2;
if (n2 != NULL)
{
n2 = n2->next;
}
}
*pp = no;
}
}
七.单链表排序(冒泡排序&快速排序)
单链表排序 (冒泡排序)
冒泡 一定是 只改变 其数据域 不改变其 指针域/*
void SeqListSort(Node* pHead)
{
Node* tail = NULL;
int pos = 0;
while (tail != pHead)
{
Node* cur = pHead;
Node* next = cur->next;
while (next != tail)
{
if ((cur->data) > (next->data))
{
DataType tmp = cur->data;
cur->data = next->data;
next->data = tmp;
pos = 1;
}
cur = cur->next;
next = next->next;
}
if (pos == 0)
{
break;
}
tali = cur;
}
}*/
八.合并两个有序链表,合并后依然有序
void MergeList(Node* p, Node* m)
{
Node* newhead; *tail;
if (p == NULL)
return m;
if (m == NULL)
return p;
if (p->data < m->data)
{
newhead = p;
p = ->next
}
else
{
newhead = m;
m = m->next;
}
tail = newhead;
while (p && m)
{
if (p->data < m->data)
{
newhead->next = p;
p = p->next;
}
else
{
newhead->next = m;
m = m->next;
}
tail = tail->next;
}
if (p)
{
tail->next = m;
}
else
{
tail->next = p;
}
}
十.查找单链表的倒数第k个节点,要求只能遍历一次链表
void FindMid(Node* p)
{
if (p == NULL)
{
return ;
}
Node* fast = p;
Node* slow = p;
while (fast->next && fast)
{
fast = fast->next->next;
slow = slow->next;
}
return slow;
}
Node * FindTailNode(Node* p, size_t k)
{
Node* fast = p;
Node* slow = p;
while (--k)
{
if (fast == NULL)
return NULL;
fast = fast->next;
}
while (fast->next)
{
fast = fast->next;
slow = slow->next;
}
return slow;
}
- 链表逆序小结
- 链表小结
- 链表题目小结
- 链表的小结
- 链表部分小结
- 链表小结
- 链表小结
- 之线性链表小结
- 双向循环链表小结
- 链表算法题小结
- 数据结构学习之链表小结
- 数据结构入门小结之链表
- 链表和数组的区别小结
- 数据结构链表小结(一)
- 数据结构之静态链表小结
- c语言之链表学习小结
- 数据结构—链表的操作小结
- 链表面试题小结
- Leetcode 547
- 一道简单的题目
- html——javasprit脚本之DOM
- VMWare虚拟机无法连接上网的解决
- 来自风平浪静的明天 (记忆化搜索 dp)
- 链表小结
- SQL注入原理讲解,很不错!
- Java下拉列表联动的实现(从数据库读取数据)
- 数据库笔记群集
- Servlet API中redirect(重定向)与forward(转发)/include(包含)的区别
- unity声音的管理
- android studio 上面查看data\data 目录权限修改
- opacity背景透明文字也透明?解决方法
- mac下虚拟机安装windows