当前位置:   article > 正文

单链表的C语言实现_单链表的主程序

单链表的主程序

如下为单链表的实现,代码实现了单链表的创建、数据插入、数据删除、计算链表长度、修改指定数据、查找数据、打印链表等功能。运行环境为: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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

4.运行结果截图
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/557111
推荐阅读
相关标签
  

闽ICP备14008679号