List.h
#include <iostream>typedefintDataType;structnode{Node ():d ATA (0), Pnextnode (NULL) {}//Node ConstructorsDataType data; Node*Pnextnode;};classlist{Private: Node*head;//the only head pointer as a linked list intSize//Linked list length Public: List () {head=NewNode;size=0;} BOOLIsEmpty ();//Determine if empty table BOOLInsertlist (intI,datatype elem);//I's indicator starts from 1, not starting from 0 voidPushlist (DataType elem);//add an element at the end of a linked list BOOLDeleteList (inti);//deletes the element at the specified position voidClearlist ();//clear entire linked list voidDeleterepetitivedata ();//Delete duplicate elements voidPrintlist ();//output linked list sequentially intGetSize (); Node* Fine (inti);//find the first node and return the pointer to the nodenode* Finepre (inti);//Find the node before the I node and return the pointer};
List.cpp
#include"List.h"#include<iostream>#include<vector>using namespacestd;//Judging Empty TablesBOOLList::isempty () {if(head==NULL)return false; Else return true;}//insert data at bit IBOOLList::insertlist (inti,datatype Elem) { if(i<1) return false; Else if(head==null| | i==1)//if it is an empty table{Head->data=Elem; Size++; return true; } Else if(i>size)//when a bit is larger than the linked list length, add it at the trailer{pushlist (elem); return true; } Else{Node*pre=fine (I-1); Node*follow=Fine (i); Node*s=NewNode;//request memory for a new nodepre->pnextnode=s;//connect the front node.s->pnextnode=follow;//Connection Post NodeS->data=Elem; Size++; return true; }}//add an element at the tailvoidList::P ushlist (DataType elem) {if(head==NULL) {Head=NewNode; Head->data=Elem; Size++; } Else{Node*cur=Head; while(cur->pnextnode) cur=cur->Pnextnode; Node*s=NewNode; Cur->pnextnode=s; S->data=Elem; Size++; }}//Print linked listvoidList::P rintlist () {Node*cur=Head; while(cur!=NULL) {cout<<cur->data<<" "; Cur=cur->Pnextnode; } cout<<Endl;}//sizeintlist::getsize () {returnsize;}//find the first node.node* List::fine (inti) { if(i==1) returnHead; Else{Node*cur=Head; for(intpos=1;p os<i;pos++) cur=cur->Pnextnode; returncur; } }//find the first node of Inode* List::finepre (inti) { if(i<2) {cout<<"parameter must be greater than or equal to 2! "<<Endl; returnNULL; } Else if(i==2) returnHead; Else returnFine (I-1);}//Delete Element IBOOLList::D eletelist (inti) { if(i<1|| I>size)//limit the range of I return false; Else if(i==1) {Node*temp=Head; Head=head->Pnextnode; Delete temp; Size--; return true; } Else{Node*cur= This-Fine (i); Node*pre= This-Finepre (i); Pre->pnextnode=cur->pnextnode;//Reconnect NodesDelete cur; Size--; return true; }}//empty the entire listvoidlist::clearlist () {Node*temp=Head; while(head!=NULL) {Temp=Head; Head=head->Pnextnode; Delete temp; } size=0;}//Delete duplicate elementsvoidList::D eleterepetitivedata () {intflag=0;//code run flag, 0 for not running if(size==1)//jump out when there is only one element return; Node*stand=Head; after the end of the loop or after the same data is found, it will point to the next node. Node*cursor;//Cursorsvector<int>Storge; for(intI=0; i<size;i++) { //use for loop because you want to get the position of the deleted element//the time complexity of the algorithm is O (n^2) using the handshake-based comparisonCursor=stand->Pnextnode; Flag=0; while(cursor!=NULL) { if(stand->data==cursor->data) {Stand=stand->Pnextnode; Flag=1; //Save the subscript of duplicate data in descending orderStorge.insert (Storge.begin (), i+1); Break; } Elsecursor=cursor->Pnextnode; } if(!flag) Stand=stand->Pnextnode; } intitemp=storge.size (); while(itemp--) { This->deletelist (storge.at (0));//extract the subscript and release the excess nodes.storge.erase (Storge.begin ()); } storge.clear (); //release vector memory }
Main.cpp
#include"List.h"#include<iostream>using namespacestd;voidMain () {//List class test codelist List; List. Insertlist (1,1); List. Insertlist (5,3); List. Insertlist (2,2); List. Insertlist (4,4); List. Printlist (); //Print these 4 elementscout<<"the length of the linked list is:"<<list. GetSize () <<Endl; cout<<"now delete the 2nd element"<<Endl; List. DeleteList (2); List. Printlist (); cout<<"now empty the list ."<<Endl; List. Clearlist (); //Empty Listcout<<"Output Empty table:"<<Endl; List. Printlist (); List. Pushlist (4); List. Pushlist (1); List. Pushlist (7); List. Pushlist (1); List. Pushlist (1); List. Pushlist (2); List. Pushlist (2);//Press in 4 1 7 1 1 2 2cout<<"the newly added elements of the output list:"<<Endl; List. Printlist (); cout<<"after deleting the same element, the linked list is:"<<Endl; List. Deleterepetitivedata (); List. Printlist ();}/*Summary: 1. List classes should first write fine () and Findpre (), then insert and delete 2. The structure of the list should add a subscript to each node. Since the subscript is added in the operation, the element is not uniform with the subscript, and if any elements are deleted, the subscript will be reassigned.
This leadsvoid List::D eleterepetitivedata () not available
A relatively straightforward approach is to delete data when duplicate data is detected and vector
Indirect deletion
*/
C + + linked list to delete duplicate data