Delete and invert "1234ssc.com" in C + + for linked list

Source: Internet
Author: User

On the operation of linked list in MFC project

1. For a given integer n, the write algorithm deletes the nth node in the list, and the first node of the list is pointed to by a.

Since there is no standard header file for node in C + +, to define the node class manually, only the simple data and next pointers and the contents of the destructor section are defined here:

 1 class node 2 {3 public:4 node (const int &in,node *nextnode =virtual ~node ();  6 Node*next; 7 int Data; 8 9};             

You can define a node after #include "Node.h".

1 node *p1,*p2,*P3;  2     p1=new node (1);  3 p2=new node (2, p1);  4 p3=new node (3,p2);           

The list of links defined here is shown below:

P3-->p2-->p1

3-->2-->1

About the definition of the Erasevalue function:

  Emperor Prestige platform "q:737888396"   Emperor Prestige Platform "q:737888396"   Emperor Prestige Platform "q:737888396"   Prestige platform "q:737888396"   Emperor Prestige platform "q:737888396"   Emperor Prestige Platform "q:737888396"   Emperor Prestige Platform "q:737888396"   Emperor Prestige Platform "q:737888396"   Emperor Prestige platform "Q : 737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige platform" q:737888396 "  Prestige platform" q:737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige platform "q:737888396"   Emperor Prestige Platform "q:737888396"   Emperor Prestige Platform "q:737888396"   Emperor Prestige Platform "q:737888396"   Emperor Prestige platform "Q : 737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige platform" q:737888396 "  Prestige platform" q:737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige platform "q:737888396"   Emperor Prestige Platform "q:737888396"   Emperor Prestige Platform "q:737888396"   Emperor Prestige Platform "q:737888396"   Emperor Prestige platform "Q : 737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige Platform" q:737888396 "  Emperor Prestige platform" q:737888396 "  Prestige platform" q:737888396 "  Emperor Prestige platform "q:737888396"   Emperor Prestige Platform "q:737888396"      

1 Template <typename t>2void Erasevalue (Node*first,Const t&N3{4 node *curr=first,*pre=NULL;5if (n==1)6{7 first=first->Next8Delete Curr;}10 else {for (int i=1;i <n;i++)  {12 pre=curr; Curr=curr->next; }15 16 pre->next= Curr->next;  delete curr;} 18 19}        

function call and output (change):

When the n=1 will be error, to be resolved.

1 node *cur=P3;2 3  CString temp,str; 4 null)  5  { 6 temp. Format ( "%d ",cur-> data);  7 Str+=temp; 8 cur=cur- >next; 9 }10 AfxMessageBox (str);               

2. Write an algorithm to reverse the list, do not copy the list elements, but instead reset the links and pointers, so that first points to the original last node, and all links between the nodes are reversed.

Test without output:

1 Template <typename t>2void reverse (Node*first,Const t&N3{4 node *front=NULL;5Forint i=0;i<n-1;i++)6{7 Node *curr=first,*pre=NULL;8while (curr->next!=NULL)9{Ten pre=Curr11 curr=curr->next;13 }14 if (I==0&&curr->next==null) Front=curr;< Span style= "color: #008080;" >15 pre->next=null; Curr->next=pre; }18 if (I=n-1) First->next=front; Front=first;                 

Beginner C + + and data structure, there are errors please even point out , thank you!!

Delete and invert "1234ssc.com" in C + + for linked list

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.