Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report