在这篇博客中,我们将探讨如何使用C语言实现一个简单的单链表。单链表是一种基本的数据结构,广泛应用于各种编程场景中。我们将逐步分析代码中的每个部分,帮助你更好地理解单链表的实现。
代码结构
这段代码实现了一个简单的图书信息管理系统,使用单链表来存储图书信息。每个节点包含图书的ISBN、书名和价格。
1. 节点结构体定义
首先,我们定义了一个节点结构体 Node,它包含图书的基本信息和指向下一个节点的指针。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define MAXSIZE 100;
typedef int ElemType;
// 定义图书信息结构体
typedef struct node{
char isbn[20];
char bookname[20];
double price;
struct node *next;
}Node;
2. 初始化链表
initList 函数用于初始化链表,创建一个头节点并返回其指针。
// 初始化节点
Node* initList(){
Node* head = (Node*)malloc(sizeof(Node));
if (head == NULL){
printf("链表初始化失败!");
return NULL;
}
head->next = NULL;
return head;
}
3. 创建节点
CreateNode 函数用于创建一个新的节点,并初始化其数据。
// 创建节点
Node* CreateNode(char isbn[], char bookname[], double price) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("节点创建失败!");
return NULL;
}
strcpy(newNode->isbn, isbn);
strcpy(newNode->bookname, bookname);
newNode->price = price;
newNode->next = NULL;
return newNode;
}
4. 插入节点
insertElem 函数用于在指定位置插入一个新节点。
// 插入节点
bool insertElem(Node* L, Node* newNode, int pos){
Node* p = L;
if (pos < 0){
return false;
}
while(pos!=0){
if (p->next == NULL){
return false;
}
p = p->next;
pos --;
}
newNode->next = p->next;
p->next = newNode;
return true;
}
5. 删除节点
deleteElem 函数用于删除指定位置的节点。
// 删除节点
bool deleteElem(Node* L, int pos){
Node *p = L->next;
Node *prev = L;
if (pos <= 0){
return false;
}
while (pos!=1){
if (p->next == NULL){
return false;
}
p = p->next;
prev = prev->next;
pos --;
}
Node *q = p;
prev->next = p->next;
free(q);
return true;
}
6. 更新节点
updateNode 函数用于更新指定位置的节点信息。
// 修改节点
bool updateNode(Node* L, Node* newNode, int pos){
Node *p = L->next;
Node *Prev = L;
if (pos <= 0){
return false;
}
while (pos!=1){
if (p->next == NULL){
return false;
}
Prev = Prev->next;
p = p->next;
pos --;
}
newNode->next = p->next;
Prev->next = newNode;
return true;
}
7. 查询和展示节点
queryNode 和 showNode 函数用于查询和展示节点信息。
// 展示节点
void showNode(Node* L){
printf("----------------------\n");
printf("isbn:%s\n",L->isbn);
printf("bookName:%s\n",L->bookname);
printf("price:%lf\n",L->price);
printf("----------------------\n");
}
/ 查询节点
void queryNode(Node* L, int pos){
Node *p = L->next;
if (pos <= 0){
printf("查询失败!");
return;
}
while (pos!=1){
if (p->next == NULL){
printf("查询失败!");
return;
}
p = p->next;
pos --;
}
printf("第%d号节点的信息为:\n");
showNode(p);
}
8.展示链表
void showList(Node* L){
Node* p = L->next;
printf("------------------------------\n");
printf("链表信息为:\n");
while(p != NULL){
showNode(p);
p = p->next;
}
printf("------------------------------\n");
}
9. 主函数
在 main 函数中,我们提供了一个简单的菜单,用户可以通过输入选择不同的操作,如插入、删除、更新、查询和展示链表。
int main(){
Node * L = initList(); // 初始化
bool flag = true;
int choice = 0;
char isbn[20];
char bookname[20];
double price;
int pos;
while(flag){
printf("\n------------------------------\n");
printf("******************************\n");
printf("**** 0:退出 ****\n");
printf("**** 1:插入新节点 ****\n");
printf("**** 2:删除节点 ****\n");
printf("**** 3:修改节点 ****\n");
printf("**** 4:查询节点 ****\n");
printf("**** 5:展示单链表 ****\n");
printf("******************************\n");
printf("------------------------------\n");
printf("请输入你要进行的操作:");
scanf("%d", &choice);
switch(choice){
case 0:
flag = false;
break;
case 1:
printf("请输入要插入的节点的前驱节点下标:");
scanf("%d", &pos);
printf("请输入要插入的节点信息:");
scanf("%s %s %lf",&isbn, &bookname, &price);
Node *insertNode = CreateNode(isbn, bookname, price);
if(insertElem(L, insertNode, pos)){
printf("------------------------------\n");
printf("插入成功!");
}
else{
printf("------------------------------\n");
printf("插入失败!");
free(insertNode);
}
break;
case 2:
printf("请输入要删除的元素下标:");
scanf("%d", &pos);
if(deleteElem(L, pos)){
printf("------------------------------\n");
printf("删除成功!");
}
else{
printf("------------------------------\n");
printf("删除失败!");
}
break;
case 3:
printf("请输入要更新的元素下标:");
scanf("%d", &pos);
printf("请输入更新值:");
scanf("%s %s %lf",&isbn, &bookname, &price);
Node *updatedNode = CreateNode(isbn, bookname, price);
if(! updateNode(L, updatedNode, pos)){
free(updatedNode);
printf("------------------------------\n");
printf("更新失败!");
}
else{
printf("------------------------------\n");
printf("更新成功!");
}
break;
case 4:
printf("请输入要查询的元素下标:");
scanf("%d", &pos);
printf("------------------------------\n");
queryNode(L, pos);
break;
case 5:
showList(L);
break;
default:
printf("------------------------------\n");
printf("非法输入!");
break;
}
}
return 0;
}
运行示例
总结
通过这段代码,我们可以看到如何使用C语言实现一个简单的单链表。单链表的基本操作包括节点的插入、删除、更新、查询和展示。希望这篇博客能帮助你更好地理解单链表的实现原理。
---
希望这篇博客文案对你有帮助!