Data Structure and Algorithm - Linear List - Singly Linked List

The second physical structure of the linear table in the data structure - the chained storage structure, its c++ implementation code is as follows:

#ifndef DANLIANBIAO_H
#define DANLIANBIAO_H
#include<iostream>
using namespace std;
typedef int ElemType;
struct Lnode
{     ElemType data;     Lnode* next; }; typedef Lnode* LinkList; //Single linked list initialization bool InitList(LinkList& L) {     L = new Lnode;     L->next = nullptr;     return true; } //Single linked list destruction bool DestroyList(LinkList& L) {     while (L)     {         LinkList p = L->next;         delete L;         L = p;     }     return true; } //Empty the singly linked list























bool ClearList(LinkList& L)
{     LinkList p = L->next;     while (p)     {         LinkList q = p->next;         delete p;         p = q;     }     L->next = nullptr;     return true; } //judgment Whether the singly linked list is empty bool IsEmpty(const LinkList& L) {     if (L->next)     {         return false;     }     else     {         return true;     } } //Find the length of singly linked list L int ListLength_L(LinkList L) {     LinkList p ;     p = L->next;     int i = 0;     while (p) {





























        i++;
        p = p->next;
    }
    return i;
}
//Get value--take the content of the i-th element in the singly linked list
bool GetElem_L(LinkList L, int i, ElemType& e)
{     LinkList p = L->next ;     int j = 1;     while (p && j < i)     {         p = p->next;         ++j;     }     if (!p || j > i)     {         return false;     }     e = p->data;     return true; }













//Find 1 by value
//If found, return the address of the data element whose value is e in L; if the search fails, return null
LinkList LocateElem_L1(LinkList L, ElemType e)
{     LinkList p = L->next;     while (p && p- >data != e)     {         p = p->next;     }     return p; } //Search by value 2 //Find and return the position number of the data element whose value is e in L, if the search fails, return 0 int LocateElem_L2(LinkList L , ElemType e) {     LinkList p = L->next;     int j = 1;     while (p && p->data != e)     {         p = p->next;         j++;     }     if (p) return j;     else return 0; }




















//Insert--insert a new node whose value is e before the i-th node
bool ListInsert_L(LinkList& L, int i, ElemType e)
{     LinkList p = L;     int j = 0;     while (p && j < i - 1)      {         p = p->next;         ++j;     }     if (!p || j > i - 1) return false;     LinkList s = new Lnode;     s->data = e;     s->next = p ->next;     p->next = s;     return true; } //Delete--delete the i-th node bool ListDelete_L(LinkList& L, int i,ElemType& e) {     LinkList p = L;     LinkList q;     int j = 0;     while (p->next && j < i - 1)     {         p = p->next;























        ++j;
    }
    if (!(p->next) || j > i - 1) return false;
    q = p->next;
    p->next = q->next;
    e = q->data;
    delete q;
    return true;
}
//Header interpolation to create a singly linked list
void CreateList_H(LinkList& L, int n)
{     L = new Lnode;     L->next = NULL;     for (int i = n; i > 0; --i )     {         LinkList p = new Lnode;         cin >> p->data;     p-         >next = L->next; L- >         next = p ; n) {     L = new Lnode;     LinkList r = L;















    for (int i = 0; i < n; ++i)
    {
        LinkList p = new Lnode;
        cin >> p->data;
        p->next = NULL;
        r->next = p;
        r = p;
    }
    
}
#endif // !DANLIANBIAO_H

Guess you like

Origin blog.csdn.net/hu853712064/article/details/124027957