赞
踩
如下为单链表的实现,代码实现了单链表的创建、数据插入、数据删除、计算链表长度、修改指定数据、查找数据、打印链表等功能。运行环境为:Windows 10。集成开发工具为:visual studio 2019。
1.头文件SingleList.h
#pragma once #ifndef SINGLELIST_H #define SINGLELIST_H struct Node { int data; struct Node* Next; }; struct Node* CreatList();//创造空链表,并且返回头指针 struct Node* CreatNode(int data);//创造结点 int LengthofList(struct Node* header);//计算链表长度 void PrintList(struct Node* header);//打印链表 struct Node* FindPoint(struct Node* header, int data);//查找指定数据,并返回数据地址 void InsertByHeader(struct Node* header, int data);//在表头后插入数据 struct Node* InsertByPosition(struct Node* header, int data,int n);//在第n个数据后面插入数据data void DeletNodeByPoint(struct Node* header, int data);//在链表中删除指定数据 void ModifyValue(struct Node* header, int data, int value);//将链表里面的data更改为value #endif // !SINGLELIST_H
2.源文件SingleList.c
#include<stdio.h> #include<malloc.h> #include"SingleList.h" struct Node* CreatList()//创造空链表,并且返回头指针 { struct Node* header = (struct Node*)malloc(sizeof(struct Node)); header->Next = NULL; return header; } struct Node* CreatNode(int data)//创造结点 { struct Node* ptr = (struct Node*)malloc(sizeof(struct Node)); ptr->data = data; ptr->Next = NULL; return ptr; } int LengthofList(struct Node* header)//计算链表长度 { struct Node* p = header->Next; int len = 0; while (p) { len++; p = p->Next; } return len; } void PrintList(struct Node* header)//打印链表 { struct Node* p = header->Next; while (p) { printf("%d ", p->data); p = p->Next; } printf("\n"); } struct Node* FindPoint(struct Node* header, int data)//查找指定数据,并返回数据地址 { struct Node* p = header->Next; while (p) { if (p->data == data) { printf("找到指定数据%d\n",data); return p; } p = p->Next; } printf("未找到指定数据\n"); return p; } void InsertByHeader(struct Node* header, int data)//在表头后插入数据 { struct Node* p=CreatNode(data); p->Next = header->Next; header->Next = p; } struct Node* InsertByPosition(struct Node* header, int data, int n)//在第n个数据后面插入数据data { if (n > LengthofList(header)) { printf("超出链表长度!!!\n"); return NULL; } struct Node* p = header; struct Node* q = (struct Node*)malloc(sizeof(struct Node)); q->data = data; while (n--) { p = p->Next; } q->Next = p->Next; p->Next = q; return q; } void DeletNodeByPoint(struct Node* header, int data)//在链表中删除指定数据 { struct Node* p = header->Next; struct Node* current=header; while (p) { if (p->data == data) { current->Next = p->Next; printf("数据%d删除成功!\n",data); break; } current = p; p = p->Next; } if(!p) printf("未找到指定数据%d,删除失败!\n",data); } void ModifyValue(struct Node* header, int data, int value)//将链表里面的data更改为value { struct Node* p = FindPoint(header, data); p->data = value; }
3.主程序main.c
#include"SingleList.h" int main() { struct Node* list = CreatList();//创建空链表 for (int i = 0; i < 10; i++) { InsertByHeader(list, i); } printf("the length of the list is:%d\n", LengthofList(list));//打印数据长度 PrintList(list);//打印链表 FindPoint(list, 3);//查找数据3 DeletNodeByPoint(list, 2);//删除数据2 PrintList(list);//打印链表 DeletNodeByPoint(list, 5); PrintList(list); ModifyValue(list, 1, 11);//将数据1修改为11 FindPoint(list, 1);//查找数据3 PrintList(list); InsertByPosition(list, 19, 5);//在第5个数后面插入19 PrintList(list); return 0; }
4.运行结果截图
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。