zoukankan      html  css  js  c++  java
  • Singly Linked List

    Singly Linked List

    Singly linked list storage structure:
    typedef struct Node
    {
    ElemType data;
    struct Node *next;
    }Node;


    typedef struct Node *LinkList;

    LinkedList without head node:


    LinkedList with head node:

    Operations:

    /*check the size of link list.*/
    int ListLength(LinkList *L)
    {
    i=0;
    LinkList p;
    p=L->next;
    while(!p)
    {
    p=p->next;
    ++i;
    }
    return i;
    }

    /*return the value of number i data element in list L to e.*/
    Status GetElem(LinkList L,int i,ElemType *e)
    {
    int j;
    LinkList p;
    /*let p point to the first node of L.*/
    p=L->next;
    /*j works as a counter.*/
    j=1;
    /*p is a null and j is not equal to i.*/
    while(p&&j<i)
    {
    /*let p point to the next node.*/
    p=p->next;
    ++j;
    }
    /*the ith element does not exist.*/
    if(!p||j>i)
    {
    return ERROR;
    }
    /*get the number i element.*/
    *e=p->data;
    return OK;
    }

    List Insert


    Status ListInsert(LinkList *L,int i,ElemType e)
    {
    int j;
    LinkList p,s;
    p=*L;
    j=1;
    /*search for number i node.*/
    while(p&&j<i)
    {
    p=p->next;
    ++j;
    }
    if(!p||j>i)
    {
    /*number i node does not exist.*/
    return ERROR;
    }
    /*make a new node.*/
    s=(LinkList)malloc(sizeof(Node));
    s->data=e;
    /*key steps:*/
    s->next=p->next;
    p->next=s;
    return OK;

    }

    List Delete


    Status ListDelete(LinkList *L,int i,ElemType *e)
    {
    int j;
    LinkList p,q;
    p=*L;
    j=1;
    /*search for number i node.*/
    while(p->next&&j<i)
    {
    p=p->next;
    ++j;
    }
    if(!(p->next)||j>i)
    {
    /*number i node does not exist.*/
    return ERROR;
    }
    /*key steps*/
    q=p->next;
    p->next=q->next;
    *e=q->data;
    free(q);
    return OK;

    }

    /*create a list using head inserting method.*/
    void CreateListHead(LinkList *L,int n)
    {
    LinkList p;
    int i;
    *L=(LinkList)malloc(sizeof(Node));
    /*create a linked list with head node.*/
    (*L)->next=NULL;
    for(i=0;i<n;i++)
    {
    /*generate a new node.*/
    p=(LinkList)malloc(sizeof(Node));
    /*store the data of number i node as 100+i.*/
    p->data=100+i;
    p->next=(*L)->next;
    /*insert the node to head.*/
    (*L)->next=p;
    }
    }


    /*create a list using tail inserting method.*/
    void CreateListTail(LinkList *L,int n)
    {
    LinkList p,r;
    int i;
    /*create a linked list with head node.*/
    *L=(LinkList)malloc(sizeof(Node));
    r=*L;
    for(i=0;i<n;i++)
    {
    /*generate a new node.*/
    p=(LinkList)malloc(sizeof(Node));
    p->data=100+i;
    r->next=p;
    r=p;
    }
    /*marks the end of list.*/
    r->next=NULL;
    }

    /*clear the list to empty.*/
    Status ClearList(LinkList *L)
    {
    LinkList p,q;
    /*let p point to first node.*/
    p=(*L)->next;
    /*while it's not list end.*/
    while(p)
    {
    q=p->next;
    free(p);
    p=q;
    }
    /*set the list head pointer to null.*/
    (*L)->next=NULL;
    return OK;
    }

  • 相关阅读:
    vue学习之router
    vue学习之组件
    xshell操作
    Webstorm快捷操作
    javascript判断节点是否在dom
    影子节点 shadowDOM
    虚拟节点操作——DocumentFragment
    理解浏览器的历史记录
    浏览器渲染
    web请求流程
  • 原文地址:https://www.cnblogs.com/askDing/p/5978400.html
Copyright © 2011-2022 走看看