数据结构题型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;
}
相关推荐
挺菜的2 小时前
【算法刷题记录(简单题)003】统计大写字母个数(java代码实现)
java·数据结构·算法
2401_858286112 小时前
125.【C语言】数据结构之归并排序递归解法
c语言·开发语言·数据结构·算法·排序算法·归并排序
双叶8363 小时前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)
c语言·开发语言·数据结构·c++
学不动CV了6 小时前
数据结构---链表结构体、指针深入理解(三)
c语言·arm开发·数据结构·stm32·单片机·链表
算法_小学生8 小时前
LeetCode 287. 寻找重复数(不修改数组 + O(1) 空间)
数据结构·算法·leetcode
Wo3Shi4七11 小时前
哈希冲突
数据结构·算法·go
V我五十买鸡腿12 小时前
顺序栈和链式栈
c语言·数据结构·笔记·算法
七灵微13 小时前
数据结构实验习题
数据结构
杰克尼1 天前
BM5 合并k个已排序的链表
数据结构·算法·链表
xiaolang_8616_wjl1 天前
c++文字游戏_闯关打怪
开发语言·数据结构·c++·算法·c++20