Data Structure BASICS (8) -- Design and Implementation of a single-chain table (1) basic operations

Source: Internet
Author: User

Data Structure BASICS (8) -- Design and Implementation of a single-chain table (1) basic operations
Linked List Overview

Disadvantages of array:

1. element insertion: In addition to inserting elements at the end of the array, inserting elements at any position of the array requires frequent movement of array elements (the elements after the insertion position must be moved back ), the time complexity is about O (N );

2. delete an array: In addition to deleting elements at the end of an array, removing elements from any other position of the array requires frequent movement of array elements (the elements after the deletion must move forward), and the time complexity is O (N );

Features of linked list:

Because no data shift is required to insert or delete elements in the linked list, only O (1) time is required. Therefore, the linked list is suitable for frequent insertion and deletion;

But the linked list also has its disadvantages: The linked list does not apply to situations where frequent access is required, because if you need to query a data, the linked list needs to traverse the entire data sequence and the required O (n) time, however, because arrays support random access, the array only requires O (1) Time to complete data access, so the array is very convenient!


Single-chain table

Single-chain table features:


A single-chain table only needs one node (first node) to point to this linked list. Sometimes, for ease of operation, add a "head node" before the first node (referred to as a "dumb node" in the introduction to algorithms) to use the pointer to the head node as the head pointer of the linked list (for convenience, we have adopted this linked list implementation scheme with an additional header node, and laid the foundation for us to transform the linked list into a circular linked list and a circular double-chain table in the future ). <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Authorization/b7d1KrL2CwgsdjQ68/I1dK1vbXaIGktMSC49sr9vt3UqsvYoaM8L3A + PHA + Signature = "brush: java;"> class Node {private: Type data; // data field: Node data Node * next; // pointer field: Next node };

However Class, which needs to be transformed:

// Linked list node template
 
  
Class Node {// you can use the MyList class as the Node's friend meta // or use the Node class as the nested class of MyList, Which is nested in MyList. You can also use this function to implement the friend class MyList function.
  
   
; Template
   
    
Friend ostream & operator <(ostream & OS, const MyList
    
     
& List); private: // constructor Description: // next = NULL; // because this is a newly generated Node, the next Node is empty (const Type & dataValue ): data (dataValue), next (NULL) {} Type data; // data field: Node data Node * next; // pointer field: next Node };
    
   
  
 

Single-chain table structure:

// Linked list template
 
  
Class MyList {template
  
   
Friend ostream & operator <(ostream & OS, const MyList
   
    
& List); public: MyList ();~ MyList (); // Insert the element into the header void insertFront (const Type & data); // Insert the element to the Position index (index starts from 1) void insert (const Type & data, int index); // Delete the node void remove (const Type & data); bool isEmpty () const; // reverse void invort () in the linked list; // link the linked list to the end of the linked list void concatenate (const MyList
    
     
& List); private: // pointer to the first Node
     
      
* First ;};
     
    
   
  
 
// Create a template for the linked list
 
  
MyList
  
   
: MyList () {// first points to an empty Node first = new Node
   
    
(0); first-> next = NULL;} // structure template of the linked list
    
     
MyList
     
      
::~ MyList () {Node
      
        * DeleteNode = NULL; while (first! = NULL) {deleteNode = first; first = first-> next; delete deleteNode ;}}
      
     
    
   
  
 

Element insertion:

Visible from the previous image, you only need to modify the pointer to insert a node in the single-link table. But at the same time, to insert an element before the I node, modify the pointer of the I-1 node.

Therefore, the basic work of the I node in the single-chain table is: Find the I-1 node in the linear table, and then modify it to point to the next pointer.

template 
 
  void MyList
  
   ::insertFront(const Type &data){    Node
   
     *newNode = new Node
    
     (data);    newNode -> next = first -> next;    first -> next = newNode;}
    
   
  
 
Template
 
  
Void MyList
  
   
: Insert (const Type & data, int index) {// since we have added an empty node to the header, // If the linked list is empty, or add an element at the position where the linked list is 1 // its operation is the same as adding an element at other locations int count = 1; // searchNode is certainly not a NULL Node at this time
   
    
* SearchNode = first; // locate the position to be inserted // If the given index is too large (beyond the length of the linked list) // Insert the element to the end of the linked list table. // The cause is searchNode-> next! = NULL: This condition is no longer met. // it has reached the end of the table while (count <index & searchNode-> next! = NULL) {++ count; searchNode = searchNode-> next;} // insert a linked list Node
    
     
* NewNode = new Node
     
      
(Data); newNode-> next = searchNode-> next; searchNode-> next = newNode ;}
     
    
   
  
 

Element deletion:

The basic operation to delete node I in a single linked list is: Find the node I-1 in the linear table, modify it to the next pointer.

Template
 
  
Void MyList
  
   
: Remove (const Type & data) {if (isEmpty () return; Node
   
    
* Previous = first; // Save the previous Node to be deleted for (Node
    
     
* SearchNode = first-> next; searchNode! = NULL; searchNode = searchNode-> next) {if (searchNode-> data = data) {previous-> next = searchNode-> next; delete searchNode; // re-adjust the searchNode pointer // check whether there are still equal elements in the traversal chain table // if the current searchNode has reached the last node // that is, searchNode-> next is already NULL, the following statement cannot execute if (previous-> next = NULL) break; searchNode = previous-> next;} previous = searchNode ;}}
    
   
  
 

// Empty template of the linked list
 
  
Bool MyList
  
   
: IsEmpty () const {return first-> next = NULL ;}
  
 

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.