1.定义链表(重点)

typedef struct link {
    int  elem;
    struct link * next;
}Link;

2.初始化链表(重点)

Link* a() {
    int i;
    Link* p = NULL;
    Link* temp = (Link*)malloc(sizeof(Link));
    temp->elem = 0;
    temp->next = NULL;
    p = temp;
    for (i = 1; i < 5; i++) {
        Link* a = (Link*)malloc(sizeof(Link));
        a->elem = i;
        a->next = NULL;
        temp->next = a;
        temp = temp->next;
    }
    return p;
}

3.向链表中插入元素

void b(Link* p, int elem, int add) {
    int i;
    Link* c = NULL;
    Link* temp = p;
    for (i = 1; i < add; i++) {
        temp = temp->next;
        if (temp == NULL) {
            printf("插入位置无效\n");
            return;
        }
    }
    c = (Link*)malloc(sizeof(Link));
    c->elem = elem;
    c->next = temp->next;
    temp->next = c;
}

4.删除元素

int c(Link* p, int elem) {
    Link* del = NULL, *temp = p;
    int find = 0;
    while (temp->next) {
        if (temp->next->elem == elem) {
            find = 1;
            break;
        }
        temp = temp->next;
    }
    if (find == 0) {
        return -1;
    }
    else
    {
        del = temp->next;
        temp->next = temp->next->next;
        free(del);
        return 1;
    }
}

5.查找元素

int d(Link* p, int elem) {
    int i = 1;
    p = p->next;
    while (p) {
        if (p->elem == elem) {
            return i;
        }
        p = p->next;
        i++;
    }
    return -1;
}

6.更改元素值

int e(Link* p, int oldElem, int newElem) {
    p = p->next;
    while (p) {
        if (p->elem == oldElem) {
            p->elem = newElem;
            return 1;
        }
        p = p->next;
    }
    return -1;
}

7.输出链表

void f(Link* p) {
    p = p->next;
    while (p) {
        printf("%d ", p->elem);
        p = p->next;
    }
    printf("\n");
}

8.释放链表(重点)

void g(Link* p) {
    Link* fr = NULL;
    while (p->next)
    {
        fr = p->next;
        p->next = p->next->next;
        free(fr);
    }
    free(p);
}

9.综合程序

#include <bits/stdc++.h>
#include <cstdlib>
typedef struct link {
    int  elem;
    struct link * next;
}Link;
Link* a() {
    int i;
    Link* p = NULL;
    Link* temp = (Link*)malloc(sizeof(Link));
    temp->elem = 0;
    temp->next = NULL;
    p = temp;
    for (i = 1; i < 5; i++) {
        Link* a = (Link*)malloc(sizeof(Link));
        a->elem = i;
        a->next = NULL;
        temp->next = a;
        temp = temp->next;
    }
    return p;
}
void c(Link* p, int elem, int add) {
    int i;
    Link* c = NULL;
    Link* temp = p;
    for (i = 1; i < add; i++) {
        temp = temp->next;
        if (temp == NULL) {
            printf("插入位置无效\n");
            return;
        }
    }
    c = (Link*)malloc(sizeof(Link));
    c->elem = elem;
    c->next = temp->next;
    temp->next = c;
}
int d(Link* p, int elem) {
    Link* del = NULL, *temp = p;
    int find = 0;
    while (temp->next) {
        if (temp->next->elem == elem) {
            find = 1;
            break;
        }
        temp = temp->next;
    }
    if (find == 0) {
        return -1;
    }
    else
    {
        del = temp->next;
        temp->next = temp->next->next;
        free(del);
        return 1;
    }
}
int e(Link* p, int elem) {
    int i = 1;
    p = p->next;
    while (p) {
        if (p->elem == elem) {
            return i;
        }
        p = p->next;
        i++;
    }
    return -1;
}
int f(Link* p, int oldElem, int newElem) {
    p = p->next;
    while (p) {
        if (p->elem == oldElem) {
            p->elem = newElem;
            return 1;
        }
        p = p->next;
    }
    return -1;
}
void b(Link* p) {
    p = p->next;
    while (p) {
        printf("%d ", p->elem);
        p = p->next;
    }
    printf("\n");
}
void g(Link* p) {
    Link* fr = NULL;
    while (p->next)
    {
        fr = p->next;
        p->next = p->next->next;
        free(fr);
    }
    free(p);
}
using namespace std;
int main() {
	long long a1,a2,a3,a4,a5,a6;
    Link* p = a();
    cout<<"初始化链表"<<endl; 
    b(p);
    cout<<"请输入要在第几行插入数字几"<<endl; 
    cin>>a1>>a2;
    c(p, a2, a1);
    b(p);
    cout<<"请输入要删除元素"<<endl;
    cin>>a3;
    d(p, a3);
    b(p);
    cout<<"请输入要查找的元素"<<endl; 
    cin>>a4;
	cout<<"元素"<<a4<<"在链表中的"<<e(p, a4)<<endl;
	cout<<"请输入需要更改的元素和新元素的值"<<endl;
    cin>>a5>>a6;
    f(p, a5, a6);
    b(p);
    char str1;//下面是装逼代码
    cout<<"输入  s  释放链表"<<endl;
	cin>>str1;//输入任意字符取消装逼但仍然释放链表
	if(str1=='s'){
    cout<<"正在释放链表......"<<endl; 
    for(long long i=1;i<=4;i++){
    cout << "\aOperation \"HyperHype\" is now activated!\n";
    cout << "Enter your agent code:________\b\b\b\b\b\b\b\b";
    long code=rand();
    cout << "\aYou entered " << code << "...\n";
    cout << "\aCode Verified! Proceed with Plan Z3!\n";
	}
    g(p);
	cout<<"释放链表成功";
	}else{
	g(p);//要删千万不要删这句
    }//到这里基本都是装逼可删除
    return 0;
}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