Details about the transpose problem of the linked list

Source: Internet
Author: User

// Question: ------------------ transpose of the linked list ------------------------
// Requirements:
// Transpose the local link of the linked list. Is to put the data storage of the linked list upside down.
// Example:
// Input: 12345678910
// Output: 10987654321

// Algorithm analysis:
// The most important part of the program is to place each contact after the header contact before the header contact. Until the header contact arrives
// Tail point. Records the transpose of the linked list.
// Algorithm complexity analysis:
// Only the length of the chain table is the complexity of moving each contact forward. O (N );
// Program analysis:
// The C ++ implementation method adopted by the program; the class is used to implement each vertex. The usable space table is used in the program to speed up
// The speed at which the contacts are to be deleted and then generated in the program. Use C ++ to reload the new and delete functions. Static class members are also used.
// Data members are used to implement the available space tables.

# Include <iostream>
Using namespace STD;
// Implement the data connector class.
Template <class ELEM>
Class list
{
PRIVATE:
Static list <ELEM> * freelist; // static class member
Public:
ELEM data;
List * next;
List (ELEM dataval, list * nextval = NULL) // The constructor initializes the class at the same time.
{
Data = dataval;
Next = nextval;
}
List (list * nextval = NULL)
{
Next = nextval;
}
Void * operator new (size_t); // Declaration of the new overload function.
Void operator Delete (void *); // declaration of the Delete overload function.
~ List (){};
};

Template <class ELEM>
List <ELEM> * List <ELEM>: freelist = NULL; // initialization of static class data members.

// Implement the new overload function. Remember because it is a template function. Therefore, remember that the returned results do not point to any type.
// When you use the new function, it can point to the type you need again.
Template <class ELEM>
Void * List <ELEM>: Operator new (size_t)
{
If (freelist = NULL) // generate an available space table with a contact unit. You can also generate multiple
Return: new list; // For example: new list [N]. N indicates the number of contacts you want to generate.
List <ELEM> * temp = freelist;
Freelist = freelist-> next;
Return temp;
}

Template <class ELEM> // implementation of the Delete overload function.
Void list <ELEM>: Operator Delete (void * PTR)
{
(List <ELEM> *) PTR)-> next = freelist; // because void * PTR does not point to any type of pointer. So remember to force
Freelist = (list <ELEM> *) PTR; // converts it to a list <ELEM> * pointer.
}
//
Template <class ELEM>
Class alist
{
PRIVATE:
List <ELEM> * head; // header pointer.
List <ELEM> * tail; // tail pointer.
List <ELEM> * fence; // mark the pointer.
Int size; // The length of the linked list.
Public:
Alist ()
{
Head = tail = fence = new list <ELEM>; // Initialization
Size = 0;
}
~ Alist ()
{
While (Head! = NULL)
{
Fence = head;
Fence = fence-> next;
Delete fence;
}
}
Bool append (const ELEM &);
Void print () const;
Bool changlist ();
};
// In order to better see the transpose effect, data is inserted from the end point.
Template <class ELEM>
Bool alist <ELEM >:: append (const ELEM & T)
{
If (size = 0) // do not set an empty header contact.
{
Head-> DATA = T;
Tail = head;
Tail-> next = NULL;
++ Size;
Return true;
}
Tail = tail-> next = new list <ELEM> (T, null );
Size ++;
Return true;
}
// Print all data functions. This is a printing function that does not take the lead. The lead contacts will be different. Start with head-> next.
// Instead of starting from head.
Template <class ELEM>
Void alist <ELEM >:: print () const
{
List <ELEM> * temp = head;

Cout <"<";
While (temp! = Fence)
{
Cout <temp-> data <"";
Temp = temp-> next;
}
Cout <"| ";
While (temp! = NULL)
{
Cout <temp-> data <"";
Temp = temp-> next;
}
Cout <"> ";
}
// This is the program transpose function. To transpose, remember that you need three contacts.
Template <class ELEM>
Bool alist <ELEM>: changlist ()
{
List <ELEM> * temp = head; // The temp contact is used to ensure that it is always the first contact when the next contact is inserted.
If (Head-> next = NULL) // The linked list is empty and exits.
Return false;
While (Head-> next! = NULL) // It ends when the start point reaches the end point.
{
Fence = head-> next; // first set the marker contact and then start the next contact of the header contact. To ensure that it is always moved back.
Head-> next = fence-> next; // Delete the next contact of the Start header.
Fence-> next = temp; // place the deleted contacts at the beginning.
Temp = fence; // use the position of the header contact as the current header contact.
}
Tail = head; // the start point is now the end point. Therefore, the end contact is assigned.
Head = temp; // assign the current header to the original header. All the marked contacts are returned.
Return true;
}

Int main ()
{
Alist <char>;
Char item [100];
Cout <"Enter the -- 10 -- char you want :";
Cin> item;
For (INT I = 0; I <10; I ++)
{
A. append (item [I]); // input data.
}
Cout <"print all element before changing:" <Endl;
A. Print (); // The data location before the transpose.
A. changlist (); // transpose
Cout <"print all element after changed:" <Endl;
A. Print (); // The transposed data location.
Return 0;
}
// Example of the program running result:
// Enter the --- 10 --- char you want to: houyonghua
// Print all element before changing
// <| H o u y o n g h u a>
// Print all element after changed
// <| A u h g n o y u o h>
// You can modify the types in alist <ELEM> In the same program to input and output other types.
// ------------------------------- Program experiment ended --------------------------------------------------

Note:

This is an experiment exercise I wrote when studying data structures. Here is a detailed explanation. I hope someone who doesn't understand this problem can

Know how to transpose a linked 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.