链表大杂烩(增删改查)——C语言版

具有对链表进行增删改查的功能

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

struct ListNode{
    int val;
    struct ListNode *next;
};

void Menu();
struct ListNode *Create(struct ListNode *head);
void Print(struct ListNode *head);
struct ListNode *Head_Add(struct ListNode *head , int target , int insert);
struct ListNode *Tail_Add(struct ListNode *head , int target , int insert);
struct ListNode *Delete(struct ListNode *head , int target);
void Search(struct ListNode *head , int locate);
struct ListNode *Modify(struct ListNode *head , int target , int new_val);

int main(){
    int option,target,insert,new_val,locate;
    struct ListNode *head = NULL;
    while(1){
        Menu();
        printf("请输入功能序号:");
        scanf("%d",&option);
        switch(option){
            case 1 :
                if(head != NULL){
                    printf("链表已被创建,添加数据请选择对应选项!\n");
                    break;
                }
                head = Create(head);
                break;
            case 2 :
                if(head == NULL){
                    printf("请先创建链表!\n");
                    break;
                }
                Print(head);
                break;
            case 3 :
                if(head == NULL){
                    printf("请先创建链表!\n");
                    break;
                }
                printf("请输入被插入的数据:");
                scanf("%d",&target);
                printf("请输入插入的数据:");
                scanf("%d",&insert);
                head = Head_Add(head , target , insert);
                break;
            case 4 :
                if(head == NULL){
                    printf("请先创建链表!\n");
                    break;
                }
                printf("请输入被插入的数据:");
                scanf("%d",&target);
                printf("请输入插入的数据:");
                scanf("%d",&insert);
                head = Tail_Add(head , target , insert);
                break;
            case 5 :
                if(head == NULL){
                    printf("请先创建链表!\n");
                    break;
                }
                printf("请输入删除的数据:");
                scanf("%d",&target);
                head = Delete(head , target);
                break;
            case 6 :
                if(head == NULL){
                    printf("请先创建链表!\n");
                    break;
                }
                printf("请输入查找数据的位置:");
                scanf("%d",&locate);
                Search(head , locate);
                break;
            case 7 :
                if(head == NULL){
                    printf("请先创建链表!\n");
                    break;
                }
                printf("请输入需要修改的数据:");
                scanf("%d",&target);
                printf("请输入修改后的数据:");
                scanf("%d",&new_val);
                head = Modify(head , target , new_val);
                break;
            case 8 :
                printf("退出成功!\n");
                return 0;
            default :
                printf("请输入正确的序号!\n");
                break;
        }
    }
}

void Menu() {
    printf("-------------------------链表功能-------------------------\n");
    printf("                       ①创建链表\n");
    printf("                       ②打印链表\n");
    printf("                       ③头插法\n");
    printf("                       ④尾插法\n");
    printf("                       ⑤删除数据\n");
    printf("                       ⑥查找数据\n");
    printf("                       ⑦修改数据\n");
    printf("                       ⑧退出\n");
    printf("--------------------------------------------------------\n");
}

struct ListNode *Create(struct ListNode *head){
    struct ListNode *p,*q;
    q = NULL;
    int count,n;
    count = 0;
    printf("请输入链表的数据(输入-1停止录入):");
    while(1){
        scanf("%d",&n);
        if(n == -1)  break;
        p = (struct ListNode *)malloc(sizeof(struct ListNode));
        p->val = n;
        if(count == 0){
            head = p;
            count++;
        }
        else  q->next = p;
        q = p;
    }
    if(q != NULL)  q->next = NULL;
    printf("创建成功!\n");
    return head;
}

void Print(struct ListNode *head){
    printf("已存储链表为:");
    while(head != NULL){
        printf("%d ",head->val);
        head = head->next;
    }
    printf("\n");
}

struct ListNode *Head_Add(struct ListNode *head , int target , int insert){
    struct ListNode *pre,*cur,*new_list;
    cur = head;
    while(cur != NULL && cur->val != target){
        pre = cur;
        cur = cur->next;
    }
    if(cur == NULL){
        printf("查无此数!\n");
        return head;
    }
    new_list = (struct ListNode *)malloc(sizeof(struct ListNode));
    new_list->val = insert;
    pre->next = new_list;
    new_list->next = cur;
    printf("添加成功!\n");
    return head;
}

struct ListNode *Tail_Add(struct ListNode *head , int target , int insert){
    struct ListNode *h,*new_list,*temp;
    h = head;
    while(h != NULL && h->val != target)  h = h->next;
    if(h == NULL){
        printf("查无此数!\n");
        return head;
    }
    temp = h->next;
    new_list = (struct ListNode *)malloc(sizeof(struct ListNode));
    new_list->val = insert;
    h->next = new_list;
    new_list->next = temp;
    printf("添加成功!\n");
    return head;
}

struct ListNode *Delete(struct ListNode *head , int target){
    struct ListNode *cur,*pre,*temp;
    cur = head;
    while(cur != NULL && cur->val != target){
        pre = cur;
        cur = cur->next;
    }
    if(cur == NULL){
        printf("查无此数!\n");
        return head;
    }
    temp = cur->next;
    pre->next = temp;
    printf("删除成功!\n");
    return head;
}

void Search(struct ListNode *head , int locate){
    int count = 0;
    while(head != NULL){
        count++;
        if(count == locate){
            printf("%d位的数据为:%d\n",locate,head->val);
            break;
        }
        head = head->next;
    }
    if(head == NULL)  printf("查无此数!\n");
}

struct ListNode *Modify(struct ListNode *head , int target , int new_val){
    struct ListNode *cur;
    cur = head;
    while(cur != NULL && cur->val != target)  cur = cur->next;
    if(cur == NULL){
        printf("查无此数!\n");
        return head;
    }
    while(cur != NULL){
        if(cur->val == target)  cur->val = new_val;
        cur = cur->next;
    }
    printf("修改成功!\n");
    return head;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值