数据结构,介绍单链表的头插法、尾插法、删除操作完整代码可直接运行

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

typedef struct Node {
	int data;
	struct Node* next;
};

//初始化,创建一个有头结点的链表
Node* InitList()
{
	Node* L = (Node*)malloc(sizeof(Node));
	L->data = 0;
	L->next = L;//单循环链表,没有数据元素时,头结点指向自己
	return L;
}

//头插法创建链表
void Headinsert(Node* L,int data)
{
	Node* node = (Node*)malloc(sizeof(Node));//新建node节点并为它开辟空间
	node->data = data;
	node->next = L->next;//指针断开
	L->next = node;//指针链接
}

void Print(Node* L)//参数是传入的头指针
{
	Node* node = L->next;
	while (node!=L)//循环链表遍历结束的标志
	{
		cout << node->data;
		node = node->next;
	}
	cout << endl;
}
int main()
{
	Node* L = InitList();
	Headinsert(L, 1);
	Headinsert(L, 2);
	Headinsert(L, 3);
	Headinsert(L, 4);
	Headinsert(L, 5);
	Headinsert(L, 6);
	Print(L);
	system("pause");
	return 0;
}

接下来我们在此代码的前提下,再加一段尾插法的代码

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;

typedef struct Node {
	int data;
	struct Node* next;
};

//初始化,创建一个有头结点的链表
Node* InitList()
{
	Node* L = (Node*)malloc(sizeof(Node));
	L->data = 0;
	L->next = L;//单循环链表,没有数据元素时,头结点指向自己
	return L;
}

//头插法创建链表
void Headinsert(Node* L, int data)
{
	Node* node = (Node*)malloc(sizeof(Node));//新建node节点并为它开辟空间
	node->data = data;
	node->next = L->next;//指针断开
	L->next = node;//指针链接
	L->data++;//记录数据个数寻找最后一个结点
}

void TailInsert(Node* L, int data)
{
	Node* n = L;//前插时找到的最后一个结点
	Node* node = (Node*)malloc(sizeof(Node));
	node->data = data;
	while (n->next != L)//n等于L时,说明已经遍历到最后一个结点
	{
		n = n->next;
	}
	node->next = L;
	n->next = node;//尾插法
}
void Print(Node* L)//参数是传入的头指针
{
	Node* node = L->next;
	while (node != L)//循环链表遍历结束的标志
	{
		cout << node->data;
		node = node->next;
	}
	cout << endl;
}

int main()
{
	Node* L = InitList();
	Headinsert(L, 1);
	Headinsert(L, 2);
	Headinsert(L, 3);
	Headinsert(L, 4);
	Headinsert(L, 5);
	Headinsert(L, 6);
	TailInsert(L, 7);
	TailInsert(L,8);
	Print(L);
	system("pause");
	return 0;
}

再加一条删除链表数据的功能

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
using namespace std;

typedef struct Node {
	int data;
	struct Node* next;
};

//初始化,创建一个有头结点的链表
Node* InitList()
{
	Node* L = (Node*)malloc(sizeof(Node));
	L->data = 0;
	L->next = L;//单循环链表,没有数据元素时,头结点指向自己
	return L;
}

//头插法创建链表
void Headinsert(Node* L, int data)
{
	Node* node = (Node*)malloc(sizeof(Node));//新建node节点并为它开辟空间
	node->data = data;
	node->next = L->next;//指针断开
	L->next = node;//指针链接
	L->data++;//记录数据个数寻找最后一个结点
}

void TailInsert(Node* L, int data)
{
	Node* n = L;//前插时找到的最后一个结点
	Node* node = (Node*)malloc(sizeof(Node));
	node->data = data;
	while (n->next != L)//n等于L时,说明已经遍历到最后一个结点
	{
		n = n->next;
	}
	node->next = L;
	n->next = node;//尾插法
}

//实现删除功能
int DeleteList(Node* L, int data)//参数分别是头指针和删除的结点
{
	Node* preNode = L;//记录要删除结点之前的一个结点
	Node* node = L->next;//node 结点指向L的第一个节点
	while (node != L)
	{
		if (node->data == data)
		{
			preNode->next = node->next;
			free(node);
			return TRUE;
		}
		preNode = node;
		node = node->next;	
	}
	return FALSE;
}
void Print(Node* L)//参数是传入的头指针
{
	Node* node = L->next;
	while (node != L)//循环链表遍历结束的标志
	{
		cout << node->data;
		node = node->next;
	}
	cout << endl;
}

int main()
{
	Node* L = InitList();
	Headinsert(L, 1);
	Headinsert(L, 2);
	Headinsert(L, 3);
	Headinsert(L, 4);
	Headinsert(L, 5);
	Headinsert(L, 6);
	TailInsert(L, 7);
	TailInsert(L, 8);
	DeleteList(L, 4);
	DeleteList(L, 7);
	Print(L);
	system("pause");
	return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值