单链表的简单应用

	单链表是一种链式存取的数据结构,用一组任意地址空间(地址空间即存储单元)来存放线性表的数据元素。单链表中的数据是以节点的形式来表示,而节点是用结构体来描述,每个节点都是由元素和指针构成,即该结构体中包含两个成员变量:存放元素的成员变量和存放下一个节点地址的成员变量。
	单链表的简单ADT如下所示:

#include <stdio.h>
#include <stdlib.h>

typedef char ElemType; //单链表数据类型为char

typedef struct Lnode { //单链表节点定义
ElemType data;
struct Lnode *next;
} LNode;

typedef enum Status { //函数运行结果返回值
ERROR,
SUCCESS
} Status;

Status BuildList(LNode** L) //头插入法建表
{
L = (LNode)malloc(sizeof(LNode));
(L)->next = NULL;
char c;
LNode
p;
printf(“请输入一串字符,回车键结束:”);
c = getchar();
while (c != ‘\n’) {
p = (LNode*)malloc(sizeof(LNode));
p->data = c;
p->next = (*L)->next;
(*L)->next = p;
c = getchar();
}
}

Status SearchList(LNode* L, int i) //按序号查找第i个元素
{
int j = 0;
while (L->next != NULL && j != i) { //L指向链表最后一个元素,或第i个元素时退出循环
j++;
L = L->next;
}
if (j != i) { //若退出循环后L未指向第i个元素
printf(“无第%d个元素\n”, i);
return ERROR;
}
printf(“找到第%d个元素:%c\n”, i, L->data); //若L指向第i个元素
return SUCCESS;
}

Status InsertList(LNode* L, int i, char e) //在第i个元素前插入新结点,该节点元素为e
{
int j = 0;
LNode* p;
while (L->next != NULL && j != i) { //L指向链表最后一个元素,或第i个元素时退出循环
j++;
p = L;
L = L->next;
}
if (j != i) { //若退出循环后L未指向第i个元素
printf(“无第%d个元素\n”, i);
return ERROR;
}
LNode* q = (LNode*)malloc(sizeof(LNode));
q->data = e;
q->next = p->next;
p->next = q;
}

Status DeletList(LNode* L, int i) //删除第i个结点
{
int j = 0;
LNode* p;
while (L->next != NULL && j != i) { //L指向链表最后一个元素,或第i个元素时退出循环
j++;
p = L;
L = L->next;
}
if (j != i) { //若退出循环后L未指向第i个元素
printf(“无第%d个元素\n”, i);
return ERROR;
}
p->next = L->next;
free(L);
}

void PrintList(LNode* L)
{
printf(“当前单链表内数据为:”);
L = L->next;
while (L) {
printf("%c ", L->data);
L = L->next;
}
putchar(’\n’);
}


作者:guangtaoxie
来源:CSDN
原文:https://blog.csdn.net/tongxuexie/article/details/79684125
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单单链表应用实例代码,使用C语言实现: ```c #include <stdio.h> #include <stdlib.h> // 定义链表结构体 typedef struct Node { int data; struct Node* next; } node; // 创建新节点 node* create_node(int data) { node* new = (node*)malloc(sizeof(node)); new->data = data; new->next = NULL; return new; } // 在链表末尾插入节点 void insert_node(node** head, int data) { node* new = create_node(data); if (*head == NULL) { *head = new; return; } node* current = *head; while (current->next != NULL) { current = current->next; } current->next = new; } // 删除指定值的节点 void delete_node(node** head, int data) { if (*head == NULL) { return; } if ((*head)->data == data) { node* temp = *head; *head = (*head)->next; free(temp); return; } node* current = *head; while (current->next != NULL && current->next->data != data) { current = current->next; } if (current->next != NULL) { node* temp = current->next; current->next = current->next->next; free(temp); } } // 打印链表 void print_list(node* head) { printf("Linked List: "); while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } int main() { node* head = NULL; insert_node(&head, 1); insert_node(&head, 2); insert_node(&head, 3); insert_node(&head, 4); insert_node(&head, 5); print_list(head); delete_node(&head, 3); print_list(head); delete_node(&head, 1); print_list(head); return 0; } ``` 该代码实现了一个单链表的基本操作,包括创建节点、在末尾插入节点、删除指定值的节点和打印链表。可以通过在 `main` 函数中调用这些函数来使用这个单链表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值