首页 > 其他分享 >11.17双向循环链表应用

11.17双向循环链表应用

时间:2023-11-21 23:01:24浏览次数:31  
标签:Node pp int 11.17 next 链表 prior 双向 NULL

 

#include<bits/stdc++.h>
using namespace std;
typedef struct f{
    int data;
    f* prior;
    f* next;
}node,*Node;
void build(Node p){
    int n;
    cin>>n;
    while(n--){
        int x;
        cin>>x;
        Node now=new node();
        now->data=x;
        now->prior=p;
        now->next=NULL;
        p->next=now;
        p=p->next;
    }
}

void show(Node p){
    p=p->next;
    while(p!=NULL){
        cout<<p->data;
        p=p->next;
    }
}
 void change(Node p){
    Node o=p;
    p=p->next;
    p=p->next;
    int x;
    cin>>x;
    while(p!=NULL&&p->data!=x){
        p=p->next;
    }
    if(p==NULL){
        cout<<"未找到"<<x;
    }
    else{//调换
        Node pp=p->prior,ep=pp->prior;
        Node a=pp->prior,b=pp,c=p,d=p->next;
        ep->next=p;
        pp->prior=c;
        pp->next=d;
        p->prior=a;
        p->next=b;
        show(o);
    }
}
 
int main(){
    Node p=new node();
    p->next=NULL;
     build(p);
    
     change(p);
    return 0;
}

先定义结构体包括data,prior,next;再new一个新链表build插入相应的数(尾插法);写一个change函数来查找输入的的数和其前一个数,如果没有找到输出“ cout<<"未找到"<<x;”,如果找到了交换两个数;最后输出交换后的链表。

标签:Node,pp,int,11.17,next,链表,prior,双向,NULL
From: https://www.cnblogs.com/aixin52129211/p/17847838.html

相关文章

  • 11.15链表逆置
     structListNode*reverse(structListNode*head){structListNode*L=(structListNode*)malloc(sizeof(structListNode)),*p,*q;L->next=NULL;p=head;//中间量while(p){q=(structListNode*)malloc(sizeof(structListNode));......
  • (链表)21-分隔链表
    /***Definitionforsingly-linkedlist.*publicclassListNode{*intval;*ListNodenext;*ListNode(){}*ListNode(intval){this.val=val;}*ListNode(intval,ListNodenext){this.val=val;this.next=next;}*}......
  • 面试题 02.07. 链表相交
    2023-11-21面试题02.07.链表相交-力扣(LeetCode)思路:1暴力法:判断的是next是不是相等1hashmap存储其中一个的全部,遍历另一个看是不是在map中(用set就行,不用map)2双指针:用2个指针分别遍历2链表(都是遍历完一个继续遍历另一个),最终会相等的,相等就是找到了暴力法:/***Defi......
  • 142. 环形链表 II
    2023-11-21142.环形链表II-力扣(LeetCode)思路:1hashmap:将其next一个个存入,直到找到next已经存在的,这里用set就行2快慢指针,一个一步步走,一个一次走2步,自己画一下图,其一定会在环中相遇,且一定是多走一圈,然后相遇时,将慢指针保留,继续走,重新定义一个指针从一开始走,他们相等时就......
  • 单链表建表,倒置,遍历(不使用Class,简洁易懂)
    在C++中通过递归方法实现单链表倒置初始化列表structListNode{ intval; LiseNode*next; ListNode(intx):val(x),next(NULL){}};遍历voidquery_node(){ node*p=head; while(p!=NULL){ cout<<p->data<<''; p=p->nxt; } cout<<endl;}建表(......
  • (链表)20-旋转链表
    1/**2*Definitionforsingly-linkedlist.3*publicclassListNode{4*intval;5*ListNodenext;6*ListNode(){}7*ListNode(intval){this.val=val;}8*ListNode(intval,ListNodenext){this.val=val;......
  • 19. 删除链表的倒数第 N 个结点
    2023-11-2019.删除链表的倒数第N个结点-力扣(LeetCode)思路:    1先遍历一遍,计算链表长度,再遍历一遍,完成    2双指针:先后指针,先走n步,再一起走    3栈,先全入栈,再出栈完成双指针:‘/***Definitionforsingly-linkedlist.*publicclass......
  • 面试必刷TOP101:30、 二叉搜索树与双向链表
    题目输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。如下图所示题解方法一:非递归版解题思路:1.核心是中序遍历的非递归算法。2.修改当前遍历节点与前一遍历节点的指针指向。importjava.util.Stack;publicTreeNodeConvertBSTToBiList(TreeNoderoot){......
  • (链表)18-随机链表的复制
    1/*2classNode{3intval;4Nodenext;5Noderandom;67publicNode(intval){8this.val=val;9this.next=null;10this.random=null;11}12}13*/1415classSolution{16public......
  • (链表)17-两两交换链表中的节点
    1/**2*Definitionforsingly-linkedlist.3*publicclassListNode{4*intval;5*ListNodenext;6*ListNode(){}7*ListNode(intval){this.val=val;}8*ListNode(intval,ListNodenext){this.val=val;......