链表---数据结构四要素

#include <QCoreApplication>
#include <iostream>
#include <QDebug>
#include <string>
#if  !NotLog
#define                     cdebug(format,any)                  ({fprintf(stderr,"[cdebug]"),fprintf(stderr,format,any);})
#define                     cdebugs(format,...)              ({fprintf(stderr,"[cdebugs]"),fprintf(stderr,format,__VA_ARGS__);})

template <class Any>void cppdebug(Any any){std::cout<<"[cppdebug]"<< any <<std::endl;}
#define                     cppdebugs(any)                      std::cout<<"[cppdebugs]"<< any

template <class Any>void qtdebug(Any any) {qDebug()<<"[qtdebug]"<< any;}
#define                     qtdebugs(any)                      qDebug()<<"[qtdebugs]"<< any
/*some debug macro*/
#define typesize(x)      qtdebug(sizeof(x))
#define typenamee(x)     qtdebug(typeid(x).name())
#define isEmptyPtr(ptr)  ( (ptr == nullptr ) ? ("is nullptr") : ("is not nullptr") )
#define isnullnoreturn(ptr)      qtdebug(isEmptyPtr(ptr));
#define isnullreturn(ptr)\
{\
    bool flag = QString(isEmptyPtr(ptr)) == ("is nullptr") ? true : false;\
    if(flag){\
    qtdebug(isEmptyPtr(ptr));\
    return -1;}\
    else\
    qtdebug(isEmptyPtr(ptr));\
    return 0;\
    }
/*end*/

#define offset(type,membername)\
    (size_t)&(((type *)0)->membername)
#define container_of(member,type,membername)\
    (type*)( (size_t)&(member) - offset(type,membername) )
/*end*/
#else
#define cdebug(x)
#define cdebugs(x)
#define cppdebug(any)
#define cppdebugs(any)
#define qtdebug(any)
#define qtdebugs(any)
#endif

//元素地址值按照类型字节长递增
//连续的地址
//同一类型的集合
//指针和(数组)
//数组就是顺序的结构
/*
struct
{
    data
}
next  struct
{
    data
}
 next   struct
{
    data
}*/

//非连续的地址,逻辑上的地址
/*
 * struct   T
 * {
 *      int data;
 *      struct T *next;
 * }
 */

/*
 * struct T
 * {
 *      //head
 *      next{}<--->node{next{}<--->head->next}
 * }
 *
 *   数据结构四大要点:head/head->next/node/node->next
 *   head------------>access linklist用于访问链表,不存放数据
 *   node------------>new node,一个新的数据结点
 *   head->next-----> address of new node,数据结点的地址
 *   node->next----->old address of address of new node,上一个数据结点的地址,三者写的时候必须从后往前写才符合逻辑
 */


/*顺序表*/
struct Data
{
    int data [10];
};
struct sequece_table
{
    struct Data* next;
};
using DataIterator = int*;
const DataIterator begin(int*be)
{
    const int *start = new int(0) ;
    start = be;
    return (const DataIterator)start;
}
const DataIterator end(int*en)
{
    const int *end = new int(0) ;
    end = en;
    return (const DataIterator)end;
}
void sequece_table_test()
{
    sequece_table *ptr = (struct sequece_table*)malloc(sizeof(struct sequece_table));//new sequece_table();

    /*init*/
    ptr->next = new Data();

    /*insert data*/
    int i = 0;
    for(i;i<10;i++)
        ptr->next->data[i] =i;

    /*iterator*/
    for(DataIterator it = begin(ptr->next->data);it<=end(&(ptr->next->data[9]));it++)
        cppdebug(*it);
}
/*顺序表*/

/*链表*/
/*链表*/
typedef class forward_list
{
public:

    int data;//数据域
    struct forward_list* next;//指针域

}link_list;

void link_list_init(link_list *head)
{
    head->data = 0;
    head->next =NULL;
}
void link_list_head_insert(link_list *head,int val)
{

    link_list *node = (link_list *)malloc(sizeof(link_list));
    node->data = val;
    node->next =  head->next;
    head->next = node;

    //head->next/node/node->next
    //node------------>new node
    //head->next-----> address of new node
    //node->next----->old address of address of new node

}
void link_list_end_insert(link_list *head,int val)
{
    while(head->next !=nullptr)
        head = head->next;
    link_list_head_insert(head, val);
}

void link_list_browse(struct forward_list*head,int size=100)
{
    for(int i=size;i>0;i--)
    {
        if(head->next != NULL)
        {
            cppdebug(head->next->data);
            head->next = head->next->next;//遍历方式二,node->next = head->next->next;
        }
    }
}

int   link_list_find(struct forward_list*head,int aim_obj,int time)
{
    int index = 0;
    for(int i=time;i>0;i--)
    {
        if(head->next != NULL)
        {
            if(head->next->data == aim_obj)
            {
                cppdebug(head->next->data);
                return index;
            }

            head->next = head->next->next;
            index++;
        }
    }
    return -1;
}

template <typename T>
void freeMem(T *t)
{
    if(t!=nullptr)
    {
        free(t);
        t=nullptr;
    }
}

void link_list_delete_entry(struct forward_list*head,int aim_obj)
{

    //删除某个结点,直接指向下一个结点的下一个结点
    //head->next = head->next->next;
    //if(x !=null ) free(); x=null

    while(head->next != NULL )
    {
        if(head->next->data == aim_obj)
        {
            head->next = head->next->next;
            break;
        }
        head = head->next;//遍历方式一
    }

}

void link_list_test()
{
    link_list *head = (link_list *)malloc(sizeof(link_list));

    link_list_init(head);

    for(int i=0;i<15;i++)
    {
        //link_list_head_insert(head,i);
        link_list_end_insert(head,i);
    }

    // link_list_browse(head);

    // int index =link_list_find(head,3,11);

    //  cppdebugs("index is ")<<index<<std::endl;

    link_list_delete_entry(head,11);

    link_list_browse(head);
}

void callF()
{
    link_list_test();

}
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    callF();
    return a.exec();
}

 

posted @ 2022-05-14 21:39  cgy33  阅读(62)  评论(0)    收藏  举报