Implementation of a simple int-type C + + single-link list

Source: Internet
Author: User

IntSLList.h

//************************ intSLList.h **************************//singly-linked List class to store integers#ifndef int_linked_list#defineInt_linked_listclassIntsllnode { Public: Intsllnode () {Next=0; } intsllnode (intEl, Intsllnode *ptr =0) {Info= El; Next =ptr; }    intinfo; Intsllnode*next;};classIntsllist { Public: Intsllist () {head= Tail =0; }    ~intsllist (); intIsEmpty () {returnHead = =0; }    voidAddtohead (int); voidAddtotail (int); intDeletefromhead ();//Delete the head and return its info;    intDeletefromtail ();//Delete the tail and return its info;    voidDeletenode (int); BOOLIsinlist (int)Const; voidPrintall ()Const;Private: Intsllnode*head, *tail;};#endif

IntSLList.cpp

//************************ intSLList.cpp **************************#include<iostream>#include"intSLList.h"using namespacestd;intsllist::~intsllist () { for(Intsllnode *p;!IsEmpty ();) {P= head->Next; DeleteHead; Head=p; }}voidIntsllist::addtohead (intel) {Head=NewIntsllnode (el, head); if(Tail = =0) Tail=head;}voidIntsllist::addtotail (intel) {    if(Tail! =0) {//if list not empty;Tail->next =NewIntsllnode (EL); Tail= tail->Next; }    ElseHead = Tail =NewIntsllnode (EL);}intintsllist::d eletefromhead () {intEl = head->info; Intsllnode*tmp =Head; if(head = = tail)//If only one node is on the list;Head = Tail =0; ElseHead = head->Next; Deletetmp; returnel;}intintsllist::d eletefromtail () {intEl = tail->info; if(head = = tail) {//If only one node is on the list;        DeleteHead; Head= Tail =0; }    Else{//if more than one node in the list,Intsllnode *tmp;//find the predecessor of tail;         for(tmp = head; Tmp->next! = tail; tmp = tmp->next); Deletetail; Tail= tmp;//The predecessor of tail becomes tail;Tail->next =0; }    returnel;}voidIntsllist::d Eletenode (intel) {    if(Head! =0)//if Non-empty list;    if(head = = Tail && el = head->info) {//if only one        DeleteHead//node on the list;Head = Tail =0; }    Else if(el = = Head->info) {//if more than one node on the listIntsllnode *tmp =Head; Head= head->Next; Deletetmp//And old head are deleted;    }    Else{//if more than one node in the listIntsllnode *pred, *tmp;  for(pred = head, TMP = head->next;//and a non-head nodeTMP! =0&&! (Tmp->info = = EL);//is deleted;pred = pred->next, tmp = tmp->next); if(TMP! =0) {pred->next = tmp->Next; if(TMP = =tail) Tail=pred; Deletetmp; }    }}BOOLIntsllist::isinlist (intElConst{Intsllnode*tmp;  for(TMP = head; TMP! =0&&! (Tmp->info = = EL); TMP = tmp->next); returnTMP! =0;}voidIntsllist::p Rintall ()Const {     for(Intsllnode *tmp = head; TMP! =0; TMP = tmp->next) cout<< Tmp->info <<" "; cout<<Endl;}

Main.cpp

#include <iostream>#include"intSLList.h"using namespacestd;intMainintargcChar*argv[]) {    intend; Intsllist*list =Newintsllist (); List->addtohead ( -); List->addtohead ( $); List->addtohead ( -); List->addtohead ( -); List->addtohead ( -); List->addtohead (999); List-printall (); Deletelist; cout<<"-----------------------------------------------------------"<<Endl; Intsllist*list2 =Newintsllist (); List2->addtotail ( -); List2->addtotail ( $); List2->addtotail ( -); List2->addtotail ( -); List2->addtotail ( -); List2->addtotail (999); List2-printall (); DeleteList2; cout<<"Press any Key to Continue ..."<<Endl; CIN>>end; return 0;}

Operation Result:

999 500 400 300 200 100
-----------------------------------------------------------
100 200 300 400 500 999
Press any Key to Continue ...

Implementation of a simple int-type C + + single-link list

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.