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

  • 相关阅读:
    亲们,知道你想更优秀,你做到这些了吗?
    Linux socket编程学习笔记(一):socket()函数详解
    关于typedef的用法总结
    c,c++里面,头文件里面的ifndef /define/endif的作用
    玩转ptrace
    文笔流畅,修辞得体
    定义和实现一个类的成员函数为回调函数
    《Shell 脚本学习指南 》 背景知识与入门 [第一、二章]
    使用ptrace跟踪进程
    FCKeditor 2.6.4.1配置
  • 原文地址:https://www.cnblogs.com/askDing/p/5978400.html
Copyright © 2020-2023  润新知