单链表
下面内容来自王道论坛数据结构书籍(本人自用)
#include<iostream>
#include<algorithm>
using namespace std;
typedef struct LNode {
int data;
struct LNode* next;
}LNode,*LinkList;
bool InitList(LinkList& L) {
L = NULL;
return true;
}
bool InitListHead(LinkList& L) {
L = (LNode*)malloc(sizeof(LNode));
if (!L) return false;
L->next = NULL;
return true;
}
LNode* GetElem(LinkList L, int i) {
if (i <1)return NULL;
int j = 1;
LNode* p = L;
while (p && j < i) {
p = p->next;
j++;
}
return p;
}
LNode* GetElemHead(LinkList L, int i) {
if (i < 1) return NULL;
int j = 1;
LNode* p = L->next;
while (p && j < i) {
p = p->next;
j++;
}
return p;
}
LNode* LocateElem(LinkList L, int e) {
LNode* p = L->next;
while (p && p->data != e) {
p = p->next;
}
return p;
}
int Length(LinkList L) {
int len = 0;
LNode* p = L;
while (p->next) {
p = p->next;
len++;
}
return len;
}
bool InsertNextNode(LNode* p, int e) {
if (!p) return false;
LNode* s = (LNode*)malloc(sizeof(LNode));
if (!s)return false;
s->data = e;
s->next = p->next;
p->next = s;
return true;
}
bool ListInsert(LinkList& L, int i, int e) {
if (i < 1)return false;
if (i == 1) {
LNode* s = (LNode*)malloc(sizeof(LNode));
s->data = e;
s->next = L;
L = s;
return true;
}
LNode* p = L;
int j = 1;
while (p && j < i) {
j++;
p = p->next;
}
if (!p)return false;
return InsertNextNode(p,e);
}
bool InsertPriorNodeElem(LNode* p, int e) {
if (!p)return false;
LNode* s = (LNode*)malloc(sizeof(LNode));
if (!s)return false;
s->next = p->next;
p->next = s;
s->data = p->data;
p->data = e;
return true;
}
bool InsertPriorNode(LNode* p, LNode* s) {
if (!s || !p)return false;
s->next = p->next;
p->next = s;
swap(p->data, s->data);
return true;
}
bool ListDelete(LinkList& L, int i, int &e) {
if (i < 1) return false;
LNode* p = GetElemHead(L, i-1);
if (!p || !(p->next)) return false;
LNode* q = p->next;
e = q->data;
p->next = q->next;
free(q);
return true;
}
bool DeleteNode(LNode* p) {
if (p->next == NULL)return false;
LNode* q = p->next;
p->next = q->next;
p->data = q->data;
free(q);
return true;
}
LinkList List_TailInsert(LinkList& L) {
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
LNode * s, * r = L;
int x;
while (cin >> x&&x!=-1) {
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
}
r->next = NULL;
return L;
}
LinkList List_HeadInsert(LinkList& L) {
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
LNode* s;
int x;
while (cin >> x&&x!=-1) {
s = (LNode*)malloc(sizeof(LNode));
s->data = x;
s->next = L->next;
L->next = s;
}
return L;
}
void Printf(LinkList L) {
LNode* s = L;
while (s->next != NULL) {
s = s->next;
cout << s->data << " ";
}
cout << endl;
}
int main() {
LinkList L;
List_TailInsert(L);
cout << "尾插法的结果" << endl;
Printf(L);
cout << "链表的第2个元素:" << GetElemHead(L, 2)->data << endl;
cout << "链表的长度:" << Length(L) << endl;
int e;
ListDelete(L, 3, e);
cout << "删除的第3个元素是:" << e << endl;
cout << "当前的链表" << endl;
Printf(L);
ListInsert(L, 3, e);
cout << "插入的第3个元素是:" << e << endl;
cout << "当前的链表" << endl;
Printf(L);
LNode* s = LocateElem(L, 5);
Printf(L);
}