链表-单链表

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef struct SNode {
    char data[100]; //数据域
    struct SNode *next; //指针域
} SNode;

//获取输入
void getInput(SNode *sNode) {
    printf("请输入data:");
    scanf("%s", sNode->data);
}

// 尾插法
void tailInsertData(SNode *sNode) {
    // 创建节点
    SNode *data;
    data = (SNode *) malloc(sizeof(SNode));
    if (data == NULL) {
        printf("申请地址失败\n");
        exit(1);
    }
    getInput(data);

    // 尾插法
    while (sNode->next != NULL) { //查询最后一个节点
        sNode = sNode->next;
    }

    sNode->next = data;
    data->next = NULL;
}

// 头插法
void headInsertData(SNode *sNode) {
    // 创建节点获取数据
    SNode *data;
    data = (struct SNode *) malloc(sizeof(struct SNode));
    if (data == NULL) {
        printf("申请地址失败\n");
        exit(1);
    }
    getInput(data);

    // 进行插入处理
    SNode *temp;
    if (sNode->next != NULL) {  // 判断首元结点是否为空
        temp = sNode->next; //记录首元结点位置
        sNode->next = data; // 在头结点后面插入数据
        data->next = temp;  // 将新插入的节点的下一个节点指向首元结点位置
    } else {
        sNode->next = data;
        data->next = NULL;
    }

}

//展示数据
void display(SNode *sNode) {
    while (sNode != NULL) {  //第一个是头结点自定义
        printf("data = %s\n", sNode->data);
        sNode = sNode->next;
    }
}

// 计算链表的长度
int calLength(SNode *sNode) {
    int count = 0;
    SNode *p;
    p = sNode->next; // 从首原地址地址开始
    while (p != NULL) {
        p = p->next;
        count += 1;
    }
    printf("链表的长度为:%d\n", count);
    return count;
}

// 销毁链表
void destroy(SNode **sNode) {  // **sNode 首地址
    if(*sNode == NULL){
        printf("链表已销毁\n");
    }else{
        SNode *p = NULL;
        while (*sNode) {  // 从头结点开始销毁
            p = *sNode;
            *sNode = (*sNode)->next;   // (*sNode)->next  第一次指向的是头结点
            free(p);
        }
        printf("destroy success!\n");
    }
}

// 清空链表
void clear(SNode **sNode){
    if((*sNode)->next == NULL){
        printf("链表已为空\n");
    }else{
        SNode *p, *q;

        p = (*sNode)->next; // 将头结点中 指针域保存的首元结点地址给 p
        (*sNode)->next = NULL; // 将头结点中的指针域置为0

        while(p){  // 从首元结点开始销毁
            q = p;
            p = p->next;
            free(q);
        }
    }
}

// 删除链表尾部的数据
void deleteTailData(SNode *sNode){
    if(sNode->next == NULL){
        printf("表为空,无法删除,可以销毁表\n");
    }else{
        SNode *last;
        while (sNode->next){
            last = sNode;
            sNode = sNode->next;
        }
        last->next = NULL;
        free(sNode->next);
        printf("成功删除尾部数据\n");
    }
}

// 删除链表头的数据
void deleteHeadData(SNode *sNode){
    if(sNode->next == NULL){
        printf("表为空,无法删除,可以销毁表\n");
    }else{
        SNode *head;
        head = sNode->next;  // 首元结点给head
        if(head->next){
            sNode->next = head->next;
        }else{
            sNode->next = NULL;
        }
        free(head);
        printf("成功删除头部数据\n");
    }
}

// 删除链表第i个位置的元素
void deleteIDate(SNode *sNode, int i) {
    if(i > calLength(sNode)){
        printf("链表长度不够\n");
    }else{
        int count = 0;
        SNode *pro, *next;
        if(i == 1){
            pro = sNode->next;
            sNode->next = pro->next;
            free(pro);
            printf("已经删除\n");
        }else{
            while (sNode != NULL) {  //第一个是头结点自定义
                count += 1;
                pro = sNode;  // 记录上一个位置
                if(count == i){
                    break;
                }
                sNode = sNode->next;
            }
            next = sNode->next->next;  // sNode->next是要删除的节点位置
            pro->next = next;
            free(next);
            printf("已经删除\n");
        }
    }
}

//查找第i个元素
void queryIDate(SNode *sNode, int i) {
    if(i> calLength(sNode)){
        printf("链表长度不够\n");
    }else{
        int count = 0;
        while (sNode != NULL) {  //第一个是头结点自定义
            count += 1;
            sNode = sNode->next;
            if(count == i){
                printf("第 %d 个元素是:%s\n", count, sNode->data);
            }
        }
    }
}

// 查找元素位置
int queryLocateData(SNode *sNode, char e[]){
    int count = -1;
    while (sNode != NULL) {  //第一个是头结点自定义
        count += 1;
        if(strcmp(sNode->data, e) == 0){
            printf("元素 %s 的位置是:%d\n", sNode->data, count);
        }
        sNode = sNode->next;
    }
    return count;
}

int main() {
    // 初识化链表
    struct SNode *l = NULL;
    l = (struct SNode *) malloc(sizeof(struct SNode));
    l->next = NULL;
    strcpy(l->data, "我是头结点");

    // 操作
    headInsertData(l);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值