單鏈表


#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *next;
} LNode, *LinkList;

// 帶頭結(jié)點(diǎn)
LinkList InitList() {
    LNode *h = (LNode *)malloc(sizeof(LNode));
    if (h == NULL) return NULL;
    h->next = NULL;
    return h;
}

// 帶頭結(jié)點(diǎn)
int ListInsert(LinkList l, int i, int e) {
    if (i < 1) return -1;
    LNode *p = l;
    int j = 0;
    while (p != NULL && j < i-1) {
        p = p->next;
        j++;
    }
    if (p == NULL) return -1;
    LNode *s = (LNode *)malloc(sizeof(LNode));
    if (s == NULL) return -1;
    s->next = p->next;
    p->next = s;
    s->data = e;
    return 1;
}

// 帶頭結(jié)點(diǎn)
int ListDelete(LinkList l, int i, int *e) {
    if (i < 1) return -1;
    LNode *p = l;
    int j = 0;
    while (p != NULL && j < i-1) {
        p = p->next;
        j++;
    }
    if (p == NULL) return -1;
    LNode *q = p->next;
    if (q == NULL) return -1;
    p->next = q->next;
    *e = q->data;
    free(q);
    return 1;
}

// 不帶頭結(jié)點(diǎn)
LinkList InitList1() {
    return NULL;
}

// 不帶頭結(jié)點(diǎn)
int ListInsert1(LinkList *l, int i, int e) {
    if (i < 1) return -1;
    if (i == 1) {
        LNode *s = (LNode *)malloc(sizeof(LNode));
        if (s == NULL) return -1;
        s->next = *l;
        s->data = e;
        *l = s;
        return 1;
    }
    LNode *p = *l;
    int j = 1;
    while (p != NULL && j < i-1) {
        p = p->next;
        j++;
    }
    if (p == NULL) return -1;
    LNode *s = (LNode *)malloc(sizeof(LNode));
    if (s == NULL) return -1;
    s->next = p->next;
    p->next = s;
    s->data = e;
    return 1;
}

// 不帶頭結(jié)點(diǎn)
int ListDelete1(LinkList *l, int i, int *e) {
    if (i < 1) return -1;
    if (i == 1) {
        LNode *q = *l;
        LNode *s = q->next;
        *e = q->data;
        free(q);
        *l = s;
        return 1;
    }
    LNode *p = *l;
    int j = 1;
    while (p != NULL && j < i-1) {
        p = p->next;
        j++;
    }
    if (p == NULL) return -1;
    LNode *q = p->next;
    if (q == NULL) return -1;
    p->next = q->next;
    *e = q->data;
    free(q);
    return 1;
}

// 通用接口
LNode * GetElem(LinkList l, int i) {
    if (i < 0) return NULL;
    LNode *p = l;
    int j = 0;
    while (p != NULL && j < i) {
        p = p->next;
        j++;
    }
    return p;
}

// 通用接口
LNode * LocateElem(LinkList l, int e) {
    LNode *p = l;
    while (p != NULL && p->data != e) {
        p = p->next;
    }
    return p;
}

// 通用接口
int Length(LinkList l) {
    int len = 1;
    LNode *p = l;
    while (p->next != NULL) {
        p = p->next;
        len++;
    }
    return len;
}

// 通用接口
void ListPrintf(LinkList l) {
    LNode *p = l;
    while (p != NULL) {
        printf("%i\n", p->data);
        p = p->next;
    }
}

// 通用接口
int Empty(LinkList l) {
    return l == NULL ? 1 : -1;
}

// 通用接口
void DestroyList(LinkList *l) {
    LNode *q = *l;
    LNode *next = NULL;
    while (q != NULL) {
        next = q->next;
        free(q);
        q = next;
    }
    *l = NULL;
}

// 通用接口: 前插操作
int InsertPriorLNode(LNode *n, int e) {
    if (n == NULL) return -1;
    LNode *s = (LNode *)malloc(sizeof(LNode));
    if (s == NULL) return -1;
    s->next = n->next;
    n->next = s;
    s->data = n->data;
    n->data = e;
    return 1;
}

// 通用接口: 后插操作
int InsertNextLNode(LNode *n, int e) {
    if (n == NULL) return -1;
    LNode *s = (LNode *)malloc(sizeof(LNode));
    if (s == NULL) return -1;
    s->next = n->next;
    n->next = s;
    s->data = e;
    return 1;
}

// 通用接口: 刪除操作
int DeleteLNode(LNode *n, int *e) {
    if (n == NULL) return -1;
    LNode *q = n->next;
    if (q == NULL) return -1;
    n->next = q->next;
    *e = n->data;
    n->data = q->data;
    free(q);
    return 1;
}

// 帶頭結(jié)點(diǎn)的單鏈表頭插
int ListHeadInsert(LinkList l) {
    int x;
    scanf("%i", &x);
    LNode *s = NULL;
    while (x != 999) {
        s = (LNode *)malloc(sizeof(LNode));
        if (s == NULL) return -1;
        s->next = l->next;
        l->next = s;
        s->data = x;
        scanf("%i", &x);
    }
    return 1;
}

