欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

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

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

復(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é)點
    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é)點
    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é)點無數(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;
}

上一篇:深入理解c++中virtual關(guān)鍵字

欄    目:C語言

下一篇:c++異常處理機制示例及詳細(xì)講解

本文標(biāo)題:c語言實現(xiàn)單鏈表算法示例分享

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3768.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有