数据结构第二章线性表代码

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <initializer_list>
#include <deque>
#include <list>
#include <array>
#include <forward_list>
#include <sstream>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <memory>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <functional>
#include <type_traits>
#include <utility>
#include <tuple>
#include <bitset>
#include <regex>
#include <random>
#include <iomanip>
#include <ctime>
#include <cmath>
using namespace::std;
using ElementType = int;
#define ERROR -1
struct LNode;
typedef LNode* PtrToLNode;
typedef PtrToLNode Position;
typedef PtrToLNode List;
struct LNode
{
	ElementType Data;
	PtrToLNode Next;
};

int Length(List L)
{
	Position p;
	int cnt = 0;
	p = L;
	while (p)
	{
		p = p->Next;
		++cnt;
	}
	return cnt;
}

ElementType FindKth(List L, int k)
{
	Position p;
	p = L;
	int cnt = 1;
	while (p&&cnt < k)
	{
		p = p->Next;
		++cnt;
	}
	if ((cnt == k)&&p)
		return p->Data;
	else
		return ERROR;
}

Position Find(List L,ElementType aim)
{
	Position p;
	p = L;
	while(p)
	{
		if (aim == p->Data)
			return p;
		p = p->Next;
	}
	return nullptr;
}

List Insert(List L, ElementType X, int i)
{
	Position tmp, pre;
	tmp = (Position)malloc(sizeof(LNode));
	tmp->Data = X;
	if (i == 1)
	{
		tmp->Next = L;
		return tmp;
	}
	else
	{
		int cnt = 1;
		pre = L;
		while (pre&&cnt < i - 1)
		{
			pre = pre->Next;
			++cnt;
		}
		if (pre == nullptr || cnt != i - 1)
		{
			cout << "invalid input i" << endl;
			return nullptr;
		}
		else
		{
			tmp->Next = pre->Next;
			pre->Next = tmp;
			return L;
		}
	}
}

List Insert2(List L, ElementType X, int i)
{
	Position pre, tmp;
	int cnt = 0;
	pre = L;
	while (pre&&cnt < i - 1)
	{
		pre = pre->Next;
		++cnt;
	}
	if (pre == nullptr || cnt != i- 1)
	{
		cout << "invalid i" << endl;
		return nullptr;
	}
	else
	{
		tmp = (Position)malloc(sizeof(LNode));
		tmp->Data = X;
		tmp->Next = pre->Next;
		pre->Next = tmp;
		return L;
	}
}

bool mydelete(List L, int i)
{
	Position pre, tmp;
	int cnt = 0;
	pre = L;
	while (pre&&cnt < i - 1)
	{
		pre = pre->Next;
		++cnt;
	}
	if (pre == nullptr || cnt != i - 1 || pre->Next == nullptr)
	{
		cout << "invalid delete" << endl;
		return false;
	}
	else
	{
		tmp = pre->Next;
		pre->Next = tmp->Next;
		free(tmp);
		return true;
	}
}

void show(List L)
{
	Position p = L->Next;
	while (p)
	{
		cout << p->Data << endl;
		p = p->Next;
	}
}

int main()
{
	PtrToLNode first = (PtrToLNode)malloc(sizeof(LNode));
	first->Data = 1;
	PtrToLNode second = (PtrToLNode)malloc(sizeof(LNode));
	second->Data = 2;
	first->Next = second;
	second->Next = nullptr;
	List L = (List)malloc(sizeof(LNode));
	L->Next = first;
	Insert2(L, 3, 3);
	Insert2(L, 4, 4);
	show(L);
	mydelete(L, 2);
	show(L);
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值