数据结构单链表的循环链表及基本操作实现(C语言实现)

el/2023/9/24 23:02:06

循环链表的基本操作的实现
特点:循环链表可以从任意一个位置循环遍历整个链表,如果设置的的为尾指针的话,其操作头节点后为节点的时间复杂度外O(1);

其实现代码如下

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>typedef int ElementType;typedef struct Node {ElementType element;struct Node *next;
} LNode, *CLinkList;//***********基本操作*****************
CLinkList Init_LinkList();bool Head_ListInsert(CLinkList L, ElementType e);CLinkList Tail_ListInsert(CLinkList L, CLinkList P, ElementType e);void PrtList(CLinkList L);int List_Length(CLinkList L);bool Delete_ListNode(CLinkList L, int locate);int GetLocate(CLinkList L, ElementType e);//**************#end*****************
int main() {CLinkList list = Init_LinkList();CLinkList point = list;for (int i = 0; i < 5; ++i) {point = Tail_ListInsert(list, point, i + 1);}printf("%d\n", List_Length(list));Delete_ListNode(list, 3);printf("%d\n", GetLocate(list, 3));PrtList(list);return 0;
}//***********基本操作实现**************
CLinkList Init_LinkList() {CLinkList NewNode = (CLinkList) malloc(sizeof(LNode));NewNode->next = NewNode;return NewNode;
}bool Head_ListInsert(CLinkList L, ElementType e) {CLinkList NewNode = (CLinkList) malloc(sizeof(LNode));if (NewNode == NULL) {printf("Memory is full!!");return false;}NewNode->element = e;NewNode->next = L->next;L->next = NewNode;return true;
}CLinkList Tail_ListInsert(CLinkList L, CLinkList P, ElementType e) {CLinkList NewNode = (CLinkList) malloc(sizeof(LNode));if (NewNode == NULL) {printf("Memory is full!!\n");return false;}NewNode->element = e;P->next = NewNode;P = NewNode;P->next = L;return P;
}int List_Length(CLinkList L) {int counter = 0;CLinkList P = L;while (P->next != L) {P = P->next;counter++;}return counter;
}void PrtList(CLinkList L) {CLinkList P = L;while (P->next != L) {P = P->next;printf("%d->", P->element);}
}int GetLocate(CLinkList L, ElementType e) {int index = 1;CLinkList P = L;while (P->next != L) {P = P->next;if (P->element == e) {return index;}index++;}return printf("Not find the element!!");
}bool Delete_ListNode(CLinkList L, int locate) {if (List_Length(L) < locate) {printf("Not find the locate in list!!\n");return false;}CLinkList P = L;CLinkList TempCell = NULL;while (locate > 1) {P = P->next;locate--;}TempCell = P->next;P->next = TempCell->next;free(TempCell);return true;
}

http://www.ngui.cc/el/3577061.html

相关文章

Pytorch中GPU训练好模型CPU下使用

GPU训练模型在CPU下使用 今天我想试试那个SkyAR,但是我没找到GPU的电脑我就想在CPU下使用GPU训练好的模型&#xff0c;使用的时遇到了下面这个问题 RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are ru…

C++按照空格分割字符串

C /A/B/1 1024 C /A/B/2 1024 C /A/B/1/3 1024 C /A 1024 R /A/B/1/3 Q / 0 1500 C /A/B/1 100 Q / 0 1500 R /A/B Q / 0 1 当我们需要处理上述数据时&#xff0c;其中有空格作为标志进行分割我们可以采取C中的一个处理string的标准库sstream。 操作方法如下&#xff1a; stri…

删除含有头结点的单链表中的最小值的结点

删除有头节点的单链表中的最小值的结点 #include <iostream>using namespace std;typedef struct node {int data;struct node *next; } LNode;void Del_minNode(LNode *L);LNode *CreateLink(const int data[], int n);void PrtLink(LNode *L);int main() {int n 6;int…

循环队列出队与入队

循环队列的入队与出队 #include <iostream>#define MaxSize 10using namespace std;typedef struct Node {int data[MaxSize];int front, rear; //队首&#xff0c;队尾指针 } SQueue;SQueue *init_queue();int En_queue(SQueue &qu, int x);int De_queue(SQueue &a…

邻接表创建有向图

使用邻接表创建有向图&#xff0c;可以使用数组链表的方式创建。 定义的顶点表如下 生成代码如下 char c; for (int i 0; i < graph->n; i) {cin >> c;graph->adj_list[i].data c;graph->adj_list[i].first_arc nullptr; }我们的图的整体结构如下图所示…

线索二叉树(先序)

#include <iostream>using namespace std;typedef struct ThBTNode {char data;ThBTNode *l_child;ThBTNode *r_child;int l_tag, r_tag;//l_tag1表示指向前驱&#xff0c;l_tag0表示指向左孩子//r_tag1表示指向后继&#xff0c;r_tag0表示指向右孩子 } ThBTNode;ThBTNod…

统计二叉树的叶子节点个数

#include <iostream> #include <queue>using namespace std;typedef struct node {char data;struct node *lchild;struct node *rchild; } TNode;TNode *CreateTree(TNode *&t);int Count(TNode *t); //统计t的叶子节点个数,非递归 int R_Count(TNode *t); //…

选择排序之选择排序

选择排序的思路很简单&#xff0c;就是左端为有序段&#xff0c;右端为待排序段&#xff0c;设置一个标志位&#xff0c;分割左右两端&#xff0c;外层循环控制标志位往后移&#xff0c;内层循环用于寻找无需段的最小值 #include <iostream>using namespace std;void Se…

创建一个简单的jsp跳转界面

创建环境&#xff1a;eclipsemaventomcatmysql 一&#xff1a;在eclipse下一次点击file-->new-->other-->maven-->maven project--> 如下图&#xff1a;勾选前两项 点击next,页面如下所示&#xff1a; Group id 和Artifact id 自己设定&#xff0c;Packaging将…

有bug的世界

不多说&#xff0c;点击---->知乎