Data structure and algorithm series (1)-implementation of a single-linked list class (C + +)

Source: Internet
Author: User

By defining a C + + class to encapsulate the data structure of a single linked list,

The encapsulation methods are:

1. Create a single linked list by typing;

2. Get the number of data elements in a single-linked list;

3. Print out the individual elements of the list;

4. Search for the position of an element in a single-linked list;

5. Insert a node after a position;

6. Delete a node in a location;

7. Single-linked list inversion;

8. Whether the single-linked list has a loopback judgment;

9. Single-linked list in ascending order;

10. The ascending merger of two single-linked lists;

11. Two single-linked lists are merged in descending order.

Note: A single-linked list is sorted using a fast-sorting method.

The following is a C + + written program code, attached to run.

#include <iostream> #include <malloc.h> #include <math.h>using namespace std;typedef struct node// Node type {int data;//data field node* next;//pointer field}node;class linklist//single-linked list class definition {public:linklist (int n=0)//But parameter constructor, Generates a linked list with N elements (excluding header nodes) {createlist (n); Len=getlength ();} node* head;//head node pointer int len;//element number public:void createlist (int n);//create single-linked list int getlength () based on input;//Gets the length of the single-linked list (number of elements) void Printlist ();//print single-linked list of individual elements int searchnode (int data);//Find the position of an element in a single-linked list, but does not exist, returns 0. BOOL Insertnode (int pos,int data);//Insert a node bool Deletenode (int pos) with the value data after POS position;//delete the first element after the POS position, delete the successful return true.void Reverse ();//The single-linked list is inverted bool Isloop ();//The existence of a single linked list will be replaced, the existence of return is true, there is no return falsevoid selfasorder ();//The list of elements in ascending order void Ascmerge ( linklist& l);//With the other list ascending merge void Descmerge (linklist& l);//with L list descending private:void quiksort (node* start,node* end);}; void linklist::createlist (int n)//create single-linked list from input {head = new Node;head->data=0;head->next=null;node *p,*q;int temp;q= Head;while (n--) {cin>>temp;p=new node;p->data=temp;p->next=null;q->next=p;q=p;p=NULL;} Q=null;delete Q;} void linklist::P rintlist ()//print single-linked list of each element {len=getlength (); int l=len;node* P=head->next;while (l--) {cout<<p- >data<< "";p =p->next;} P=null;delete P;cout<<endl;} int linklist::getlength ()//Gets the length of the single-linked list {int l=0;node* p=head;while ((p->next)!=null) {l++;p =p->next;} return l;} int linklist::searchnode (int data)//finds the position of an element in a single-linked list, returns 0{int Locate=0;node *p=head->next;while (len--) If no such element exists { if (p->data==data) {locate=10-len;break;} P=p->next;} return locate;} BOOL Linklist::insertnode (int pos,int data)//Insert Node {len=getlength (); if (Pos>len)//Insert position out of range return false;node* p= Head->next;if (0==pos) {p=head;node* q = new node;q->data=data;q->next=p->next;p->next=q;p=null;q= Null;delete p;delete Q;len++;return true;} Else{pos=pos-1;while (pos--) {p=p->next;} node* q = new Node;q->data=data;q->next=p->next;p->next=q;p=null;q=null;delete p;delete Q;len++;return true;}} BOOL linklist::D eletenode (int pos) {len=getlength (); if (Pos>=len) RETUrn false;node* P=head;pos=pos-1;while (pos--) {p=p->next;} node* q=p->next;p->next=p->next->next;delete Q;q=null;len++;return true;} void Linklist::reverse () {node *p,*q,*r;if (head->next==null) {return;} P=head->next;q=p->next;p->next=null;while (q!=null) {r=q->next;q->next=p;p=q;q=r;} Head->next=p;} BOOL Linklist::isloop () {node *p=head->next;node* q=head->next->next;while (q!=null) && (p!=q)) {p= P->next;q=q->next->next;} if (p==q) return True;elsereturn false;} void Linklist::selfasorder ()//ascending, using a quick Sort method {node* start,*end;int len=getlength ();start=head->next;end=start; while ((End->next)!=null) end=end->next; Quiksort (start,end);} void Linklist::quiksort (node* start,node*end) {if (start==end) return;int len=0;node* st=start;node* en=end;while (st!= EN) {st=st->next; len++;} Double x=len/2.0;double Xl=floor (x);d ouble xu=ceil (x);d ouble halflen=x;int l=xl;int u=xu;node* mid=start;node* midl= Start;while (u--) {mid=mid->next;} while (l--) {Midl=midL->next;} node* s=start;node* m=mid;int flag=0;for (int i=0;i<xl+1;++i) {flag=midl-start+1;if (S->data) > (m->data)) {s->data^=m->data;//Exchange m->data^= (S->data); s->data^= (m->data);} S=s->next;m=m->next;} Quiksort (START,MIDL); Quiksort (mid,end);} void Linklist::ascmerge (linklist& L) {this->selfasorder (); L.selfasorder (); node* q= (l.head)->next;node* p=head->next;int position=0;while ((P!=NULL) && (Q!=NULL) {if (q->data<p->data) {insertnode (position,q->data); q=q->next;position++;} else{p=p->next;position++;}} Position=getlength (); while (Q!=null) {insertnode (position,q->data); q=q->next;position++;}} void linklist::D escmerge (linklist& l) {ascmerge (L); Reverse ();} int main () {linklist L (Ten);cout<< "Input the L1 List:" <<endl; Linklist L1 (15); L.printlist ();cout<<endl<< "Input The data to search:" <<endl;int datasearch=0;cin>> Datasearch;cout<<l.searchnode (Datasearch) <<endl;cout<< "Input tHe data to insert: "<<endl;int datainsert=0;cin>>datainsert;cout<<" Input, the position to insert: "< <endl;int posinsert=0;cin>>posinsert;if (L.insertnode (Posinsert,datainsert)) {cout<< "Insert successfully! The new list is: "; L.printlist ();} elsecout<< "Insert failed!" <<endl;cout<< "Input the position to delete:" <<endl;int posdel=0;cin>>posdel;if (L.deletenode (Posdel)) {cout<< "Delete successfully! The new list is: "; L.printlist ();} else{cout<< "Delete failed!" <<endl;} L.reverse ();cout<< "The new list after reversed is:"; L.printlist ();/*if (L.isloop ()) cout<< "There is a loop in the List" <<endl;elsecout<< "There are No loop in The list "<<endl;*/l.selfasorder ();cout<<" list after ascsorted: "; L.printlist (); Linklist L2; L2=l; L2. Ascmerge (L1);cout<< "L and L1 Ascmerge is:" <<endl; L2. Printlist (); Linklist L3; L3=l; L3. Descmerge (L1);cout<< "L and L1 Descmerge is:" <<endl; L3. PrintlisT (); return 0;} 
Run

Data structure and algorithm series (1)-implementation of a single-linked list class (C + +)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.