C + + Stack learning--overloading of assignment arithmetic

Source: Internet
Author: User

For programming, has always been made of less, see more, this error learning method has been continued so far, so like operator overloading so simple things, or not very familiar, today by the opportunity to learn the stack, I wrote the link stack in the assignment operator overloaded function, after writing a comparison of the textbook ("Data structure C + + language description "Ren yan version", found that they really have a simple problem complication of the talent-writing procedures step by step, will not be optimized. By the way, I found a small error in the textbook and corrected it here.

    • Overloading of the stack assignment operator (self-written overloaded function)
      Explain your function: Want to implement whether the left value chain stack is not empty, can be the right value chain stack entire copy come over, so the overall idea is: first of all, the value of the public part of the two chain stack, then divided into two cases: (a) The number of left-value chain stack elements less than the number of right value chain stack; The number of left-value chain stack elements is greater than the number of right-value chain stack elements.
Template<TypeName Elemtype>Linkstack<Elemtype>Linkstack<Elemtype>:: operator =(Linkstack<Elemtype>Rights) {Nodepointer rs,ls,s,r,p; Rs=Rights.Top Ls=Top R=NULL;if(This!=&Rights) { while(LS!=NULL&&Rs!=NULL)//Assignment of the public part{ls -Data=Rs -Data; Rs=Rs -Next R=ls Ls=Ls -Next }The number of elements in the //Lvalue chain stack is less than the number of right value chain stack elements         while(LS==NULL&&Rs!=NULL) {s=New(Linknode); ASSERT (S!=0); S -Data=Rs -Data; S -Next=NULL; Rs=Rs -Next R -Next=S R=R -Next Ls=NULL; }The number of elements in the //lvalue chain stack is greater than the number of right value chain stack elements         while(LS!=NULL&&Rs==NULL) {P=ls R -Next=Ls -Next Ls=Ls -Next        Delete p; }    }return *this;}
    • Overload of the link assignment operator (data structure C + + language description)
      After reading the author's program, feel very ashamed, obviously can first empty the left value chain, and then according to a situation to copy it, why is not to think of it? Oh, stupid. The author's code is as follows:
Template<TypeName Elemtype>Linkstack<Elemtype>Linkstack<Elemtype>:: operator =(Linkstack<Elemtype>Rights) {Nodepointer P=NULL; Nodepointer RP=Rights.Top Nodepointer s;if(This!=&Rights) {clear (); while(RP) {s=New(Linknode); ASSERT (S!=0); S -Data=Rp -Data;if(!Top) {Top=S }Else{p -Next=S } p=S Rp=Rp -Next }if(p) P -Next=NULL; }return *this;}
    • Correcting an error
      However, there is a small error in the author's code that the pointer p is not initialized, and the following program is modified:
Template<TypeName Elemtype>Linkstack<Elemtype>Linkstack<Elemtype>:: operator =(Linkstack<Elemtype>Rights) {Nodepointer P=NULL; Nodepointer RP=Rights.Top Nodepointer s;if(This!=&Rights) {clear (); while(RP) {s=New(Linknode); ASSERT (S!=0); S -Data=Rp -Data;if(!Top) {Top=S P=Top/////////////////You add the pointer to the initialization section, otherwise the pointer doesn't know where to move.}Else{p -Next=S } p=S Rp=Rp -Next }if(p) P -Next=NULL; }return *this;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Stack learning--overloading of assignment arithmetic

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.