The creation algorithm of single linked list

Source: Internet
Author: User

the creation algorithm of single linked list


When a sequence contains only a link to its successor, it is called a single-linked list.

The single linked list is as follows:


The head pointer is a single-linked list of header pointers, and the single-linked list l:l is both the name of the single-linked list and its head pointer. The pointer field of the last node in the linked list is defined as a null pointer (NULL).
Definition of a single linked list:

struct Node {   elemtype data;   struct Node *next; }; typedef struct NODE Lnode; typedef struct NODE *linkedlist;
the single linked list has a lead node and does not lead to the point of the node.

A single linked list without a head node, as a single linked list with a head node:


1. The initialization of a single-linked list, that is, the establishment of an empty list.
  Initialize  void LinkedListInit1 (LinkedList L)  {    l=null;  }  without leading the node's single-linked list; Initialization of the single-linked list of the lead nodes  void LinkedListInit2 (LinkedList L)  {    l= (Lnode *) malloc (sizeof (Lnode));    if (l==null)    {      printf ("Application space failed!") ");      Exit (0);    }    l->next=null;  }

2. Single-linked list of the operation of the table longthe single-linked list of the table long operation needs to set the current pointer p and a counter J, the initial p point to the first node in the list, p each downward move a node, J adds 1, until the end of the P-linked list is reached. A linked list of leading nodes, and the list length does not include the head node.
  The single-linked list of the leading nodes is  linkedlistlength int (linkedlist L)  {    Lnode *p;            P needs to be declared as Lnode type    p=l->next;    int j=0;    while (P!=null)    {      j + +;      p=p->next;         Move p down one node    }    return J;  
3. Single-linked list for the operation of the element I nodeset p to the current node, the initial p points to the first node of the list, and then move Down I, where p points to the element I want to find.

  The single-linked list takes the lead node operation  LinkedList Linkedlistgetinode (linkedlist L, int i)  {    Lnode *p;    p=l->next;    int j=1;    while ((P!=null) && (j<i))    {      p=p->next;      j + +;    }    return p;  }

4. Single-linked list positioning operationfinds the position where element e first appears. From the first node of the list, determine whether the value of the current node is equal to E, or return a pointer to that node, or continue looking backwards until you reach the end of the list.
  Single-linked list positioning operation for the lead node  lnode linkedlistlocatee (LinkedList L, elemtype e)  {    Lnode *p;    p=l->next;    while ((P!=null) && (p->data!=e))    {      p=p->next;    }    return p;  }
5. Single-linked list insert OperationInsert a new node before node P Q: For single-linked lists that do not have a lead node, the location of the node p differs, and the insert operation has the following two cases:
1) Insert the table header in the list:
(1) Create a new node Q.
(2) Assign the data field of this node to E and point its next pointer to the first node, which is L.
(3) Modify the L to point to the new node Q.
The operation is as follows:

                                    
    2) insert in the middle of the list
        (1) Create a new node Q.
        (2) assigns the data field of this node to E and points its next pointer to P.
        (3) Find the Pre-node of p.
        (4) points to the newly created node Q in the next pointer of the pre.
      actions are as follows:
                        &NBS P                    

Insert operation for single-linked list without the lead node void LinkedListInertQE1 (LinkedList L, LinkedList p, elemtype e) {q= (Lnode *) malloc (sizeof (LNo        de)); Create a new node Q if (q==null) {printf ("Application space failed!")        ");      Exit (0);      } q->data=e;        if (p==l)//Insert {q->next=l; in table header      l=q;        } else//insert {pre=l in the middle of the table;        while ((Pre!=null) && (pre->next!=p))//search for the precursor of P pre=pre->next;        q->next=pre->next;      pre->next=q; }}//The single-linked list insertion operation of the node is void LinkedListInertQE2 (LinkedList L, LinkedList p, elemtype e) {q= (Lnode *) malloc (s        Izeof (Lnode)); Create a new node Q if (q==null) {printf ("Application space failed!")        ");      Exit (0);      } q->data=e;      Inserting a new node pre=l;      while ((Pre!=null) && (pre->next!=p))//search for the precursor of P pre=pre->next;      q->next=pre->next;    pre->next=q; }

6. Single-linked list delete operationDelete an element in the linked list E, if E appears more than once in the list, the first occurrence of E is deleted, otherwise nothing is done.
Use p to find the node where element e is located:
1) p is the first node in a linked list
(1) point L to P->next.
(2) Release p.
As follows:

