#include<iostream>
using namespace std;
typedef int ElemType;
typedef struct ListNode {
ElemType val;
ListNode* next;
}ListNode,* LinkedList;
bool InitList(LinkedList& L); // 初始化
int length(LinkedList& L); // 计算长度
int LocateElem(LinkedList& L, ElemType e); // 按找值查询元素e在顺序表中的位置
ElemType GetElem(LinkedList& L, int i); // 查询顺序表中i位置对应的元素
bool ListInsert(LinkedList& L, int i, ElemType e); // 在顺序表L的i位置插入元素e
bool ListDelete(LinkedList& L, int i, ElemType& e);// 删除顺序表L中i位置的元素, 并且将删除的元素存储到引用e中
void PrintList(LinkedList& L); // 打印链表
bool Empty(LinkedList& L); // 判断是否为空
void DestroyList(LinkedList& L);// 销毁
bool HeadAdd(LinkedList& L, ElemType e); // 头插法
bool LastHead(LinkedList& L, ElemType e); // 尾插法
bool NewNode(LinkedList& node, ElemType e);// 创建节点
/**
* 不带头节点链表实现
* @param L
* @return
*/
bool InitList(LinkedList& L) {
L = nullptr;
return true;
}
/**
* 返回链表是否为空
* @param L
* @return
*/
bool Empty(LinkedList& L) {
return L == nullptr;
}
/**
* 计算链表长度
* @param L
* @return
*/
int length(LinkedList& L) {
int ans = 0;
LinkedList cur = L;
while (cur) {
cur = cur->next;
ans++;
}
return ans;
}
/**
* 遍历打印
* @param L
*/
void PrintList(LinkedList& L) {
LinkedList cur = L;
while (cur) {
cout << cur->val << " ";
cur = cur->next;
}
puts("");
}
/**
* 销毁链表
* @param L
*/
void DestroyList(LinkedList& L) {
LinkedList cur = L;
while (cur) {
LinkedList next = cur->next;
free(cur);
cur = next;
}
L = nullptr;
}
/**
* 查找链表中i下标的值,在顺序表中元素是从1开始
* @param L
* @param i
* @return
*/
ElemType GetElem(LinkedList& L, int i) {
if (Empty(L) || i <= 0) return -1;
LinkedList cur = L;
while (cur) {
if (i-- == 1) return cur->val;
cur = cur->next;
}
return -1;
}
/**
* 返回e在链表中的位置,在顺序表中元素是从1开始
* @param L
* @param e
* @return
*/
int LocateElem(LinkedList& L, ElemType e) {
LinkedList cur = L;
int idx = 1;
while (cur && cur->val != e) idx++, cur = cur->next;
return idx;
}
/**
* 在链表的i位置插入e,在顺序表中元素是从1开始
* @param L
* @param i
* @param e
* @return
*/
bool ListInsert(LinkedList& L, int i, ElemType e) {
// 1. 进行参数合法性校验
if (Empty(L) || i <= 0) return false;
// 2. 初始特殊插入---头插
if (i == 1) return HeadAdd(L, e);
// 3. 寻找i位置的前一个节点
LinkedList cur = L;
while (cur && i-- != 2) {
cur = cur->next;
}
// 4. 创建插入的节点
if (cur == nullptr) return false; // 非法下标
LinkedList node;
NewNode(node, e);
// 5. 插入操作
node->next = cur->next;
cur->next = node;
// 6. 返回
return true;
}
/**
* 删除i位置的元素,将其存储到e中
* @param L
* @param i
* @param e
* @return
*/
bool ListDelete(LinkedList& L, int i, ElemType& e) {
// 1. 进行参数合法性校验
if (Empty(L) || i <= 0) return false;
// 2. 初始特殊插入---头节点
if (i == 1) {
L = L->next;
return true;
}
// 3. 寻找i位置的前一个节点
LinkedList cur = L;
while (cur && i-- != 2) {
cur = cur->next;
}
// 4. delete
if (cur == nullptr) return false;
e = cur->next == nullptr ? -1 : cur->next->val;
cur->next = cur->next->next;
// 5. 返回
return true;
}
/**
* 头插法
* @param L
* @param e
* @return
*/
bool HeadAdd(LinkedList& L, ElemType e) {
LinkedList node;
if (!NewNode(node, e)) return false;
node->next = L;
L = node;
return true;
}
/**
* 创建节点
* @param node
* @param e
* @return
*/
bool NewNode(LinkedList& node, ElemType e) {
node = (LinkedList) malloc(sizeof(ListNode));
if (node == nullptr) return false;
node->val = e, node->next = nullptr;
return true;
}
/**
* 尾插法
* @param L
* @param e
* @return
*/
bool LastHead(LinkedList& L, ElemType e) {
if (L == nullptr) {
LinkedList node;
if (!NewNode(node, e)) return false;
L = node;
return true;
}
LinkedList cur = L;
while (cur->next) cur = cur->next;
LinkedList node;
if (!NewNode(node, e)) return false;
cur->next = node;
return true;
}
int main2() {
LinkedList head;
InitList(head);
HeadAdd(head, 1);
HeadAdd(head, 11);
HeadAdd(head, 111);
HeadAdd(head, 1111);
HeadAdd(head, 11111);
HeadAdd(head, 111111);
PrintList(head);
ListInsert(head, 2, 222);
PrintList(head);
int e = 0;
ListDelete(head, 2, e);
cout << "delete -- e:" << e << endl;
PrintList(head);
cout << "locateElem 111: " << LocateElem(head, 111) << endl;
cout << "length:" << length(head) << endl;
cout << "getElem 1:" << GetElem(head, 1) << endl;
LastHead(head, 100);
PrintList(head);
DestroyList(head);
PrintList(head);
return 0;
}
10-03
4万+
07-04
365
01-12
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交