Linked List _ LINKLIST and table _ linklist

Source: Internet
Author: User

Linked List _ LINKLIST and table _ linklist

  • Linked List _ LINKLIST
  • Structure of linked list
    • Linked List node _ LinkNode
    • Linked List node connection
  • Basic operations on linked lists
    • Node insertion _ INSERTNODE
    • Delete _ REMOVENODE
    • Access linked list element _ VISITNODE
    • Linked List basic operation INTERFACE _ INTERFACE
  • Coding Implementation of linked lists
  • Linked List Summary

All the code in this article can be found here:
Https://github.com/qeesung/algorithm/tree/master/chapter10/10-2
Or here:
Http://download.csdn.net/detail/ii1245712564/8775457
Find

Linked List _ LINKLIST

The chain table can be classified by storage method into two types, one is memory continuousSequential linked listThe memory is not consecutive.Chain Linked ListIn this article, we will mainly introduce more flexible chain linked lists. All the linked lists that appear later are chain linked lists by default.

Linked ListIt is a non-sequential storage structure of physical storage units. The logical sequence of data elements is achieved through the pointer link sequence in the linked list. A linked list consists of a series of nodes (each element in a linked list is called a node), which can be dynamically generated at runtime. Each node consists of two parts: one is the data domain that stores data elements, and the other is the pointer domain that stores the next node address.

Structure of linked list

As the name suggests, a linked list is a table. Each item in the table stores one data, and multiple items constitute a linked list.

An item in the linked list contains the data we need:

Strings multiple items into a string to form a table structure:

In the linked list, we call such an item the ListNode node of the linked list node. How exactly are these items chained together? Let's take a look at the internal information of ListNode.

Linked List node _ LinkNode

The basic element of a linked list isListNodeThe linked list node contains two pieces of information, one isData, One is pointing to the next nodePointer next.

Linked List node connection

Note that there is a pointer named next in the ListNode information, which is used to point to the next node. This is easy to do. We can direct the next pointer to the next listNode, the next pointer of the next listNode can point to another listNode, so that we can access all the linked list node elements from the first linked list node through the next pointer!

Basic operations on linked lists

The linked list has two basic operations:

  • Insert element
  • Delete Element

The data carrier when the linked list node is used. The insert element is to insert a node, and the delete element is to delete a node.

Node insertion _ INSERTNODE

Assume that we want to add two nodes in the linked listNode1AndNode2Insert a new nodeNode3.

It can be roughly divided into the following steps:

  • SaveNode1PointNode2Pointer, that is, saveNext1
  • SetNode1OfNextPointer to new nodeNode3
  • SetNode3OfNextSet the pointer to the previously saved pointNode2Pointer


The operation runs O (1)

Delete _ REMOVENODE

If there is insert, there will be deletion. Suppose there are three linked list nodes.Node1,Node2,Node3,
Now you needNode2Delete.

It can be roughly divided into the following steps:

  • SaveNode2PointNode3Pointer, that is, saveNext2
  • SetNext1Point to NodeNode2Delete
  • SetNext1Assign a value to the previously saved pointNode3Pointer


The operation runs O (1)

Access linked list element _ VISITNODE

For a linked list, we can access any element in the linked list from the table header, because each node of the linked list has a pointer to next node.

Assume that a linked list has three nodes.Node1,Node2,Node3, The header isNode1Now we need to access the data of the third node.





In this way, we find the third node and retrieve the elements.

Linked List basic operation INTERFACE _ INTERFACE

List. h

# Ifndef List_H # define List_H/** the linked List can implement other functions freely. Only a small part is listed here */template <class Elem> class List {public: // insert an element virtual bool insert (const Elem &) = 0 in the chain table header; // insert an element virtual bool append (const Elem &) = 0 at the end of the chain table; // remove an element in the linked list virtual bool remove (const Elem &) = 0; // clear all elements in the linked list virtual void clear () = 0; // print the entire linked list virtual void print () = 0 ;}; # endif
Coding Implementation of linked lists

Link_list.h

# Ifndef LINK_LIST_H # define LINK_LIST_H # include "list. h "/*** defines the linked list node */template <class Elem> class ListNode {public: Elem data; // the actual data ListNode <Elem> * next; // point to the next node};/** subclass List to implement interface functions */template <class Elem> class LinkList: public List <Elem> {private: listNode <Elem> * header; // The table header void freeList (ListNode <Elem> * startNode); // iteratively releases all nodes starting from start public: // constructor and destructor LinkList ();~ LinkList (); // basic operation function // insert an element bool insert (const Elem &) in the chain table header &); // insert an element bool append (const Elem &) at the end of the linked list; // remove an element bool remove (const Elem &) in the linked list &); // clear void clear (), all elements of the denomination in the linked list; // print the void print ();}; # include "link_list.cc" # endif