2) p is the other node in the linked list
(1) Find the precursor node of P pre.
(2) Point Pre->next to P->next.
(3) Release p.
As follows:

     Delete operation of single-linked list without the lead node     void LinkedListDeleteQE1 (LinkedList L, LinkedList p, elemtype e)     {        pre=l;        while ((Pre!=null) && (pre->next->data!=e))      //Find the precursor of element e            pre=pre->next;        p=pre->next;        if (p!=null)                //Find the node that needs to be deleted        {          if (p==l)                 //delete is the first node            l=p->next;          else                     //Delete the other node            pre->next=p->next;          Free (p);        }     }     The deletion of the single linked list of the lead node is     void LinkedListDeleteQE2 (LinkedList L, LinkedList p, elemtype e)     {        pre=l;        while ((Pre!=null) && (pre->next->data!=e))      //Find the precursor of element e            pre=pre->next;        p=pre->next;        if (p!=null)                //Find the node you want to delete        {          pre->next=p->next;          Free (p);        }     }

7. Create a single-linked list operationThere are two ways to create a single-linked list: The head interpolation method and the tail interpolation method.
The head interpolation method is to insert the new node before the first node, as follows:


The end interpolation method is to insert the new node after the last node, as follows:

                                                       

Create a single-linked list void Linkedlistcreateheadl (LinkedList L, Elemtype a[n]) {l= (Lnode *) malloc (sizeof (Lnode)) with head interpolation for the lead node; if (l==null) {printf ("Application space failed!")        ");      Exit (0);      } l->next=null;        for (i=0; i<n; i++) {p= (Lnode *) malloc (sizeof (Lnode)); if (p==null) {printf ("Application space failed!")          ");        Exit (0);        } p->data=a[i];        p->next=l->next;      l->next=p; }}//The single-linked list void Linkedlistcreatetaill (LinkedList l, Elemtype A[n]) {l= (Lnode *) (sizeof) of the lead node is created by using the tail interpolation method.      Node)); if (l==null) {printf ("Application space failed!")        ");      Exit (0);      } l->next=null;         Tail=l;        Set the tail pointer to conveniently insert for (j=0; j<n; J + +) {p= (Lnode *) malloc (sizeof (Lnode)); if (p==null) {printf ("Application space failed!")          ");        Exit (0);        } p->data=a[j];        p->next=null;        tail->next=p;      Tail=p; }    }

8. Merge operations for single-linked listsfirst set up 3 pointers PA, Pb, PC, PA and PB respectively point to the link list la and lb's current to compare Insert node, the PC points to the last node of the linked list LC. When Pa->data≤pb->data, the node referred to by the PA is inserted behind the PC, otherwise the node referred to by PB is inserted behind the PC. Finally, when one table is merged, the remaining nodes of the other table are all inserted into the PC.

    Single-linked list of lead nodes combined Operation    void Linkedlistmergelalb (LinkedList La, LinkedList Lb, LinkedList Lc)    {      pa=la->next;      pb=lb->next;      Lc=la;          Borrow the head node of table LA as the head node of the table Lc      PC=LC;      while ((Pa!=null) && (pb!=null))      {        if (pa->data<=pb->data)        {          pc->next=pa;          PC=PA;          pa=pa->next;        }        else        {          pc->next=pb;          PC=PB;          pb=pb->next;        }      }      if (pa!=null)        pc=pa->next;      else        pc=pb->next;      Free (PB);    Release the header node of LB    }
This article according to: Tsinghua University Press "Data structure and algorithm (C language version) (3rd edition)" Collation, detailed please see books.

Appendix:

Program example: The tail interpolation method to create a single linked list

The project LinkedList for the WIN32 console application is created in VS2010 first, with the following results:

LinkedList.cpp: Defines the entry point of the console application.

LinkedList.cpp: Defines the entry point of the console application.      #include "stdafx.h" #include <stdio.h> #include <stdlib.h> typedef struct Node {int data;    struct node *next;    }*linkedlist;      LinkedList linkedlistcreatetaill (int a[8]) {LinkedList p, L, tail;      int i=0;      l= (struct node*) malloc (sizeof (struct node));      Tail=l;        for (i=0; i<8; i++) {p= (struct node*) malloc (sizeof (struct node));        p->data=a[i];        tail->next=p;      Tail=p;      } tail->next=null;    return L;      } void Linkedlistprint (LinkedList L) {LinkedList p;      p=l->next;        while (P!=null) {printf ("%d", p->data);      p=p->next;      }} void Main () {int a[8], I;      LinkedList L;  printf ("Please enter 8 list elements, end with carriage return: \ n");      for (i=0; i<8; i++) {scanf ("%d", &a[i]);      } L=linkedlistcreatetaill (a);    Linkedlistprint (L); }
Ctrl+f5 Execute the above CPP file, the program runs:

The creation algorithm of 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.