嵌入式学习Day16

作业:

双向链表按位置删除

双向链表按位置修改

双向链表按位置查找

head.h

#ifndef __HEAD_H__
#define __HEAD_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 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 delete_by_pos(Doublelink L,int pos);
int update_by_pos(Doublelink L,int pos,datatype e);
int search_by_pos(Doublelink L,int pos);
void output(Doublelink L);
#endif

test.c

#include "head.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->data=e;
    p->next=L->next;
    p->prev=L;
    if(L->next!=NULL)
        L->next->prev=p;
    L->next=p;
    L->len++;
    return 0;


}
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;
    s->prev=p;
    p->next=s;
    L->len++;
    return 0;
}
void output(Doublelink L)
{
    Doublelink p=L;
    printf("正向结果是:\n");
    while(p->next)
    {
        p=p->next;
        printf("%c\t",p->data);
    }
    printf("逆向结果是:\n");
    while(p->prev)
    {
        printf("%c\t",p->data);
        p=p->prev;
    }
    printf("\n");

}
int delete_head(Doublelink L)

{

    if(L==NULL || L->len==0)
    {
        return -1;
    }
    Doublelink q=L->next;
    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;
    while(p->next)
    {
        p=p->next;
    }
       
    p->prev->next=NULL;
    free(p);
    p=NULL;
    L->len--;
    return 0;
}
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();
    if(p==NULL)
        return -1;
    //s的数据域
    s->data=e;
    //s的指针域
    s->next=p->next;
    s->prev=p;
    if(p->next!=NULL)
        p->next->prev=s;
    p->next=s;
    L->len++;

}
int delete_by_pos(Doublelink L,int pos)
{
    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 q=p->next;
    p->next=q->next;
    if(q->next!=NULL)
        q->next->prev=p;
    free(q);
    q=NULL;
    L->len--;

}
int update_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;i++)
    {
        p=p->next;
    }
    p->data=e;
    return 0;
}
int search_by_pos(Doublelink L,int pos)
{
    if(L==NULL||pos<1||pos>L->len+1)
    {
        return -1;
    }
    Doublelink p=L;
    for(int i=0;i<pos;i++)
    {
        p=p->next;
    }
    printf("查找的元素为:%c\n",p->data);
    return 0;
}

main.c

#include "head.h"
int main(int argc, const char *argv[])
{
    Doublelink L=create_head();
    if(L==NULL)
        return -1;
    int n,pos;
    datatype e;
    printf("请输入输入的个数:");
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        printf("请输入要插入的值:");
        scanf(" %c",&e);
        insert_rear(L,e);
    }
    output(L);
    printf("请输入删除元素的位置:");
    scanf("%d",&pos);
    delete_by_pos(L,pos);
    output(L);
    printf("请输入修改元素的位置和元素:");
    scanf("%d %c",&pos,&e);
    update_by_pos(L,pos,e);
    output(L);
    printf("请输入查找元素的位置:");
    scanf("%d",&pos);
    search_by_pos(L,pos);
    return 0;
}

单向循环链表尾插

单向循环链表头删

单向循环链表尾删

单向循环链表遍历

head.h

#ifndef __HEAD_H__
#define __HEAD_H__

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

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

Looplink create_head();
Looplink create_Node();
int insert_head(Looplink L,datatype e);
int insert_rear(Looplink L,datatype e);
int output(Looplink L);
int delete_head(Looplink L);
int delete_rear(Looplink L);


#endif

main.c

#include"head.h"
int main(int argc, const char *argv[])
{
    Looplink L=create_head();
    int n;datatype e;
    printf("输入数的个数:");
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        printf("输入头插的数值:");
        scanf("%f",&e);
        insert_head(L,e);
        //insert_rear(L,e);
    }
    printf("头插的结果是:");
    //printf("尾插的结果是:");
    output(L);
    printf("\n");

    //头删
    printf("头删后的结果:");
    delete_head(L);
    output(L);
    printf("\n");

    //尾删
    printf("尾删后的结果:");
    delete_rear(L);
    output(L);
    printf("\n");
    return 0;
}

test.c


#include"head.h"
//头结点
Looplink create_head()
{
    Looplink L=(Looplink)malloc(sizeof(struct Node));
    if(L==NULL)
        return NULL;
    L->next=L;
    L->len==0;
    return L;
}


//节点
Looplink create_Node()
{
    Looplink p=(Looplink)malloc(sizeof(struct Node));
    if(p==NULL)
        return NULL;
    p->next=NULL;
    p->data=0;
    return p;
}


//头插
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++;//表长度+1
    return 0;
}


//尾插
int insert_rear(Looplink L,datatype e)
{
    if(L==NULL||L->len==0)
        return -1;
    Looplink p=L;
    for(int i=0;i<L->len;i++)
    {
        p=p->next;
    }
    Looplink s=create_Node();
    
    s->next=p->next;//指针域
    p->next=s;
    L->len++;//插入表长度增加
    return 0;
}

//遍历
int output(Looplink L)
{
    Looplink p=L;
    for(int i=0;i<L->len;i++)
    {
        p=p->next;
        printf("%.2f\t",p->data);
    }
    return 0;
}


//头删
int delete_head(Looplink L)
{
    if(L==NULL||L->len==0)
        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||L->len==0)
        return -1;
    Looplink p=L;
    for(int i=0;i<L->len-1;i++)
    {
        p=p->next;
    }
    free(p->next);
    p->next=L;
    L->len--;
    return 0;
}

Nice2cu  21:47:23
#include"head.h"
int main(int argc, const char *argv[])
{
    Looplink L=create_head();
    int n;datatype e;
    printf("输入数的个数:");
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        printf("输入头插的数值:");
        scanf("%f",&e);
        insert_head(L,e);
        //insert_rear(L,e);
    }
    printf("头插的结果是:");
    //printf("尾插的结果是:");
    output(L);
    printf("\n");

    //头删
    printf("头删后的结果:");
    delete_head(L);
    output(L);
    printf("\n");

    //尾删
    printf("尾删后的结果:");
    delete_rear(L);
    output(L);
    printf("\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值