Linear Table - the basic operation of a single linked list

Single list of definitions and presentation

Single chain is a chain of accessing the data structure, a set ofAddress anyA memory cell storing the data elements in a linear list.

Data is linked listNodeRepresented, each node constituting the two domains comprises: storing the domain wherein the data element information becomesData field (data); Immediate successor field stores the storage position is referred toPointer field (next)
Here Insert Picture Description

Singly linked list pointer is uniquely determined by the head, so a single linked list can be usedHead pointer named after. If the head pointer name is L, then the list is simply referred in Table L.

Single linked list head pointer can be uniquely determined, the language used in c -structured pointer to describe:

//单链表的存储结构
typedef struct LNode
{
    ElemType data;        //结点的数据域
    struct LNode *next;   //结点的指针域
}LNode,*LinkList;         //LinkList为指向结构体LNode的指针类型

The first difference element node, the first node, the head pointer:

  • 首元结点It refers to a node of the first element of the list stored in a1
  • 头结点Is a node before the node attached to the first element, the first element pointer field points to the node
  • 头指针The first node is a pointer to a linked list of pointers
    Here Insert Picture Description

Increase the role of the head node of the list:

  • Facilitate the handling of the first element nodes
  • Empty tables to facilitate unitary and non-empty table

Achieve the basic operation of a single linked list


① single list initialization(Empty table configuration)

[] Algorithm steps

  • Generating a new node as the first node, with a head pointer pointing to the first node L
  • The head node pointer field blanking

【Algorithm Description】

Status InitList(LinkList &L)
{  
    L=new LNode;   //生成一个新结点作为头节点,用头指针L指向头节点
    L->next NULL;  //头节点指针域置空
    return OK;
 }

Note:
L = new new LNode; (C ++) can be written as
L = (LinkList) malloc (sizeof (LNode)); ©

malloc function usage (c):

  • malloc is a dynamic memory allocation function, for applying a contiguous memory block size of the area designated to void return type allocated memory area address (void indeterminate type pointer)
  • When using malloc open space used after the completion of use free函数释放of space
  • malloc function pointer must be added in front of the type 强制转换can only be used
  • Header filesstdlib.h
  • 指针自身 = (指针类型*)malloc(sizeof(指针类型)*数据数量)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p=NULL;
int n =5;
p=(int*)malloc(sizeof(int)*n);
}

free function usage (c):

  • For releasing malloc (or calloc, realloc) function to allocate memory space pointer variable.
  • After using the pointer variables need to be re指向NULL
#include<stio.h>
#include<stdlib.h>
int main()
{  
    int *p=( int*)malloc(sizeof(int)) ;
    *p=5;
    free(p);
    p=NULL;
}    

② single list of destruction(Head node, the head pointer, all the elements are destroyed)

