我的数据结构之路漫漫啊啊啊啊啊啊啊

写在秋招之前~开始学数据结构 不知道能学多少,但是能学夺少算多少~奥里给!!!`

这个单链表是跟着这位博主的思路写的!超级清晰~有关一系列单链表的操作

emmmm 首先是C++的单链表啊~主要是内存空间的分配 太容易混淆了,用结构体做的,没有用类做那么清晰~但是理解就OK了哈哈哈哈

struct  Node //节点
{
	int data;
	Node *next;
};
struct Linklist //指向节点的指针
{
	Node* head;
};
void Insert(Linklist &L,int i,int elem)
{
	Node *n=new Node();
	n->next = NULL;
	n->data = elem;//首先分配一个内存给新进来的元素~
	

	if (L.head==NULL)
	{
		L.head = n;//插入的是第一个元素就认为是头节点
		return;
	}
	if (i==0)
	{
		n->next = L.head->next;
		L.head = n;
		return;
	}
	Node *Currnode = L.head;
	int count = 0;
	while (Currnode->next != NULL && count<i-1)
	{
		Currnode = Currnode->next;
		count++;
	}
	if (count ==i-1)
	{
		n->next = Currnode->next;
		Currnode->next = n;
	}
}
void PrintList(Linklist L)
{
	if (L.head==NULL)
	{
		return;
	}
	Node* Currnode = L.head;
	while (Currnode!= NULL)
	{
		cout << Currnode->data << " ";
		Currnode = Currnode->next;
	}
	cout << endl;
}
void InitialList(Linklist *L)
{
	Node *n = new Node();
	n->next = NULL;
	L->head = n;
}
int GetListLLength(Linklist L)
{
	int count = 0;
	while (L.head != NULL)
	{
		count++;
		L.head = L.head->next;		
	}
	return count;
}
void DeleteList(Linklist *L,int i)
{
	Node *n = new Node();
	Linklist L2;
	InitialList(&L2);
	L2 = *L;
	if (L->head==NULL||i>GetListLLength(*L)-1)
	{
		return;
	}
	int count = 0;
	while (count<i-1)
	{
		L2.head = L2.head->next;
		count++;
	}
	n = L2.head->next;
	L2.head->next = L2.head->next->next;	
	delete n;

}
void test03()
{
	Linklist List;
	InitialList(&List);
	for (int i = 0; i < 10; i++)
	{
		Insert(List, i, i);
	}
	PrintList(List);
	cout << "链表目前长度为:" << GetListLLength(List) << endl;
	DeleteList(&List, 2);
	PrintList(List);
	cout << "链表目前长度为:" << GetListLLength(List) << endl;

int main()
{
	cout << "正在测试我的代码。。" << endl;
	srand((unsigned int)time(NULL));
	test03();
	system("pause");

}

接下来是循环链表~这个指针的指向顺序好容易弄混 😔主要就在于要把当前链表的头节点赋值给新的一个节点,然后慢慢移动,CurrNode太重要拉!

//循环链表
void InitialCircleList(Linklist *L)
{
	Node *n = new Node();
	n->next = n;
	L->head = n;
}
int GetCircleLength(Linklist L)
{
	int count = 1;
	Node *current = new Node();
	current = L.head;
	while (current->next !=L.head)
	{
		count++;
		current = current->next;
	}
	//delete current;
	return count;
}
void InsertCircle(Linklist *L, int i, int elem)
{
	Node *n = new Node();
	n->data = elem;
	n->next = NULL;
	if (i<0 )
	{
		return;
	}
	int count = 0;
	if (i==0)
	{
		
		n->next = n ;
		L->head = n;
		return;
	}
	Node *CurrNode = new Node();
	CurrNode = NULL;
	CurrNode = L->head;
	while ((CurrNode->next != L->head) &&(count<i-1))
	{
		CurrNode = CurrNode->next;
		count++;
	}
	if (count==i-1)
	{
		

		n->next= CurrNode->next;
		CurrNode->next = n;
		L;
	}
	//delete CurrNode;
}
void PrintCircle(Linklist L)
{
	int index = 0;
	if (GetCircleLength(L)==0)
	{
		return;
	}
	
	Node *CurrNode = new Node();
	CurrNode = L.head;
	while (index< GetCircleLength(L))
	{
		cout << CurrNode->data;
		CurrNode = CurrNode->next;
		index++;
	}
	cout << endl;

}
void DeleteCircleByIndex(Linklist *L, int i)
{
	if (i<0||i>GetCircleLength(*L))
	{
		return;
	}
	Node *n = new Node();
	Node *CurrNode = new Node();
	CurrNode = L->head;
	int count = 0;
	while (CurrNode->next!= L->head && count<i-1)
	{
		CurrNode = CurrNode->next;
		count++;
	}
	n=CurrNode->next;
	CurrNode->next = CurrNode->next->next;
	delete n;
}
void test05()
{
	Linklist L;
	InitialCircleList(&L);
	for (int i=0;i<10;i++)
	{
		InsertCircle(&L, i, i);
	}
	cout << "现在的长度为:" << GetCircleLength(L) << endl;
	cout << "循环链表为:" << endl;
	PrintCircle(L);
	DeleteCircleByIndex(&L,2);
	cout << "现在的长度为:" << GetCircleLength(L) << endl;
	PrintCircle(L);
}

int main()
{
	cout << "正在测试我的代码。。" << endl;
	srand((unsigned int)time(NULL));
	test05();
	system("pause");
}

还有个疑问就是 我不delete掉 会不会内存泄漏了呢💦

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值