3.1 Single Linked list

Source: Internet
Author: User

1. Introduction to Single-link listarrays are useful in programming languages, but there are at least two drawbacks to an array (1) You need to know the size of the array when compiling(2) is stored continuously in computer memory, which means inserting or deleting one data in an array, and adjusting other data. There are no such problems with the use of linked list structuresrepresents the structure of a linked list and its construction process. each node in the linked list shown is An instance of the following class definition (the node value is an integer type) : This is a C + + object-oriented approach, in C, we are accustomed to define the structure of the body.
  1. classIntSLLNode{
  2. public:
  3. IntSLLNode(){//第一个构造函数初始化next指针为NULL
  4. next =0;
  5. }
  6. IntSLLNode(int i,IntSLLNode*in =0){//第二个构造函数设置两个成员数据的值
  7. info = i; next = in;
  8. }
  9. int info;
  10. IntSLLNode*next;//指向下一个节点,这个节点的类型与本节点相同,这里要定义成公有成员,以便实施链表的操作
  11. };
The node consists of two members: info and Next, the info member is used for user-useful storage, and next is used to connect the linked list nodes to form the linked list structure. With this definition we can construct the linked list as shown:Intsllnode *p = new Intsllnode (ten);p->next = new Intsllnode (8);p->next->next = new Intsllnode (a);This only defines a head pointer p to access the data, not very convenient, in the actual operation will generally define two pointers or more pointers for easy operation.
2. Single-link list operationThe list node type has been defined by the class Intsllnode, the next is to define a class implementation of the list of various errors, single-linked list operation is nothing more than adding and deletingDefine the following class Intsllist
  1. classIntSLList{
  2. public:
  3. IntSLList(){
  4. head = tail =0;
  5. }
  6. ~IntSLList();
  7. int isEmpty(){
  8. return head ==0;
  9. }
  10. void addToHead(int);
  11. void addToTail(int);
  12. int deleteFromHead();// delete the head and return its info;
  13. int deleteFromTail();// delete the tail and return its info;
  14. void deleteNode(int);
  15. bool isInList(int)const;
  16. void printAll()const;
  17. private:
  18. IntSLLNode*head,*tail;//两个私有成员变量,分别是指向单链表的头和尾的指针
  19. };

2.1 Insert (1) Add to the list at the beginning of the four steps:
    • Create an empty node
    • Initialize node value El
    • Add a new node to the top of the list and point the new node's next to the head node, which is the current value of the head
    • Update head to a pointer to a new node
    1. voidIntSLList::addToHead(int el){
    2. head =newIntSLLNode(el,head);//一句话就完成了四步,这就是C++的魅力了
    3. if(tail ==0)
    4. tail = head;
    5. }
(2) Add to the end of the list four steps:
    • Create an empty node
    • Initialize the node value El because the node is to be added to the end of the list so that next of the new node is set to null
    • Next to the tail node of the original list, point to the new tail node, so that the new node is added to the end
    • Update tail to a pointer to a new node
    1. voidIntSLList::addToTail(int el){
    2. if(tail !=0){// 先判断链表是否为空
    3. tail->next =newIntSLLNode(el);
    4. tail = tail->next;
    5. }
    6. else head = tail =newIntSLLNode(el);
    7. }
2.2 Delete (1) Delete the head node and return its value in order to be rigorous in the program, two special cases need to be considered:
    • One is to attempt to remove a node from an empty list.
    • The second is the case of a linked list with only one node.
  1. intIntSLList::deleteFromHead(){
  2. if(!isEmpty())//检查链表链表是否为空
  3. {
  4. int el = head->info;
  5. IntSLLNode*tmp = head;
  6. if(head == tail)//加入链表只有一个节点;
  7. head = tail =0;
  8. else head = head->next;
  9. delete tmp;
  10. return el;
  11. }
  12. else 
  13. //std::cout<<"The List is Empty!"
  14. return 0 ;
  15. }
Note:The above program actually has a problem, the program began to use the IF statement to check whether the list is empty, if it is empty will do else operation returns 0, and for the statement that calls this method does not know whether the return 0 is the head node itself is the value of 0, or because the list is empty and return 0. The best solution is to determine whether the linked list is empty before calling this method. (2) Delete the tail node and return its value the delete operation is implemented by the member function Deletefromtail. After removing the tail node, the tail should point to the new end node of the list, that is, the tail must move a node in the opposite direction, but cannot move in the opposite direction because there is no direct link from the last node to its predecessor node. It is therefore necessary to find the precursor node from the shipowner head of the list and stop just before tail. This task is done by iterating through the list with a temp variable in the For loop.
  1. intIntSLList::deleteFromTail(){
  2. int el = tail->info;
  3. if(head == tail){// if only one node on the list;
  4. delete head;
  5. head = tail =0;
  6. }
  7. else{// if more than one node in the list,
  8. IntSLLNode*tmp;// find the predecessor of tail;
  9. for(tmp = head; tmp->next != tail; tmp = tmp->next);
  10. delete tail;
  11. tail = tmp;// the predecessor of tail becomes tail;
  12. tail->next =0;
  13. }
  14. return el;
  15. }
(3) Delete the two deletions that are discussed earlier in the node with the value El are the deletion of a node from the beginning or the end. If you want to delete a node that contains a specific integer, and you do not care about the node's position in the linked list, you need to use a different method. This node may be at the beginning or end of the list, or anywhere in the list. Simply put, you must first locate the node and then connect its predecessor node to the successor node to remove it from the list. Because you do not know where the node is, the operation is much more complex.
This node can be removed from the linked list by connecting the precursor and the successor, but because the list is onlybackward links, so it is not possible to get their precursors from a node. One way to do this repentance is to scan the list first to findDelete the node and then scan the list again to find its precursor, and another method as shown, with the help of two pointer variables pred and TMP. The preceding figure discusses only one scenario, with the following:
    • An attempt was made to delete a node from an empty list, at which point the function exits immediately.
    • Remove unique nodes from a single-node list: Both head and tail are set to NULL
    • Removes the first node from a list of at least two nodes, which needs to update the head
    • Removes the last node from a list of at least two nodes, which needs to be updated tail
    • There is no node in the list that contains a number: No action is made.
  1. voidIntSLList::deleteNode(int el){
  2. if(head !=0)  if Non-empty list;
  3. {
  4. if(head == tail && el == head->info){// if only one
  5. delete head;// node on the list;
  6. head = tail =0;
  7. }
  8. elseif(el == head->info){// if more than one node on the list
  9. IntSLLNode*tmp = head;
  10. head = head->next;
  11. delete tmp;// and old head is deleted;
  12. }
  13. else{// if more than one node in the list
  14. IntSLLNode*pred,*tmp;
  15. for(pred = head, tmp = head->next;// and a non-head node
  16. tmp !=0&&!(tmp->info == el);// is deleted;
  17. pred = pred->next, tmp = tmp->next)
  18. if(tmp !=0){
  19. pred->next = tmp->next;
  20. if(tmp == tail)
  21. tail = pred;
  22. delete tmp;
  23. }
  24. }
  25. }
  26. }
2.3 The Find insert and delete operations have modified the linked list. The lookup operation scans the existing list to determine if it contains a number, in this case with the Boolean member function isinlist ()implement this operation.
    1. boolIntSLList::isInList(int el)const{
    2. IntSLLNode*tmp;
    3. for(tmp = head; tmp !=0&&!(tmp->info == el); tmp = tmp->next);
    4. return tmp !=0;
    5. }
Complete code: http://www.oschina.net/code/snippet_588162_48571



From for notes (Wiz)



3.1 Single-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.