链表的创建及其插入删除花式查询操作

前言

个人小记


一、结构定义

typedef struct Node
{
    int data;
    struct Node* next;
}Node;

二、初始化

Node* init_Node(int val)
{
    Node* node=(Node*)malloc(sizeof(Node));
    node->data=val;
    node->next=NULL;
    return node;
}

三、插入

Node* insert(Node* head,int pos,int val)
{
    int len=linklist_len(head);
    if(pos>len)
    {
        printf("插入位置不合法\n");
        return head;
    }
    Node New_head;
    New_head.next=head;
    Node* p=&New_head;
    for(int i=0;i<pos;i++)p=p->next;
    Node* node=init_Node(val);
    node->next=p->next;
    p->next=node;
    return New_head.next;
}

四、删除

Node* delete(Node* head,int val)
{
    if(head==NULL)
    {
        printf("链表为空,无法删除\n");
        return head;
    }
    Node New_head;
    New_head.next=head;
    Node*q=&New_head;
    Node* p=head;
   while(p!=NULL&&p->data!=val)
    {
        q=p;
        p=p->next;
    }
    if(p==NULL)
    {
        printf("查无此值,无法删除\n");
        return New_head.next;
    }
    q->next=p->next;
    free(p);
    printf("删除成功\n");
    return New_head.next;
}

五、花式查询

void search(Node* head,int val)
{
    if(head==NULL)
    {
        printf("链表为空\n");
        return ;
    }
    Node* p=head;
    int len=0;
    while(p&&p->data!=val)
    {
        len++;
        p=p->next;
    }
    if(p==NULL)
    {
        printf("查无此值\n");
        return ; 
    }
    print(head);
    for(int i=0;i<(2*len+1)*2-1;i++)
    {
        printf(" ");
    }
    printf("^\n");
    for(int i=0;i<(2*len+1)*2-1;i++)
    {
        printf(" ");
    }
    printf("|");
    printf("\n\n\n\n");
    return ;
}

总代码

#include<stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Node
{
    int data;
    struct Node* next;
}Node;

Node* init_Node(int val)
{
    Node* node=(Node*)malloc(sizeof(Node));
    node->data=val;
    node->next=NULL;
    return node;
}


int linklist_len(Node* head)
{
    int len=0;
    while(head)
    {
        len++;
        head=head->next;
    }
    return len;
}

Node* insert(Node* head,int pos,int val)
{
    int len=linklist_len(head);
    if(pos>len)
    {
        printf("插入位置不合法\n");
        return head;
    }
    Node New_head;
    New_head.next=head;
    Node* p=&New_head;
    for(int i=0;i<pos;i++)p=p->next;
    Node* node=init_Node(val);
    node->next=p->next;
    p->next=node;
    return New_head.next;
}

Node* delete(Node* head,int val)
{
    if(head==NULL)
    {
        printf("链表为空,无法删除\n");
        return head;
    }
    Node New_head;
    New_head.next=head;
    Node*q=&New_head;
    Node* p=head;
   while(p!=NULL&&p->data!=val)
    {
        q=p;
        p=p->next;
    }
    if(p==NULL)
    {
        printf("查无此值,无法删除\n");
        return New_head.next;
    }
    q->next=p->next;
    free(p);
    printf("删除成功\n");
    return New_head.next;
}

void clear_linklist(Node* head)
{
    if(head==NULL)return ;
    for(Node* p=head,*q;p;p=q)
    {
        q=p->next;
        free(p);
    }
    return ;
}

void print(Node* head)
{
    printf("当前链表为:\n");
    int len=0,f=0;
    while(head)
    {
        len+=printf("%2d",head->data);
        f++;
        len+=printf("->");
        head=head->next;
    }
    printf("\n");
    for(int i=0;i<len;i++)
    {
        printf("-");
    }
    printf("\n");
    for(int i=0;i<f;i++)
    {
        printf("%3d",i);
    }
    printf("\n");
    return ;
}

void search(Node* head,int val)
{
    if(head==NULL)
    {
        printf("链表为空\n");
        return ;
    }
    Node* p=head;
    int len=0;
    while(p&&p->data!=val)
    {
        len++;
        p=p->next;
    }
    if(p==NULL)
    {
        printf("查无此值\n");
        return ; 
    }
    print(head);
    for(int i=0;i<(2*len+1)*2-1;i++)
    {
        printf(" ");
    }
    printf("^\n");
    for(int i=0;i<(2*len+1)*2-1;i++)
    {
        printf(" ");
    }
    printf("|");
    printf("\n\n\n\n");
    return ;
}


int main()
{
    srand((unsigned)time(0));
    Node* head=NULL;
    int t,flg=0,pos,val;
    print(head);
    while(printf("插入位置从0开始,输入0为插入,输入1为删除,输入2为查询,请输入:")&&scanf("%d",&t)!=EOF)
    {
        switch(t)
        {
            case 0:
            printf("输入插入的位置与插入的值:");
            scanf("%d%d",&pos,&val);
            head=insert(head,pos,val);
            print(head);
            break;
            case 1:
            printf("请输入要删除的值:");
            scanf("%d",&val);
            head=delete(head,val);
            print(head);
            break;
            case 2:
            printf("请输入要查找的值:\n");
            scanf("%d",&val);
            search(head,val);
            print(head);
        }
    }

    clear_linklist(head);
    return 0;
}
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值