Compared with the List interface, we have added a new header pointer pointing to the table header of the linked List. We can use this pointer as the entry to find all the elements in the linked List!

Link_list.cc

# Include <iostream> // constructor and destructor template <class Elem> LinkList <Elem>: LinkList () {// first create a linked list header = new ListNode <Elem> (); header-> next = NULL ;} /** release all nodes starting with the node pointer during iteration */template <class Elem> void LinkList <Elem>: freeList (ListNode <Elem> * startNode) {while (startNode! = NULL) {ListNode <Elem> * tempNode = startNode-> next; delete startNode; startNode = tempNode;} return;} template <class Elem> LinkList <Elem> ::~ LinkList () {// recursively release the entire linked list freeList (header );} // basic operation function // insert an element template <class Elem> bool LinkList <Elem>: insert (const Elem & ele) into the head of the linked list) {ListNode <Elem> * newNode = new ListNode <Elem> (); if (newNode = NULL) return false; newNode-> next = NULL; newNode-> data = ele; listNode <Elem> * tempNode = header-> next; header-> next = newNode; newNode-> next = tempNode; return true ;} // insert an element template <class Elem> bool at the end of the linked list LinkList <Elem >:: append (const Elem & ele) {// In tempNode, it now points to the last element ListNode <Elem> * newNode = new ListNode <Elem> (); if (newNode = NULL) return false; newNode-> next = NULL; newNode-> data = ele; // first locate the ListNode <Elem> * lastNode = header at the end of the linked list; while (lastNode-> next! = NULL) {lastNode = lastNode-> next;} // append node lastNode-> next = newNode; return true ;} // remove an element template <class Elem> bool LinkList <Elem>: remove (const Elem & ele) from the linked list) {// find the corresponding element ListNode <Elem> * targetNode = header; while (targetNode-> next! = NULL & targetNode-> next-> data! = Ele) {targetNode = targetNode-> next;} if (targetNode-> next = NULL) // The target element return false is not found; listNode <Elem> * tempNode = targetNode-> next; delete targetNode-> next; targetNode-> next = tempNode; return true ;} // clear the template <class Elem> void LinkList <Elem >:: clear () {freeList (header-> next ); header-> next = NULL;} // print the entire linked list template <class Elem> void LinkList <Elem >:: print () {ListNode <Elem> * TempNode = header; while (tempNode-> next! = NULL) {std: cout <tempNode-> next-> data <"\ t"; tempNode = tempNode-> next;} std: cout <std:: endl ;}

Link_list_test.cc

#include <iostream>#include "link_list.h"int main(int argc, char const *argv[]){    LinkList<int> list;    // insert three elements    std::cout<<"Insert 1 2 3"<<std::endl;    list.insert(1);    list.insert(2);    list.insert(3);    list.print();    list.clear();    std::cout<<std::endl;    // append three elements    std::cout<<"append 1 2 3"<<std::endl;    list.append(1);    list.append(2);    list.append(3);    list.print();    list.clear();    std::cout<<std::endl;    // append three elements then remove one of them    std::cout<<"append 1 2 3 then remove 3"<<std::endl;    list.append(1);    list.append(2);    list.append(3);    list.remove(3);    list.print();    list.clear();    std::cout<<std::endl;    return 0;}

Compilation Method:

G ++ link_list_test.cc-o link_list_test

Running method:

./Link_list_test

Running result:

Insert 1 2 3
3 2 1
Append 1 2 3
1 2 3
Append 1 2 3 then remove 3
1 2

Linked List Summary

Since linked lists have such a big advantage, we will discard traditional arrays in the future and use linked lists! Of course, this is not the case. The linked list and array have their own advantages and use cases:

  • Linked List: Can be in constant time O (1) The data deletion and addition operations are completed within the time range. O (n) The Data Access to the linked list does not support direct access, because the data of the linked list nodes is not sequentially distributed, in the memory (usually in the heap) the data distribution of the linked list may be as follows:

    Therefore, the linked list is suitable for frequent insertion and deletion of data.
  • Array: The array can be in constant time O (1) Because the element distribution in the array is continuous, you can directly add an offset to the starting address of the array to find the corresponding element, the portion of the array in the memory may be as follows:

    If you want to delete and add data, the running time is O (n) Therefore, arrays are suitable for scenarios with frequent data access.

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.