博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基本的链表操作
阅读量:5940 次
发布时间:2019-06-19

本文共 3319 字,大约阅读时间需要 11 分钟。

链表排序

    二路归并(暂未实现)
    基本思想:维护一个队列,从头便利链表,找到一段按降序排列的链表段,一段一段的分开,然后将它们按顺序入队,每次从对头取出两个进行归并,归并后的结果入队。
 
View Code
#include 
using namespace std; struct node{
int d; node *next; node(int x){d = x;next = NULL;} }; //链表找中点 node *findminnode(node *head) {
if(head == NULL || head->next == NULL) return head; node *onestep = head; node *twostep = head; while(twostep->next != NULL && twostep->next->next != NULL){
onestep = onestep->next; twostep = twostep->next->next; } return onestep; } //两条单链表判断是否相交 bool judgexiangjiao(node *head1,node *head2) {
if(head1 == NULL || head2 == NULL) return 0; node *p1 = head1; node *p2 = head2; while(p1->next != NULL) p1 = p1->next; while(p2->next != NULL) p2 = p2->next; if(p1 == p2) return 1; return 0; } //单链表判断有无环,并输出环的起始点 bool ishascycle(node *head,node *&onenode) {
if(head == NULL || head->next== NULL) return 0; node *onestep = head; node *twostep = head; while(twostep->next != NULL && twostep->next->next != NULL){
onestep = onestep->next; twostep = twostep->next->next; if(onestep == twostep){
onenode = onestep; return 1; } } return 0; } node * findfirstnode(node *head) {
node *onenode = NULL; bool isc = ishascycle(head,onenode); if(isc){
node * fromhead = head; node * fromonenode = onenode->next; int headcount = 0; int onecount = 0; while(fromhead != onenode){
fromhead=fromhead->next; headcount++; } while(fromonenode != onenode){
fromonenode = fromonenode->next; onecount++; } int cha = 0; node *fromlong = NULL; node *fromshort = NULL; if(headcount > onecount){
cha = headcount - onecount; fromlong = head; fromshort = onenode->next; }else{
cha = onecount - headcount; fromshort = head; fromlong = onenode->next; } while(cha--){
fromlong = fromlong->next; } while(fromlong != fromshort){
fromshort = fromshort->next; fromlong = fromlong->next; } return fromlong; } return NULL; } //链表逆序 node *reve(node *head) {
if(head == NULL ||head->next == NULL) return NULL; node *pre = NULL; node *cur = head; node *nex ; while(cur != NULL) {
nex = cur->next; cur->next = pre; pre = cur; cur = nex; } return pre; } int main() {
node *head = new node(1); node *n2 = new node(2); node *n3 = new node(3); node *n4 = new node(4); node *n5 = new node(5); node *n6 = new node(6); node *n7 = new node(7); node *n8 = new node(8); head->next = n2; n2->next = n3; n3->next = n4; n4->next = n5; n5->next = n6; n6->next = n7; n7->next = n8; cout<
d<
next = n3; node * jiao ; bool an = ishascycle(head,jiao); cout<
<
d<
next = NULL; node *p = reve(head); while(p){ cout<
d<<" "; p= p->next; } cout<

转载于:https://www.cnblogs.com/confide/archive/2012/03/09/2388190.html

你可能感兴趣的文章
设置Eclipse编码方式
查看>>
分布式系统唯一ID生成方案汇总【转】
查看>>
Cross-compilation using Clang
查看>>
营销系统--手动补偿
查看>>
图标字体设计
查看>>
【转】Principles of training multi-layer neural network using backpropagation
查看>>
并查集hdu1232
查看>>
改动Androidproject的名称(非Eclipse重命名)
查看>>
tomcat work目录的作用就是编译每个项目里的jsp文件为java文件如果项目没有jsp页面则这个项目文件夹为空...
查看>>
dedecms后台左侧菜单500错误怎么处理
查看>>
Maven配置将war包部署到Tomcat(tomcat7-maven-plugin)
查看>>
Spring MVC学习-------------訪问到静态的文件
查看>>
Unity应用架构设计(11)——一个网络层的构建
查看>>
运行自己的shell脚本
查看>>
内存错误的类别
查看>>
Authentication 方案优化探索(JWT, Session, Refresh Token, etc.)
查看>>
Struts2 关于返回type="chain"的用法.
查看>>
Maven私服安装及配置——(十二)
查看>>
设计模式 - 迭代器模式(iterator pattern) 具体解释
查看>>
Codeforces554B:Ohana Cleans Up
查看>>