在上一篇單鏈表的基礎上進行改進,主要改進的地方多爲循環控制點、創建初始化
單鏈表:https://blog.csdn.net/hpu2022/article/details/104582008
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ElemType int
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
// 帶頭節點的初始化
bool InitList(LinkList &headList)
{
headList = (LNode*) malloc(sizeof(LNode));
if(headList == NULL){
return false;
}
headList->next = headList; // 初始化時頭結點的下一個結點指向自己
return true;
}
// 按位查找
LNode* GetElem(LinkList headList, int i)
{
if(i < 0){
return NULL;
}
LNode *p = headList;
int j = 0;
while(j < i){
p = p->next;
j++;
if(p == headList){ // p再次等於headList表示循環一週了
return NULL;
}
}
return p;
}
// 按值查找
LNode* LocateElem(LinkList headList, ElemType e)
{
LNode *p = headList->next;
while(p != headList && p->data != e){
p = p->next;
}
return p;
}
// 在指定結點p後插入元素e
bool InsertNextNode(LNode *p, ElemType e)
{
if(p == NULL){
return false;
}
LNode *node = (LNode *)malloc(sizeof(LNode));
if(node == NULL){ // 內存分配失敗
return false;
}
node->data = e;
node->next = p->next; // 連接node的後繼結點
p->next = node; // 連接node的前驅結點
// printf("%d\n", p->next->data);
return true;
}
// 在第idx的位置上插入e
bool ListInsert(LinkList &headList, int idx, ElemType e)
{
if(idx < 1){
return false;
}
LNode *p = GetElem(headList, idx-1);
return InsertNextNode(p, e); // 調用在指定結點後插入元素的函數
}
// 在指定結點p之前插入元素e O(n)方法
bool InsertPriorNode(LinkList &headList, LNode *p, ElemType e)
{
LNode *tmp = headList;
while(tmp != NULL){ // 循環找到p結點的前一個結點
if(tmp->next == p){
break;
}
tmp = tmp->next;
}
if(tmp == NULL){
return false;
}
LNode *node = (LNode *)malloc(sizeof(LNode));
if(node == NULL){
return false;
}
node->data = e;
node->next = p;
tmp->next = node;
return true;
}
// 在指定結點p之前插入元素e O(1)方法
bool InsertPriorNode(LNode *p, ElemType e)
{
if(p == NULL){
return false;
}
LNode *node = (LNode *)malloc(sizeof(LNode));
if(node == NULL){
return false;
}
// 移動數據 實現插入結點
node->data = p->data; // 新申請的結點替換掉當前的p結點
p->data = e; // 然後把p結點的數據更換,這樣p就成了先前p的前驅結點
node->next = p->next; // 連接
p->next = node;
return true;
}
// 刪除操作 刪除第idx個位置的元素
bool ListDelete(LinkList &headList, int idx)
{
if(idx < 0){
return false;
}
LNode *p = GetElem(headList, idx-1); // 找到第idx-1個結點
if(p == NULL || p->next == NULL){ // 如果idx無效
return false;
}
LNode *tmp = p->next; // tmp爲當前要刪除的結點
p->next = tmp->next;
free(tmp);
return true;
}
// 刪除指定結點 O(n)方法
bool DeleteNode(LinkList &headList, LNode *p)
{
if(p == NULL){
return false;
}
LNode *tmp = headList;
while(tmp->next != p){ // 循環找到被刪除結點的前驅結點
tmp = tmp->next;
if(tmp == headList){
return false;
}
}
tmp->next = p->next;
free(p);
return true;
}
// 刪除指定結點 O(1)方法
// 缺陷是無法刪除最後一個結點
// 帶頭節點的判空方法
bool IsEmpty(LinkList &headList)
{
return (headList->next == NULL);
}
// 求表長
int Length(LinkList headList)
{
LNode *p = headList;
int len = 0;
while(p->next != headList){
p = p->next;
len++;
}
return len;
}
// 尾插法建立單鏈表
LinkList ListTailInsert(LinkList &headList)
{
headList = (LinkList)malloc(sizeof(LNode)); // 建立頭結點
headList->next = headList;
LNode *tail = headList;
ElemType e;
while(scanf("%d", &e) != EOF){
LNode *newnode = (LNode *)malloc(sizeof(LNode));
newnode->data = e;
newnode->next = headList;
tail->next = newnode;
tail = newnode;
}
tail->next = headList;
return headList;
}
// 頭插法建立單鏈表
LinkList ListHeadInsert(LinkList &headList)
{
headList = (LinkList)malloc(sizeof(LNode)); // 建立頭結點
headList->next = headList;
LNode *head = headList;
ElemType e;
while(scanf("%d", &e) != EOF){
LNode *newnode = (LNode *)malloc(sizeof(LNode));
newnode->data = e;
newnode->next = head->next; // newnode變成頭結點後的第一個結點
head->next = newnode; // 更新第一個結點
}
headList = head;
return headList;
}
// 銷燬單鏈表
bool ListDestroy(LinkList headList)
{
if(headList == NULL){
return false;
}
LNode *p = headList->next;
if(p != headList){
headList->next = p->next; // 刪除p結點
free(p); // 釋放p結點空間
p = headList->next; // p指向新的結點
}
free(headList); // 釋放頭結點
return true;
}
// 單鏈表反轉
LinkList ListReverse(LinkList &headList)
{
LinkList newList = (LinkList)malloc(sizeof(LNode)); // 新建一個鏈表的頭結點
newList->next = newList; // 新鏈表先形成一個環
LNode *p = headList->next;
while(p != headList){
LNode *newnode = (LNode *)malloc(sizeof(LNode)); // 新建一個結點準備複製
newnode->data = p->data; // 複製數據
newnode->next = p->next; // 複製下一個指針
newnode->next = newList->next; // 將新節點插入到新鏈表的第一個結點前
newList->next = newnode; // 更新頭結點指向的第一個結點
p = p->next; // p後移 準備下次上述操作
}
ListDestroy(headList); // 銷燬之前的鏈表 防止內存溢出
headList = newList;
return headList;
}
int main()
{
LinkList headList;
bool flag = false;
flag = InitList(headList);
printf("%d\n", flag);
// ListTailInsert(headList);
// ListHeadInsert(headList);
for(int i = 0; i < 10; i++){
ListInsert(headList, i+1, i+1);
}
// 調用鏈表操作函數
ListInsert(headList, 6, 5);
InsertPriorNode(headList, headList->next->next, 10);
InsertPriorNode(headList->next->next, 11);
ListDelete(headList, 8);
DeleteNode(headList, headList->next->next);
DeleteNode(headList, headList->next->next);
ListReverse(headList);
printf("Here\n");
int i = 1;
for(LNode *p = headList->next; p != headList; p = p->next){
printf("第%d個結點的值是:%d\n", i++, p->data);
}
printf("表長是%d\n", Length(headList));
return 0;
}