服务器之家

服务器之家 > 正文

c语言实现单链表算法示例分享

时间:2021-01-16 13:13     来源/作者:C语言程序设计

复制代码 代码如下:


#include <stdio.h>
#include <stdlib.h>
typedef char DataType;
typedef struct Node{
    DataType data;
    struct Node * Next;
}ListNode,* LinkList;
void Judement(LinkList head){ //判断分配内存
    if (!head){
        printf("Overflow.");
        exit(-1);
    }
}
LinkList CreatListF(void){ //头插法创建Single Linked List
    DataType ch;
    LinkList head = (ListNode*)malloc(sizeof(ListNode));
    Judement(head);
    ListNode* s;
    ch = getchar();
    while (ch != '\n'){
        s = (ListNode*)malloc(sizeof(ListNode));
        Judement(s);
        s->data = ch;
        s->Next = head->Next;
        head->Next = s;
        ch = getchar();
    }
    return head;
}
LinkList CreatListS(void){ //尾插法创建Single Linked List
    char ch;
    ListNode* s;
    LinkList head = (ListNode*)malloc(sizeof(ListNode));
    Judement(head);
    ch = getchar();
    while (ch != '\n'){
        s = (ListNode*)malloc(sizeof(ListNode));
        Judement(s);
        s->data = ch;
        head->Next = s;
        head = s;
        ch = getchar();
    }
    head->Next = NULL;
    return head;
}
int GetLength(LinkList head){  //获取长度
    int length = 0;
    LinkList p = head->Next;
    while (p){
        length += 1;
        p = p->Next;
    }
    return length;
}
ListNode* GetNodeById(LinkList head, int i){ //依序号查找元素
    if (i<1 || i>GetLength(head)){
        exit(1);
    }
    int j=1; //防止极端情况扫描逾界
    LinkList p = head->Next;
    while (p != NULL && j < i){
        j += 1;
        p = p->Next;
    }
    return p;
}
ListNode* GetNodeByValue(LinkList head, DataType e){ //依值查找元素
    LinkList p = head->Next;
    while (p != NULL&&p->data != e){
        p = p->Next;
    }
    return p;
}
int InsertList(LinkList head, DataType e, int i){ //插入e值在第i节点
    if (i<1 || i>GetLength(head) + 1){
        exit(1);
    }
    LinkList s = (ListNode*)malloc(sizeof(ListNode));
    s->data = e;
    LinkList q, p = head;
    int j = 1;
    while (j <= i){
        q = p;
        p = p->Next;
        j += 1;
    }
    s->Next = q->Next;
    q->Next = s;
    return 0;
}
int DeleteListNodeById(LinkList head, int i){ //依序号删除节点
    int j = 1;
    ListNode* p,* q;
    if (i<1 || i>GetLength(head)){
        exit(1);
    }
    p = head;
    while (j < i){
        p = p->Next;
        j += 1;
    }
    q = p->Next;
    p->Next = q->Next;
    free(q);
    return 0;
}
int DeleteListRepeatNode(ListNode* head){ //清除冗余数据
    ListNode* p, *q, *s;
    if (p == NULL){
        exit(1);
    }
    p = head->Next; //首节点无数据
    while (p->Next != NULL){
        q = p;
        while (q->Next != NULL){
            if (q->Next->data == p->data){
                s = q->Next;
                q->Next = q->Next->Next;
                free(s);
            }
            q = q->Next;
        }
        p = p->Next;
    }
    return 0;
}

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部