单向链表的基本操作

一、单链表的定义

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。单链表是最基础,最常用的一种数据结构,所以学好单链表是十分重要的。

二、单链表的基本操作

#ifndef LINK_FUNCTION_H
#define LINK_FUNCTION_H
#include <stdio.h>
#include <stdlib.h>
typedef int datastype;
typedef struct node{
    datastype data;
    struct node* next;
}listnode,*linklist;
//创建单链表,只生成头结点,不进行插入数据
linklist list_create1(void );
//创建单链表,生成头结点,并插入数据
linklist list_create2(void );
//遍历单链表,并打印数据
void list_show(linklist H);
//对单链表进行头插操作
void list_headinsert(linklist H,datastype value);
//根据节点的位置进行查找数据
linklist list_locate_pos(linklist H,int pos);
//根据节点的数据域进行查找
linklist list_locate_value(linklist H,datastype value);
//插入数据
void list_insert_pos(linklist H,int pos,datastype data);
//删除数据
datastype list_pop(linklist H,int pos);
//有序插入数据
void list_orderinsert(linklist H,datastype value);
#endif //LINK_FUNCTION_H

三,具体实现

#include "function.h"
linklist list_create1(void ){
    linklist H;
    H= malloc(sizeof(listnode));
    if(!H){
        printf("malloc failed\n");
        return NULL;
    }
    H->next=NULL;
    return H;
}
linklist list_create2(void ){
    linklist H,p,r;
    int sign=0;
    H= malloc(sizeof(listnode));
    if(!H){
        printf("malloc failed\n");
        return NULL;
    }
    H->next=NULL;
    r=H;
    while (1){
        printf("input sign(continue to input 0,stop 1)\n");
        scanf("%d",&sign);
        if(sign==1){
            return H;
        }

        p= malloc(sizeof(listnode));
        if(!p){
            printf("malloc failed\n");
            return NULL;
        }
        printf("input value\n");
        scanf("%d",&p->data);
        p->next=r->next;
        r->next=p;
        r=r->next;
    }

}
void list_show(linklist H){
    linklist p;
    p=H;
    while (p->next){
        printf("%d\n",p->next->data);
        p=p->next;
    }
}
void list_headinsert(linklist H,datastype value){
    linklist p;
    p= malloc(sizeof(listnode));
    if(!p){
        printf("malloc failed\n");
        return;
    }
    p->data=value;
    p->next=H->next;
    H->next=p;
}
linklist list_locate_pos(linklist H,int pos){
    //判断用户给的pos是否合理,如果pos<=0,立即返回NULL
    if(pos<=0){
        printf("pos is illegal\n");
        return NULL;
    }
    linklist p;
    p=H;
    int tmp=0;
    //while循环有双层条件。是为了防止pos值过大,超过单链表的总节点数量
    while (p->next && tmp<pos){
        p=p->next;
        tmp++;
    }
    if(tmp==pos)
    return p;
    //pos过大
    else
        return NULL;
}
linklist list_locate_value(linklist H,datastype value){
    linklist p;
    p=H;
    while (p->next){
        if(p->next->data==value)
            return p->next;
        else
            p=p->next;
    }
    printf("unfound the value\n");
    return NULL;
}
void list_insert_pos(linklist H,int pos,datastype data){
    linklist p,r;
    p= malloc((sizeof(listnode)));
    if(!p){
        printf("malloc failed\n");
        return;
    }
    p->data=data;
    if(pos==1){
        r=H;
    } else{
        r= list_locate_pos(H,pos-1);
    }
    p->next=r->next;
    r->next=p;
}
datastype list_pop(linklist H,int pos){
    linklist p,r;
    if(pos<=0){
        printf("pos is illegal\n");
        exit(0);
    } else if(pos==1){
        p=H;
    } else{
        p= list_locate_pos(H,pos-1);
    }
    r=p->next;
    p->next=r->next;
    return r->data;
}
void list_orderinsert(linklist H,datastype value){
    linklist p,r;
    p= malloc(sizeof(listnode));
    if(!p){
        printf("malloc failed\n");
        return;
    }
    r=H;
    p->data=value;
    while(r->next && value>r->next->data){
        r=r->next;
    }
    p->next=r->next;
    r->next=p;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值