Get the App
SLTechnology News&Howtos  ›  Development  › 

How does C++ realize single-linked list inversion and then interleaving rejoin and sparse matrix

Shulou Source: shulou.com Published: 2022-06-02 03:31:56 09月13日 Update

This article mainly explains "how C++ implements single-linked list reversal and then staggered reconnection and sparse matrix", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "C++ how to achieve single-linked list inversion and then staggered rejoin and sparse matrix" bar!

/ * 1 leetcode title 1 2 3 4 n 1 n color 1 2 n color 2 3 N color 3 * / # include#include#include// container-- Class template # include// uses the random value # includeusing namespace std; # define N 1000 # define K 100 typedef struct node {int x; node * next Public: node (): X (1), next (NULL) {} node (int a): X (a), next (NULL) {} node;int xx [] = {0phae7, 5, 5, 4, 2, 2, 11, 12, 0, int, 12, 0, void linkcreat (node*head) {if (head==NULL) {head=new node;} head- > x=xx, while (inext=head- > next;head- > next=add;} void show (node*head) {node* pendant head; while (p) {coutnext=q } void V (node * & head,int k) {node * newhead=head;node * paired headforth node * qroomHeadock node * t = NULL; while (KMub -) {qipp; pamphp-> next; Q-> next=t; tfolq;} head=q; newhead- > next=p;} void VV (node * & head) {/ / Fast and slow pointer to find the intermediate node node * pendant headlocator node * q=head While (p-> next- > nextlinks null) {qq=p; pins-> next- > next; qlocq-> next;} coutnextframes null) {qq=p; pads-> next; qq- > next=t; tweeqs;} p-> next=qq; Q-> next=p; / / merge node * pp=head; Q-> next=NULL; while (p-> nextlinks null) {tasking packs-> next; t-> next=pp- > next; pp- > next=t from new links Pp=pp- > next- > next;} Q-> next=p;} int main () {node * head=new node (1); linkcreat (head); show (head); VV (head); show (head);} / * # include "wz.h" template struct element {int row, col; / / rows, columns T item; / / element}; const int MaxTerm=100 Template class SparseMatrix {public: SparseMatrix () {}; SparseMatrix (int intmu,int intnu,int inttu,element datatemp []); / / Parametric constructor to initialize sparse matrix ~ SparseMatrix () {}; / / destructor to free storage space element GetMatrix (int intnumber) / / output the array element void Prt () corresponding to the subscript; / / display the triple sequence table void Trans1 (SparseMatrix & B); / / the matrix transposition algorithm void Trans2 (SparseMatrix A, SparseMatrix & B) with direct fetch and sequential storage; / / the matrix transpose algorithm private with sequential fetch and direct storage: private: element data [MaxTerm] / / Matrix non-zero elements intmu, nu, tu; / / number of rows, columns, number of non-zero elements}; # endiftemplate SparseMatrix::SparseMatrix (int intmu,int intnu,int inttu,element datatemp []) {if (inttu > MaxTerm) throw "incorrect initialization parameters of the constructor"; mu = intmu;nu = intnu;tu = inttu; for (int item0 | | intnumber < 0) throw "incorrect input position" Return data [I];} template void SparseMatrix::Prt () {for (int item0)

Tags: Ternary order element matrix number sparse subscript algorithm cantilever interleaving function location content array storage learning utility deeper interest parameter Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Technology Docker vpn Redmi