数据结构题型6-后插结点操作

cpp 复制代码
#include <iostream>  //引入头文件
using namespace std;

typedef int Elemtype;

#define Maxsize 100
#define ERROR 0
#define OK    1

typedef struct LNode
{
	Elemtype data;//数据域
	struct LNode* next;//指针域
}LNode, * LinkList;

bool InitList(LinkList& L)   //初始化
{
	L = (LinkList)malloc(sizeof(LNode));
	if (L == NULL)
		return ERROR;
	L->data = 0;
	L->next = NULL;
	return OK;
}

bool ListEmpty(LinkList L)  //判断是否为空
{
	if (L->next != NULL)
	{
		cout << "not empty" << endl;
		return OK;
	}
	else
	{
		cout << "empty,只有头节点" << endl;
		return ERROR;
	}
}

int ListLength(LinkList L)
{
	LNode* p;
	p = L->next;
	int i = 0;
	while (p != NULL)
	{
		p = p->next;
		i++;
	}
	return i;
}

LinkList List_HeadInsert(LinkList& L)  //头插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		if (cin.get() == '\n') break;
	}
	return L;
}

LinkList List_TailInsert(LinkList& L)  //尾插法
{
	L = (LinkList)malloc(sizeof(LNode));
	L->data = 0;
	L->next = NULL;
	LNode* s = NULL, * r = NULL;
	r = L;
	int x = 0;
	while (cin >> x)
	{
		s = (LinkList)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		if (cin.get() == '\n') break;
	}
	s->next = NULL;
	return L;
}
LNode* GetElem(LinkList L, int i)
{
	if (i < 0) return NULL;
	int j = 0;
	LNode* p = L;
	while (p && j < i)
	{
		p = p->next;
		j++;
	}
	return p;
}
LNode* LocateElem(LinkList L, int e)
{
	LNode* p = L->next;
	while (p && p->data != e)
	{
		p = p->next;
	}
	return p;
}

bool InsertPriorNode(LNode* p, int e)
{
	if (p == NULL) return ERROR;
	LNode* s = (LinkList)malloc(sizeof(LNode));
	if (s == NULL) return ERROR;
	s->next = p->next;
	p->next = s;
	s->data = p->data;
	p->data = e;
	return OK;
}
//------------------------核心代码------------------------//
bool InsertNextNode(LNode* p, int e)
{
	if (p == NULL) return ERROR;
	LNode* s = (LinkList)malloc(sizeof(LNode));
	if (s == NULL) return ERROR;
	s->next = p->next;
	p->next = s;
	s->data = e;
	return OK;
}
//------------------------核心代码------------------------//
int main(void)
{
	LinkList L = NULL;
	List_TailInsert(L);  //尾插法
	cout << "------------------插入特定数据前------------------" << endl;
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	//cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	InsertNextNode(L->next, 100);
	cout << "------------------插入特定数据后------------------" << endl;
	cout << L << " " << L->data << " " << L->next << endl;
	cout << L->next << " " << L->next->data << " " << L->next->next << endl;
	cout << L->next->next << " " << L->next->next->data << " " << L->next->next->next << endl;
	cout << L->next->next->next << " " << L->next->next->next->data << " " << L->next->next->next->next << endl;
	return 0;
}
相关推荐
大数据张老师1 小时前
数据结构——邻接矩阵
数据结构·算法
深思慎考4 小时前
从合并两个链表到 K 个链表:分治思想的递进与堆优化
数据结构·链表·递归··队列·合并链表
又见野草4 小时前
软件设计师知识点总结:数据结构与算法(超级详细)
数据结构·算法·排序算法
曹牧7 小时前
C#:数组不能使用Const修饰符
java·数据结构·算法
大数据张老师7 小时前
数据结构——拓扑排序
数据结构
草莓工作室8 小时前
数据结构10:树和二叉树
数据结构
当战神遇到编程10 小时前
链表的概念和单向链表的实现
数据结构·链表
INGNIGHT10 小时前
单词搜索 II · Word Search II
数据结构·c++·算法
QuantumLeap丶12 小时前
《数据结构:从0到1》-06-单链表&双链表
数据结构·算法
violet-lz12 小时前
数据结构八大排序:快速排序-挖坑法(递归与非递归)及其优化
数据结构