链表的partition
来源:互联网 发布:mysql数据库清空所有 编辑:程序博客网 时间:2024/06/05 02:48
#include <iostream>#include <string>using namespace std;class Node {private: int item; Node *next;public: Node(); Node(int value); Node *getNext() const; void setNext(Node * nextPtr); int getItem() const; void setItem(int value);};Node::Node():item(0),next(nullptr){}Node::Node(int value):item(value),next(nullptr){}Node * Node::getNext() const{ return next;}void Node::setNext(Node * nextPtr){ next = nextPtr;}int Node::getItem() const{ return item;}void Node::setItem(int value){ item = value;}Node * partition(Node * headPtr,int x){ Node * beforStart = nullptr; Node * afterStart = nullptr; while(headPtr != nullptr) { Node * nextNodePtr = headPtr->getNext(); if(headPtr->getItem() < x) { headPtr->setNext(beforStart); beforStart = headPtr; } else { headPtr->setNext(afterStart); afterStart = headPtr; } headPtr = nextNodePtr; } if(beforStart == nullptr) return afterStart; Node * head = beforStart; while(head->getNext() != nullptr) head = head->getNext(); head->setNext(afterStart); return beforStart; }void printNodeChain(Node * headPtr){while(headPtr != nullptr){cout<<headPtr->getItem()<<" ";headPtr = headPtr->getNext();}cout<<endl;}int main(){int arr[6] = {3,8,9,1,2,4};Node * headPtr = nullptr;for(int i = 0; i < 6; i++){Node * nodePtr = new Node();nodePtr->setItem(arr[i]);nodePtr->setNext(headPtr);headPtr = nodePtr;}printNodeChain(headPtr);headPtr = partition(headPtr,5);printNodeChain(headPtr);return 0;}
0 0
- 链表的partition
- Partition List 链表的partition操作 分成2个链表
- 链表的分化(Partition一分为二)
- [LeetCode] Partition List(!!!!巧妙的链表插入)
- LeetCode 86. Partition List 链表的划分 Python Solution
- LeetCode | Partition List(partition方法处理链表)
- Partition List 分割链表
- Partition List 分离链表
- SQL表分区 partition的用法
- Partition List 链表划分区间@LeetCode
- 86. Partition List&链表分割
- LeetCode(Partition List) 划分链表
- Partition List 二分链表(重)
- Leetcode Partition List 分割链表
- LintCode Partition List 链表划分
- leetcode Partition List二分链表问题
- 86. Partition List(分割链表)--M
- LeetCode Partition List(链表分段)
- indent-C语言代码格式化工具
- 鼠标,控件,互动
- 一个小程序看代码规范
- 素数筛选【模板】
- C/C++代码格式规则中比较有争议的几处
- 链表的partition
- poj2411 状态压缩dp
- Xenomai user irq 示例
- apk反编译和重新打包
- 世界杯的中国元素——英利公司
- 最大熵模型学习笔记李航统计学习
- 解决windows端口被占用
- 微软爆史上最大规模裁员 七分之一员工被“炒”
- Eclipse中Outline里各种图标的含义