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;
    }

  • 相关阅读:
    竞赛生每日一题(212) 徐康华竞赛优学
    利用python爬取特定类别图片---labelimg制作自己的目标检测数据集
    Labview各版本及开发工具模块下载
    Windows安装tensorflow经验总结(尤其安装GPU版本的细看)
    opencv与labview的结合(升级版:彩色图像的传输)
    opencv与Labview的结合(Dll调用)
    QT如何重写控件内部的函数 ——趣味小程序(按钮随机移动,鼠标无法点击)
    QT多个窗体切换显示
    QT实现鼠标操作事件(获得鼠标的坐标和间值)
    VS/C++/win10/opencv 神经网络数字识别
  • 原文地址:https://www.cnblogs.com/askDing/p/5978400.html
Copyright © 2011-2022 走看看