链表基本操作 c++描述

//
// Created by 周亮 on 2021/11/2.
//
#include "iostream"

typedef struct st{
    char *data;
    struct st *next;
}lNode;

void init(lNode *fp);
bool empty(lNode *fp);
void headInsert(lNode *fp, char *data);
void tailInsert(lNode *fp, char *data);
void insertPrior(lNode *p, char *data);
void deleteNode(lNode *fp);

int main(){
    lNode *fp = (lNode*) malloc(sizeof (lNode*));
    init(fp);
    headInsert(fp, "first");
    headInsert(fp, "second");
    tailInsert(fp, "third");
    deleteNode(fp);
    int a = 1;
    return 0;
}

void init(lNode *fp){
    fp->data = nullptr;
    fp->next = nullptr;
}

bool empty(lNode *fp){
    if (fp->next == nullptr){
        return true;
    }
    return false;
}

void headInsert(lNode *fp, char *data){
    lNode *p = (lNode *) malloc(sizeof (lNode));
    p->data = data;
    p->next = fp->next;
    fp->next = p;
}

void tailInsert(lNode *fp, char *data){
    lNode *p = (lNode *) malloc(sizeof (lNode));
    p->data = data;
    lNode *s =fp;
    while (s->next != nullptr){
        s = s->next;
    }
    p->next = s->next;
    s->next = p;

}

void insertBack(lNode *p, char *data){
    headInsert(p, data);
}

void insertPrior(lNode *p, char *data){//先在后面添加一个node,把p指向的那个node上的数据改成要添加的数据,再把p后面那个node的数据改成p的数据
    lNode *s = (lNode *) malloc(sizeof(lNode));
    s->data = p->data;
    s->next = p->next;
    p->next = s;
    p->data = data;


}

void deleteNode(lNode *fp){
    lNode *s = fp;
    while (s->next != nullptr){
        lNode *tmp = s;
        s = s->next;
        free(tmp);
    }
}
上一篇:浮点异常


下一篇:C语言 fread 函数 - C语言零基础入门教程