day5 链表每日学习

文章展示了如何使用C语言实现双向链表和循环链表的数据结构,包括创建头节点、插入元素(头插法和尾插法)、删除元素(头删和尾删)、按位置插入、查找和修改等操作。代码详细定义了链表节点结构,并提供了相应的函数实现。
摘要由CSDN通过智能技术生成
#ifndef __H2_H__
#define __H2_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef char datatype;

typedef struct Node
{
        union
        {
            int len;
            datatype data;
        };
        struct Node *next;
        struct Node *prev;
}*DoubleLink;


DoubleLink create_head();
DoubleLink create_node();
int insert_head(DoubleLink L,datatype e);
int output(DoubleLink L);
int insert_rear(DoubleLink L,datatype e);
int delete_head(DoubleLink L);
int delete_rear(DoubleLink L);
int insert_by_pos(DoubleLink L,int pos,datatype e);
int search_by_pos(DoubleLink L,int pos);
int delete_by_pos(DoubleLink L,int pos);
int update_by_pos(DoubleLink L,int pos,datatype e);
#endif

#include "h2.h"
int main(int argc, const char *argv[])
{
    DoubleLink L=create_head();

    int n;
    datatype e;
    int pos;
    printf("请输入插入的个数");
    scanf("%d%*c",&n);
    for(int i=0;i<n;i++)
    {
        printf("请输入第%d个值",i+1);
        scanf("%c%*c",&e);
        insert_head(L,e);
    }
    output(L);
#if 0
    printf("请输入想要在头部插入的值");
    scanf("%c%*c",&e);
    insert_head(L,e);
    output(L);

    printf("请输入想要在尾部插入的值");
    scanf("%c%*c",&e);
    insert_rear(L,e);
    output(L);

    printf("删除头部的值");
    delete_head(L);
    output(L);

    printf("删除尾部的值");
    delete_rear(L);
    output(L);


    printf("请输入想要插入的位置");
    scanf("%d%*c",&pos);
    printf("请输入想要修改的值");
    scanf("%c%*c",&e);
    insert_by_pos(L,pos,e);
    output(L);

    printf("请输入想要查找的位置");
    scanf("%d%*c",&pos);
    search_by_pos(L,pos);
    output(L);

    printf("请输入想要删除的位置");
    scanf("%d%*c",&pos);
    delete_by_pos(L,pos);
    output(L);
#endif
    printf("请输入想要修改的位置");
    scanf("%d%*c",&pos);
    printf("请输入想要修改的值");
    scanf("%c%*c",&e);
    update_by_pos(L,pos,e);    
    output(L);
    return 0;
}
#include "h2.h"
//创建头节点空间
DoubleLink create_head()
{
    DoubleLink L=(DoubleLink)malloc(sizeof(struct Node));
    if(L==NULL)
        return NULL;
    L->len=0;
    L->next=NULL;
    L->prev=NULL;
    return L;

}
//创建节点空间

DoubleLink create_node()
{
    DoubleLink p=(DoubleLink)malloc(sizeof(struct Node));
    if(p==NULL)
        return NULL;
    p->data=0;
    p->next=NULL;
    p->prev=NULL;
    return p;

}
//头结点位置插入
int insert_head(DoubleLink L,datatype e)
{
    //判断头结点是否存储
    if(L==NULL)
        return -1;
    //创建新节点
    DoubleLink p=create_node();
    if(p==NULL)
        return-1;
    //新节点p的数据域
    p->data=e;
    //p的指针域
    p->next=L->next;
    p->prev=L;
    if(L->next!=NULL)
        L->next->prev=p;
    L->next=p;
    L->len++;
    return 0;
}

