数据结构——单链表增删改查实操练习

非科班出身学生学习计算机数据结构课程实操练习,用以巩固学习知识,项目函数有实操验证且结果符合预期。

代码取用自由,放出来也是自己方便复习。

如有算法上的漏洞或建议,可在评论区提出。

目录

单链表数据节点定义

初始化单链表

表头插入法插入元素

表尾插入法插入元素

计算链表长度函数

单链表便利函数

单链表按位查找

单链表按值查找

单链表节点按位插入

单链表节点按位删除

附测试时主函数


单链表数据节点定义
typedef struct LNode
{
    int data;
    struct LNode* next;
} LNode, * LinkList;
初始化单链表
void InitLinkList(LinkList& L) {
    L = (LinkList)malloc(sizeof(LNode));
    L->next = NULL;
}
表头插入法插入元素
LinkList InsertHead(LinkList& L) {
    LNode* s;
    int x;
    InitLinkList(L);
    printf("please input a number (If you input the number 999 which means the end of inputing)\n");
    scanf("%d", &x);
    while (x != 999)
    {
        s = (LNode*)malloc(sizeof(LNode));
        s->data = x;
        s->next = L->next;
        L->next = s;
        scanf("%d",&x);/* code */
    }
    return L;
}
表尾插入法插入元素
LinkList InsertTail(LinkList &L){
    LNode* s,*t;
    int x;
    InitLinkList(L);
    printf("please input a number (If you input the number 999 which means the end of inputing)\n");
    scanf("%d", &x);
    t = L;
    while (x != 999)
    {
        s = (LNode*)malloc(sizeof(LNode));
        s->data = x;
        s->next = t->next;
        t->next = s;
        t = s;
        scanf("%d", &x);/* code */
    }
    return L;
}
计算链表长度函数

int length(LinkList L) {
    LNode* p = L->next;
    int len = 0;
    while (p)
    {
        len++;
        p = p->next;/* code */
    }
    return len;
}
单链表便利函数

void PrintfList(LNode* L) {
    if (L == NULL)
    {
        printf("error!\n");/* code */
    }
    while (L->next!=NULL)
    {
        printf("%d\n", L->next->data);
        L = L->next;/* code */
    }
}
单链表按位查找
LNode* FinebyPos(LinkList L, int i) {
    LNode* p = L->next;
    int j = 1;
    if (i == 0)
    {
        return L;
    }
    if (i < 1)
    {
        printf("error!\n");
    }
    while (p && j < i)
    {
        p = p->next;
        j++;
    }
    return p;
}
单链表按值查找
LNode* FinebyValue(LinkList L, int i) {
    LNode* p = L->next;
    int j = 1;
    while (p->data!=i)
    {
        p = p->next;
        if (p->next==NULL)
        {
            printf("it is not in this list!\n");
            return NULL;
        }
    }
    printf("it is in this list!\n");
    return p;
}
单链表节点按位插入

void Insert(LinkList& L) {
    LNode* p,*i;
    p = L->next;
    int x;
    int j = 1;
    int w;
    printf("please input the number you want to insert:\n");
    scanf("%d",&x);
    printf("please select the position of this element :\n");
    scanf("%d",&w);

    if (w<=0)
    {
        printf("error!\n");
    }
    i = (LNode*)malloc(sizeof(LNode));
    i->data = x;
    while (p&&j<w-1)
    {
        p = p->next;
        j++;
    }
    i->next = p->next;
    p->next = i;
}

单链表节点按位删除

void DeleteLNode(LinkList& L){
    LNode* p, * q;

    p = L->next;
    int j=1;
    int x;
    printf("please input the position of this element you want to erase:\n");
    scanf("%d",&x);

    if (x<=0|| x > length(L))
    {
        printf("error!\n");
    }
    if (x==1)
    {
        L->next = p->next;
        free(p);
    }

    if (x>1)
    {
        while (j<x-1) {
            p = p->next;
            j++;
        }
        q = p->next;
        p->next = q->next;
        

        free(q);
    }

}
附测试时主函数
int main()
{   
    //int a;
    LinkList L;
    printf("\n\n\nInsert number from head of this linklist\n");
/*    InsertHead(L);
    printf("\nthe length of this list is %d\n", length(L));
    PrintfList(L);

    printf("\n\n\n Fine by Pos Experience:\n");
    printf("please input the position of number that you want:\n");
    scanf("%d", &a);
    int NumbyPos = (FinebyPos(L, a))->data; 
    printf("%d", NumbyPos);

    printf("\n\n\nFine the number by value\n");
    printf("please input the number that you want to fine :\n");
    LNode* b;
    int m;
    scanf("%d",&m);
    b = FinebyValue(L, m);
    if (b)
    {
        printf("that number you want to fine is %d!\n", b->data);
    }
*/

    InsertTail(L);
    PrintfList(L);
    Insert(L);
    printf("after inserting a element:\n");
    PrintfList(L);
    printf("delete an element :\n");
    DeleteLNode(L);
    printf("after deleting :\n");
    PrintfList(L);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值