数据结构笔记

链表

<pre name="code" class="cpp">#include<iostream>
using namespace std;
struct stNode
{
	int data;
	stNode *next;
};
void output(stNode *p,int n)
{
	stNode *current = p->next;
	for (int i = 0; i < n; i++)
	{
		cout << current->data;
		current = current->next;
	}
}

void deleteList(stNode *head)
{
	//删除链表,从头开始删除,首先删除头节点,然后依次删除
	stNode *current = head;
	while (current != NULL)
	{
		stNode *temp = current->next;//用临时指针变量来储存下个节点指针
		delete current;//删除当前节点
		current = temp;//将当前指针变量赋值为下一个节点指针
	}
	cout << "调用了删除链表函数" << endl;
}
int main()
{
	stNode *head = new stNode;//建立头节点
	head->next = new stNode;//建立第一个节点
	stNode *current = head->next;//将当前指针指向第一个节点
	for (int i = 0; i < 10; i++)
	{
		current->data = i; 
		current->next = new stNode;
		current = current->next;//将当前指针指向下一个节点
	}
	output(head,10);
	//deleteList(head);
	stNode *current2 = head;
	while (current2 != NULL)
	{
		stNode *temp = current2->next;//用临时指针变量来储存下个节点指针
		delete current2;//删除当前节点
		current2 = temp;//将当前指针变量赋值为下一个节点指针
	}

}


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值