//双向遍历
int output(DoubleLink L)
{
    DoubleLink p=L->next;

    printf("\n正向输出\n");
    while(p!=NULL)
    {
        printf("%c\t",p->data);
        p=p->next;
    }
    putchar('\n');

}
//尾部插入
int insert_rear(DoubleLink L,datatype e)
{
    if(L==NULL)
    return -1;
    DoubleLink p=L;
    while(p->next)
    {
        p=p->next;
    }
    //创建要插入的节点
    DoubleLink s=create_node();
    if(s==NULL)
        return -1;
    s->data=e;

    s->next=p->next;//可以不写,创建的新节点的时候就赋值NULL了
    p->next=s;
    s->prev=p;
    L->len++;
}
//头部删除
int delete_head(DoubleLink L)
{
    if(L==NULL|| L->len==0)
    {
        return -1;
    }
    DoubleLink q=L->next;
    DoubleLink p=L;
    L->next=q->next;
    if(q->next!=NULL)
        q->next->prev=L;
    free(q);
    q=NULL;
    L->len--;
}
//尾部删除
int delete_rear(DoubleLink L)
{
    if(L==NULL || L->len==0)
        return -1;
    DoubleLink p=L;
    for(int i=0;i<L->len-1;i++)
    {
        p=p->next;
    }
    DoubleLink q;
    p->next=q;
    p->next=NULL;
    free(q);
    q=NULL;
}
//按位置插入
int insert_by_pos(DoubleLink L,int pos,datatype e)
{
    //判断节点是否存在
    //判断位置
    if(L==NULL || pos<1 || pos >L->len+1)
    {
        return    -1;
    }
    DoubleLink p=L;
    for(int i=0;i<pos-1;i++)
    {
        p=p->next;
    }
    DoubleLink s=create_node();
    s->data=e;
    s->next=p->next;
    s->prev=p;
    if(p->next!=NULL)
        p->next->prev=s;
    p->next=s;
    L->len++;
    return 0;

}
//按位置查找
int search_by_pos(DoubleLink L,int pos)
{
    if(L==NULL || pos<1 || pos>L->len)
    {
        return -1;
    }
    DoubleLink p=L;
    for(int i=0;i<pos;i++)
    {
        p=p->next;
    }
    printf("值是%c",p->data);
    return 0;
}
//按位置删除
int delete_by_pos(DoubleLink L,int pos)
{
    
    if(L==NULL || pos<1 || pos>L->len)
    {
        return -1;
    }
    DoubleLink p=L;
    for(int i=0;i<pos-1;i++)
    {
        p=p->next;
    }
    DoubleLink s=p->next;
    p->next=s->next;
    s->next->prev=p;
    free(s);
    s=NULL;
    L->len--;
}
//按位置修改值
int update_by_pos(DoubleLink L,int pos,datatype e)
{
    
    if(L==NULL || pos<1 || pos>L->len)
    {
        return -1;
    }
    DoubleLink p=L;
    for(int i=0;i<pos;i++)
    {
        p=p->next;
    }
    p->data=e;

}

循环单链

#ifndef __H3_H__
#define __H3_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef float datatype;
typedef struct Node
{
    
    union
    {
        int len ;
        datatype data;
    };
    struct Node *next;
}*LoopLink;

LoopLink create_head();
LoopLink crteate_node();
void output(LoopLink L);
int insert_head(LoopLink L,datatype e);
int insert_rear(LoopLink L,datatype e);
int delete_head(LoopLink L);
int delete_rear(LoopLink L);
#endif 
#include "h3.h"
int main(int argc, const char *argv[])
{
    LoopLink L=create_head();
    int n;
    int pos;
    datatype  e;

    printf("1.请输入插入元素的个数");
    scanf("%d%*c",&n);
    for(int i=0;i<n;i++)
    {
        printf("请输入第%d个值",i+1);
        scanf("%f%*c",&e);
        insert_head(L,e);
    //    insert_rear(L,e);
    }
    output(L);

    printf("2.请输入想要在头部插入的值");
    scanf("%f%*c",&e);
    insert_head(L,e);
    output(L);

    
    printf("3.请输入想要在尾部插入的值");
    scanf("%f%*c",&e);
    insert_rear(L,e);
    output(L);

    printf("进行头部删除");
    delete_head(L);
    putchar('B');
    output(L);
    

    printf("进行尾部删除");
    delete_rear(L);
    output(L);
    return 0;
}
#include "h3.h"
//创建头节点
LoopLink create_head()
{
    LoopLink  L=(LoopLink)malloc(sizeof(struct Node));
    if(L==NULL)
        return NULL;
    L->len=0;
    L->next=L;
    return L;
}
//创建中间节点空间
LoopLink create_node()
{
    LoopLink p=(LoopLink)malloc(sizeof(struct Node));
        if(p==NULL)
            return NULL;
    p->data=0;
    p->next=NULL;
    return p;
}
//函数3 遍历输出
void output(LoopLink L)
{
    LoopLink p=L;
     while(p->next!=L)
    {
        p=p->next;
        printf("%.2f\t",p->data);
    }
    printf("\n");
}
//从头部插入
int insert_head(LoopLink L,datatype e)
{

    if(L==NULL)
    return -1;
    LoopLink p=create_node();
    p->data=e;
    p->next=L->next;
    L->next=p;
    L->len++;
    return 0;
}
//从尾部插入
int insert_rear(LoopLink L,datatype e)
{
    if(L==NULL)
        return -1;
    LoopLink p=L;
    while(p->next!=L)
    {
        p=p->next;
    }
    LoopLink s=create_node();
    s->data=e;
    s->next=p->next;
    p->next=s;
    L->len++;
    return 0;
}
//链表头部删除
int delete_head(LoopLink L)
{    
    if(L==NULL)
        return -1;
    LoopLink p=L->next;
    L->next=p->next;
    free(p);
    p=NULL;
    L->len--;
    return 0;


}

//链表尾部删除
int delete_rear(LoopLink L)
{
    if(L==NULL)
        return -1;
    LoopLink p=L;
    while(p->next->next!=L)
    {
        p=p->next;
    
    }
    LoopLink q=p->next;
    p->next=L;
    free(q);
    q=NULL;
    L->len--;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值