#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)系作者
- 文/潘曉璐 我一進(jìn)店門欣鳖,熙熙樓的掌柜王于貴愁眉苦臉地迎上來察皇,“玉大人茴厉,你說我怎么就攤上這事泽台。” “怎么了矾缓?”我有些...
- 文/不壞的土叔 我叫張陵怀酷,是天一觀的道長。 經(jīng)常有香客問我嗜闻,道長蜕依,這世上最難降的妖魔是什么? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮样眠,結(jié)果婚禮上友瘤,老公的妹妹穿的比我還像新娘。我一直安慰自己檐束,他們只是感情好辫秧,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著被丧,像睡著了一般盟戏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上甥桂,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼懂诗!你這毒婦竟也來了蜂嗽?” 一聲冷哼從身側(cè)響起,我...
- 序言:老撾萬榮一對情侶失蹤殃恒,失蹤者是張志新(化名)和其女友劉穎植旧,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體离唐,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡病附,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了亥鬓。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片完沪。...
- 正文 年R本政府宣布宽档,位于F島的核電站,受9級特大地震影響庵朝,放射性物質(zhì)發(fā)生泄漏吗冤。R本人自食惡果不足惜又厉,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望椎瘟。 院中可真熱鬧覆致,春花似錦、人聲如沸肺蔚。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽婆排。三九已至声旺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間段只,已是汗流浹背腮猖。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長得像炕婶,于是被迫代替她去往敵國和親姐赡。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 基礎(chǔ):結(jié)構(gòu)體(struct)指針 定義:多個結(jié)點(diǎn)鏈接在一起的存儲結(jié)構(gòu)枪狂,其中每一結(jié)點(diǎn)都是同類結(jié)構(gòu)體,包含兩部分:數(shù)值...
- 元素域 data 用來存放具體的數(shù)據(jù)严蓖。 鏈接域 prev 用來存放上一個節(jié)點(diǎn)的位置。 鏈接域 next 用來存放下...
- //將一個帶頭結(jié)點(diǎn)的單鏈表A分解為兩個帶頭結(jié)點(diǎn)單鏈表A和B氧急,使得A表中含有原表中序號為奇數(shù)元素颗胡,而B表中含有原表中...
- C語言知識點(diǎn)提要: Struct:Struct 內(nèi)可放置各種類型的數(shù)據(jù) 格式為: Struct TagName {...