// 帶頭結(jié)點(diǎn)的單鏈表尾插
int ListTailInsert(LinkList l) {
    int x;
    LNode *s, *r = l;
    scanf("%i", &x);
    while (x != 999) {
        s = (LNode *)malloc(sizeof(LNode));
        if (s == NULL) return -1;
        r->next = s;
        s->data = x;
        r = s;
        scanf("%i", &x);
    }
    r->next = NULL;
    return 1;
}

// 不帶頭結(jié)點(diǎn)的單鏈表頭插
int ListHeadInsert1(LinkList *l) {
    int x;
    scanf("%i", &x);
    while (x != 999) {
        LNode *s = (LNode *)malloc(sizeof(LNode));
        if (s == NULL) return -1;
        s->data = x;
        s->next = *l;
        *l = s;
        scanf("%i", &x);
    }
    return 1;
}

// 不帶頭結(jié)點(diǎn)的單鏈表尾插
int ListTailInsert1(LinkList *l) {
    int x;
    scanf("%i", &x);
    LNode *r = *l;
    while (x != 999) {
        if (*l == NULL) {
            LNode *s = (LNode *)malloc(sizeof(LNode));
            if (s == NULL) return -1;
            s->data = x;
            r = s;
            *l = s;
        } else {
            LNode *s = (LNode *)malloc(sizeof(LNode));
            if (s == NULL) return -1;
            r->next = s;
            s->data = x;
            r = s;
        }
        scanf("%i", &x);
    }
    r->next = NULL;
    return 1;
}

// 不帶頭結(jié)點(diǎn)的單鏈表逆置
void ListInverse(LinkList *l) {
    LNode *prev = NULL;
    LNode *curr = *l;
    while (curr != NULL) {
        LNode *next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    *l = prev;
}

// 分割線
void Sep() {
    printf("**************\n");
}

int main() {
    LinkList l = InitList1();
    ListTailInsert1(&l);
    ListPrintf(l);
}

void Head() {
    LinkList l = InitList();
    ListInsert(l, 1, 1);
    ListInsert(l, 2, 2);
    ListInsert(l, 3, 6);
    ListInsert(l, 3, 5);
    ListInsert(l, 4, 100);
    ListPrintf(l->next);
    Sep();
    int e = -1;
    ListDelete(l, 3, &e);
    printf("%i\n", e);
    Sep();
    printf("%i\n", Length(l->next));
    Sep();
    printf("%i\n", LocateElem(l->next, 1)->data);
    Sep();
    ListPrintf(l->next);
    Sep();
    printf("%i\n", GetElem(l->next, 4-1)->data);
}

void NoHead() {
    LinkList l = InitList1();
    ListInsert1(&l, 1, 99);
    ListInsert1(&l, 2, 123);
    ListInsert1(&l, 1, 18);
    ListPrintf(l);
    int e = -1;
    ListDelete1(&l, 3, &e);
    Sep();
    printf("%i\n", e);
    Sep();
    ListPrintf(l);
    Sep();
    DestroyList(&l);
    printf("%i\n", Empty(l));
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末项戴,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子夕晓,更是在濱河造成了極大的恐慌揖盘,老刑警劉巖组哩,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異破讨,居然都是意外死亡碑定,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進(jìn)店門欣鳖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來察皇,“玉大人茴厉,你說我怎么就攤上這事泽台。” “怎么了矾缓?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵怀酷,是天一觀的道長。 經(jīng)常有香客問我嗜闻,道長蜕依,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮样眠,結(jié)果婚禮上友瘤,老公的妹妹穿的比我還像新娘。我一直安慰自己檐束,他們只是感情好辫秧,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著被丧,像睡著了一般盟戏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上甥桂,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天柿究,我揣著相機(jī)與錄音,去河邊找鬼黄选。 笑死蝇摸,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的办陷。 我是一名探鬼主播探入,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼懂诗!你這毒婦竟也來了蜂嗽?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤殃恒,失蹤者是張志新(化名)和其女友劉穎植旧,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體离唐,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡病附,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了亥鬓。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片完沪。...
    茶點(diǎn)故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖嵌戈,靈堂內(nèi)的尸體忽然破棺而出覆积,到底是詐尸還是另有隱情,我是刑警寧澤熟呛,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布宽档,位于F島的核電站,受9級特大地震影響庵朝,放射性物質(zhì)發(fā)生泄漏吗冤。R本人自食惡果不足惜又厉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望椎瘟。 院中可真熱鬧覆致,春花似錦、人聲如沸肺蔚。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽婆排。三九已至声旺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間段只,已是汗流浹背腮猖。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留赞枕,地道東北人澈缺。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像炕婶,于是被迫代替她去往敵國和親姐赡。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容