[Algorithm] idea
scratch start pointer, the release of all the nodes sequentially
[Algorithm Description

Status DestoryList_L(LinkList &L)
{
     LinkList p;   //或Lnode *p;p为指针变量
     while(L)      //L非空 
     { 
        p=L;       //p、L指向同一结点
        L=L->next; //L指针指向下一个结点
        delete p;  //删除结点p
       } 
}      

③ Empty singly linked list (Head pointer, still head node)

[Algorithm] idea
sequentially release all the nodes and the head node pointer field set to NULL
[Algorithm Description

Status ClearList(LinkList &L)
{
    Lnode *p,*q;  //或LinkList p,q;
    p=L->next;
    while(p)      //非空,没到表尾
    {
      q=p->next;  //q指向p的下一个结点
      delete p;   //销毁p
      p=q;        //使p、q指向同一结点
     }
   L->next=NULL;  //将头节点指针域置为空
   return OK;  

④ seeking single linked list table length

[Algorithm] idea of
starting from the first element node, all the nodes sequentially counted
Description Algorithm]

int ListLength_L(LinkList L)
{
    Linklist p;  //或Lnode *p;
    p=L->next;   //p指向第一个结点
    i=0;         //计数器i
    whilw(p)     //遍历单链表,统计结点数
    {
      i++;
      p=p->next;  //p指向下一个结点
     }
}     

⑤ take the i-th element valueO(n)

[Thinking] algorithm
from the first node yuan, down the chain one by one domain next node access down
[algorithm description]

Status GetElem(LinkList L,int i,ELemType &e)
{
     p=L->next;j=1;  //p指向首元结点,计数器j=1
     while(p&&j<i)   
     {
       p=p->next;    //p指向下一个结点
       ++j;
      }
      if(!p||j>i)  return ERROR;   //i不合法i>n或i≤0
      e=p->data;       //取第i个结点的数据域
      return OK;
}       

⑥ Find by valueO(n)

[Algorithm] ideas
from the first node element, sequentially along the next search chain domain down until found equal to or empty e
[Algorithm Description

LNode *LocateElem(LinkList L,ElemType e)
{
    p=L->next;       //p指向首元结点
    while(p&&p->data!=e)  //顺链向后扫描,直到p为空或p所指数据域=e
      p=p->next;     //p指向下一个结点
    return p;        //若查找成功返回e的结点地址p,查找失败p为NULL
 }

⑦ insertion nodeO(n)

[Algorithm] idea of
the new node is inserted into a position e i-th node on the table, i.e., inserted into a (i-1) between a (i) with the a (i-1) is pointer field points to the data field for that new node e of the data field of the node pointer field pointing e a (i)
Here Insert Picture Description
[algorithm description

Status ListInsert(LinkList &L,int i,ElemType e)
{
    p=L;j=0;          //p指向头结点
    while(p&&(j<i-1))
    {
       p=p->next;++j;  //查找第i-1个结点,p指向该结点
     }
     if(!p||j>i-1)  return ERROR;   //i>n+1或i<1
     s=new LNode;      //生成新结点*s
     s->data=e;        //将结点*s的数据域置为e
     s->next=p->next;  //将结点*s的指针域指向结点a(i)
     p->next=s;        //将结点*p的指针域指向结点*s
     return OK;
}    

⑧ delete nodesO(n)

[Algorithm] idea
to delete a single linked list of nodes i a (i) and the release of a (i) of the space, so that a (i-1) pointer field points to a (i + 1)

Here Insert Picture Description
【Algorithm Description】

Status ListDelete(LinkList &L,int i)
{
     p=L;j=0;               //p指向头结点
     while((p->next)&&(j<i-1))
     {
       p=p->next;
       ++j;                //查找第i-1个结点,p指向该结点
      }
      if(!(p->next)||(j>i-1))  return ERROR;   //i>n或i<1时,删除位置不合理
      q=p->next;          //临时保存被删除结点的地址以备释放
      p->next=q->next;    //改变删除结点前驱结点的指针域
      delete q;           //释放删除结点的空间
      return OK;
 }     
       

Create a single list ⑨ forward runs lawO(n)

[Algorithm] Thinking
reverse input value of n elements, the establishment of a single list L with the header node
[Algorithm Description

void CreateList_H(LinkList &L,int n)
{
    L=new LNode;
    L->next=NULL;        //先建立一个带头节点的空链表
    for(i=0;i<n;++i)
    {
      p=new LNode;       //生成新结点*p
      cin >> p-data;     //输入元素值赋给新结点*p的数据域
      p->next=L-next; L->next=p;   //将新结点*p插入到头节点之后
    }
}      

Create a single list after interpolation ⑩O(n)

[Algorithm] Thinking
positive sequence input value of n elements, the establishment of the header with a single linked list node L
Here Insert Picture Description
[Algorithm Description

void CreatList_R(LinkList &L,int n)
{
    L=new LNode;
    L->next=NULL;       //先建立一个带头节点的空链表
    r=L;                //尾指针r指向头节点
    for(i=0;i<n;++i)
    {
       p=new LNode;      //生成新结点
       cin >> p->data;   //输入元素值赋给新结点*p的数据域
       p->next=NULL;r->nextp;    //将新结点*p插入尾结点*r之后
       r=p;    //r指向新的尾结点*p
     }
}      

Reference: "Data Structure" Yan Wei Min

Released five original articles · won praise 22 · views 1615

Guess you like

Origin blog.csdn.net/wmy0217_/article/details/103936065