1.2-线性表单链表存储
#include<stdio.h>
#include<Windows.h>
typedef int ElemType;
typedef struct LNode {
ElemType data;
LNode* next;
}LNode, * ListPoint;
//ListPoint InitList();初始化单链表
//ListPoint List_HeadInsert(ListPoint &L);头插法建立单链表
//ListPoint List_TailInsert(ListPoint &L);尾插法建立单链表
//LNode* GetElem(ListPoint L, int i);按序号查找结点值
//LNode* LocateElem(ListPoint L, ElemType e); 按值查找表结点
//ListPoint List_Insert(ListPoint L,, int i, ElemType e) 指定位置插入
//ListPoint DeleteElem(ListPoint L,);删除结点
//int Length(ListPoint L);求表长
//void PrintList(ListPoint L);打印
//ListPoint InitList();初始化单链表,创建代空表头的链表
ListPoint InitList() {
ListPoint L = (ListPoint)malloc(sizeof(LNode));
L->next = NULL;
return L;
}
//void List_HeadInsert(ListPoint L, ElemType e);头插法建立单链表
void List_HeaderInsert(ListPoint L, ElemType e) {
LNode* newNode = (LNode*)malloc(sizeof(LNode));
newNode->data = e;
newNode->next = L->next;
L->next = newNode;
}
//void List_TailInsert(ListPoint L, ElemType e);尾插法建立单链表
void List_TailInsert(ListPoint L, ElemType e) {
LNode* newNode = (LNode*)malloc(sizeof(LNode));
LNode* tailNode = L;
while (tailNode->next) {
tailNode = tailNode->next;
}
newNode->data = e;
newNode->next = NULL;
tailNode->next = newNode;
tailNode = newNode;
}
//LNode* GetElem(ListPoint L, int i);按序号查找结点值
LNode* GetElem(ListPoint L, int i) {
if (i < 0)
return NULL;
else if (i == 0)
return L;
L = L->next;
int index = 1;
while (L && index < i) {
L = L->next;
index++;
}
return L;
}
//LNode* LocateElem(ListPoint L, ElemType e); 按值查找表结点
LNode* LocateElem(ListPoint L, ElemType e) {
L = L->next;
while (L) {
if (L->data == e) {
return L;
}
L = L->next;
}
return NULL;
}
//ListPoint List_Insert(ListPoint L,, int i, ElemType e) 指定位置插入
ListPoint List_Insert(ListPoint L, int i, ElemType e) {
LNode* forthNode;
if ((forthNode = GetElem(L, i-1))) {
LNode* newNode = (LNode*)malloc(sizeof(LNode));
newNode->data = e;
newNode->next = forthNode->next;
forthNode->next = newNode;
return newNode;
}
return NULL;
}
//ListPoint DeleteElem(ListPoint L, int i);删除结点
ElemType DeleteElem(ListPoint L, int i) {
LNode* forthNode;
if ((forthNode = GetElem(L, i - 1)) && forthNode->next) {
LNode* delNode = forthNode->next;
forthNode->next = delNode->next;
ElemType i = delNode->data;
free(delNode);
return i;
}
return NULL;
}
//int Length(ListPoint L);求表长
int Length(ListPoint L) {
int count = 0;
L = L->next;
while (L) {
count++;
L = L->next;
}
return count;
}
//void DestoryList(ListPoint L); 删除整个链表
void DestoryList(ListPoint L) {
ListPoint tmp;
while (L) {
tmp = L->next;
free(L);
L = tmp;
}
}
//void PrintList(ListPoint L);打印
void PrintList(ListPoint L) {
L = L->next;
while (L){
printf("%3d", L->data);
L = L->next;
}
putchar('\n');
}
int main(int argc, char* argv) {
ListPoint L = InitList();
PrintList(L);
printf("链表总长度:%d\n", Length(L));
for (int i = 0; i < 5; i++) {
List_HeaderInsert(L, i);
}
for (int i = 10; i < 17; i++) {
List_TailInsert(L, i);
}
PrintList(L);
printf("链表总长度:%d\n", Length(L));
LNode* node1 = GetElem(L,12);
if (node1) {
printf("第10个元素是%i\n", node1->data);
}
LNode* node2= LocateElem(L, 12);
if (node2) {
printf("找到了\n");
}
else {
printf("没找到\n");
}
List_Insert(L, 0, 0);
List_Insert(L, 1, 1);
List_Insert(L, 2, 2);
List_Insert(L, 100, 1);
PrintList(L);
List_Insert(L, 15, 19);
PrintList(L);
DeleteElem(L, -1);
PrintList(L);
DeleteElem(L, 0);
PrintList(L);
DeleteElem(L, 1);
PrintList(L);
DeleteElem(L, 10);
PrintList(L);
DeleteElem(L, 13);
PrintList(L);
DeleteElem(L, 100);
PrintList(L);
DestoryList(L);
system("pause");
return 0;
}
本文来自博客园,作者:ant-king,转载请注明原文链接:https://www.cnblogs.com/ant-king/p/16092348.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」