最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c語言實(shí)現(xiàn)單鏈表算法示例分享

 更新時(shí)間:2014年02月28日 09:50:30   作者:  
這篇文章主要介紹了c語言實(shí)現(xiàn)單鏈表算法示例,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

#include <stdio.h>
#include <stdlib.h>
typedef char DataType;
typedef struct Node{
    DataType data;
    struct Node * Next;
}ListNode,* LinkList;
void Judement(LinkList head){ //判斷分配內(nèi)存
    if (!head){
        printf("Overflow.");
        exit(-1);
    }
}
LinkList CreatListF(void){ //頭插法創(chuàng)建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){ //尾插法創(chuàng)建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節(jié)點(diǎn)
    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){ //依序號刪除節(jié)點(diǎn)
    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){ //清除冗余數(shù)據(jù)
    ListNode* p, *q, *s;
    if (p == NULL){
        exit(1);
    }
    p = head->Next; //首節(jié)點(diǎn)無數(shù)據(jù)
    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;
}

相關(guān)文章

最新評論

韩城市| 杭锦旗| 赤城县| 太仓市| 瓦房店市| 葵青区| 康乐县| 门头沟区| 龙门县| 三亚市| 家居| 措勤县| 临朐县| 墨竹工卡县| 柞水县| 新密市| 夏河县| 德惠市| 嘉定区| 涟源市| 阿城市| 灵山县| 合阳县| 酉阳| 平和县| 芒康县| 尖扎县| 突泉县| 海伦市| 九台市| 外汇| 五原县| 安国市| 隆德县| 冕宁县| 威远县| 贵定县| 老河口市| 博罗县| 玉门市| 永修县|