数据结构题型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;
}
相关推荐
CQ_YM21 分钟前
数据结构之栈
数据结构·算法·
xlq2232243 分钟前
24.map set(下)
数据结构·c++·算法
立志成为大牛的小牛1 小时前
数据结构——五十四、处理冲突的方法——开放定址法(王道408)
数据结构·学习·程序人生·考研·算法
子一!!1 小时前
数据结构===红黑树===
数据结构
代码游侠2 小时前
复习——栈、队列、树、哈希表
linux·数据结构·学习·算法
熊猫_豆豆3 小时前
LeetCode 49.字母异位组合 C++解法
数据结构·算法·leetcode
小白程序员成长日记4 小时前
2025.12.02 力扣每日一题
数据结构·算法·leetcode
永远都不秃头的程序员(互关)4 小时前
在vscodeC语言多文件编译实战指南
c语言·数据结构·算法
立志成为大牛的小牛5 小时前
数据结构——五十三、处理冲突的方法——拉链法(王道408)
数据结构·学习·考研·算法
SHOJYS5 小时前
思维难度较大 贪心优化背包 [USACO22DEC] Bribing Friends G
数据结构·算法·深度优先