The creation algorithm of single linked list __ algorithm

Source: Internet
Author: User

an algorithm for creating single linked list

This article is based on: Tsinghua University Press "Data structure and algorithm (C language Edition) (3rd edition)" collation, detailed see book.

When a sequence contains only links to subsequent nodes, the list is called a single linked list.

The schematic diagram of the single linked list is as follows:


Head pointer is a single linked list of the first pointer, single linked table L:l is both a single linked list of names, but also its head pointer. The pointer field for the last node in the list is defined as a null pointer (NULL).
The definition of a single linked list:

struct Node
 {
   elemtype data;
   struct Node *next;
 typedef struct NODE Lnode;
 typedef struct NODE *linkedlist;
Single linked list has the lead node and does not lead the node points.

The above diagram is a single linked list without a head node, and the following figure is a single linked list with a head node:


1. The initialization of a single linked list, that is, to establish an empty linked list.

  Initialization
  void LinkedListInit1 (LinkedList L)
  {
    l=null
  }
  of a single linked list without a leading node A single linked list of leading nodes is initialized
  void LinkedListInit2 (LinkedList L)
  {
    l= (Lnode *) malloc (sizeof (Lnode));
    if (l==null)
    {
      printf ("Request space failed.") ");
      Exit (0);
    }
    l->next=null;
  }

2. Single linked list of the table long operation Single linked list of the table long operation needs to set the current pointer p and a counter J, at the beginning, p points to the first node in the list, and when P moves one node downward, J adds 1 until it reaches the end of the P-linked list. The linked list of the leading node, the chain table length does not include the head node.

  The single linked table of the lead node is for the table length
  int linkedlistlength (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 Gets the operation of the first node elementSet p to be the current node, the initial P point to the first node of the list, then move down I, at which point P points to the element that needs to be found.

  The single link of the lead node is operated
  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. The position of the single linked list locates the first occurrence of element E. Starting from the first node of the list, determine whether the value of the current node equals E, or return the pointer to the node, otherwise continue looking backwards until the end of the list is reached.

  The single linked table location operation of 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. Insert operation of single linked listInsert a new node Q before the node P: for a single linked list that does not take the lead node, the location of the node p is different and the insertion operation has the following two conditions:
1 Insert the table header in the linked 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, that is, L.
(3) Modify the L to point to the new node Q.
Operation diagram is as follows:

                                  &NBSP;&NBSP
    2) insert in the middle of the list
        (1) to create a new node Q.
        (2) assigns the data field of this node to E and points its next pointer to P.
        (3) finds the predecessor node pre of P.
        (4) points the next pointer of the pre to the newly created node Q.
      operation sketch is as follows:
                                            &NBSP

    Insert operation of single linked list without lead node void LinkedListInertQE1 (LinkedList L, LinkedList p, elemtype e) {q= (Lnode *) malloc (siz        EOF (Lnode)); Create a new node Q if (q==null) {printf ("Request space failed.")
        ");
      Exit (0);

      } q->data=e;
        if (p==l)///Insert {q->next=l; in table header
      l=q;
        else//insert in the middle of the table {pre=l;

        while ((Pre!=null) && (pre->next!=p))//looking for the precursor of P pre=pre->next;
        q->next=pre->next;
      pre->next=q; The insert operation void LinkedListInertQE2 (LinkedList L, LinkedList p, elemtype e) {q= (Lnode *) Ma for the single linked list of the lead node        Lloc (sizeof (Lnode)); Create a new node Q if (q==null) {printf ("Request space failed.")
        ");
      Exit (0);

      } q->data=e;
      Inserting a new node pre=l;

      while ((Pre!=null) && (pre->next!=p))//looking for the precursor of P pre=pre->next;
      q->next=pre->next;
    pre->next=q; }

6. The deletion of a single linked list deletes an element e from the list, and if E appears more than once in the list, it deletes the first occurrence of E, otherwise doing nothing.
Use p to find the node where element e resides:
1) p is the first node in the list
(1) point L to P->next.
(2) releasing p.
The schematic diagram is as follows:

2) p is the other node in the list
(1) Find P's precursor node pre.
(2) point the Pre->next to P->next.
(3) releasing p.
The schematic diagram is as follows:

7. Single linked list creation operation single linked list creation method has two kinds: the head inserts the method and the tail interpolation method.
Header interpolation is to insert the new node before the first node, the schematic diagram is as follows:


The trailing interpolation method is to insert the new node after the last node, the schematic diagram is as follows:

    Single-Link table void Linkedlistcreateheadl (LinkedList L, Elemtype a[n]) {l= (Lnode *) malloc (sizeof (Lnod
      e)); if (l==null) {printf ("Request space failed.")
        ");
      Exit (0);

      } l->next=null;
        For (i=0 i<n; i++) {p= (Lnode *) malloc (sizeof (Lnode)); if (p==null) {printf ("Request space failed.")
          ");
        Exit (0);
        } p->data=a[i];
        p->next=l->next;
      l->next=p; (////////////) Create a lead node by using the trailing interpolation (LinkedList L, Elemtype a[n]) {l= (Lnode *) malloc (siz
      EOF (Lnode)); if (l==null) {printf ("Request space failed.")
        ");
      Exit (0);
      } l->next=null;         Tail=l;
        Set the tail pointer to facilitate inserting for (j=0 j<n; j + +) {p= (Lnode *) malloc (sizeof (Lnode)); if (p==null) {printf ("Request space failed.")
          ");
        Exit (0);
        } p->data=a[j];
        p->next=null;
      tail->next=p;  Tail=p; }
    }

8. Single-linked list of the merge operation first set 3 pointers PA, Pb, PC, PA and PB respectively point to the list of LA and lb at the current to compare the insertion node, the PC points to the last node of the list LC. When pa->data≤pb->data, insert the node that the PA refers to behind the PC, or insert the PB point to the back of the PC. Finally, when a table is merged, the remaining nodes of the other table are plugged into the PC.

    Single linked list merging operations for lead nodes
    void Linkedlistmergelalb (LinkedList La, LinkedList Lb, LinkedList Lc)
    {
      pa=la->next;
      pb=lb->next;
      Lc=la;          The head node of the table LA is borrowed as the head node PC=LC of the 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 table header node of LB
    

Appendix:

Program Example: the tail interpolation method to create a single linked list

Start by creating a new WIN32 Console Application project LinkedList in VS2010, with the following results:

LinkedList.cpp: Defines the entry point for a console application.

LinkedList.cpp: Defines the entry point for a 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 to end by 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, program run screenshot:

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.