【C++】谨以此文纪念不起眼的指针给我的迎头暴击,用以警示自己保持细心

错误一眼可见:访问的指针为空

#include "pch.h"
#include <iostream>
#include<string>
using namespace std;

typedef void linklist;
typedef struct _LinkListNode  
{
	struct _LinkListNode  *next;   /*本身对象即头指针,然后.next里面放置的是下一个相连的对象首地址*/
	int data;
	int length;
}linklistnode;

linklist* linklist_create()
{
	linklistnode* ret;
	ret = (linklistnode*)malloc(sizeof(linklistnode));
	memset(ret, 0, sizeof(linklistnode));
	ret->length = 0;  //第一个位置不放数据  ,存放计数和链表
	ret->next = NULL;
	cout << ret->length << endl;
	return  ret;
}

linklist* linklist_insert(linklist *list, int pos, linklistnode* node)
{
	/*typedef struct _LinkListNode
	{
		struct _LinkListNode  *next;   
		int data;
		int length;
	}linklistnode;*/
	linklistnode *ret  = (linklistnode*)list;
	cout << "此时ret->data: "<<ret->data << endl;
	linklistnode *current = ret;

	if (pos<0 || pos>ret->length)
	{
		cout << "链表长度依然为:"<<ret->length << endl;
		cout << "插入位置错误,请确定位置是不是大于当前最末端位置" << ret->length << "或者小于0" << endl;
		return NULL;
	}
	else
	{
		for (int i = 0; i < pos; i++)
		{
			cout << "执行了位置判断" << endl;
			current->next=current->next->next;  //current作为游标
		}
		node->next = current->next;  /*完成连接*/
		current->next = node;
		ret->length++;
		cout << "插入成功  " << node->data << "长度  " << ret->length << endl;
		cout << "插入后尾接ret->next->data: " << ret->next->data << endl;
		return  ret;
	}
}


linklist linklist_node_show( linklist* list,int pos )
{
	linklistnode *ret = (linklistnode*)list;
	cout << "此时ret->data: " << ret->data << endl;
	linklistnode *current = ret;
	if (pos<0 || pos>=ret->length)
	{
		cout << "查询位置错误,请确定位置是不是大于" << ret->length << "或者小于0";
		return ;
	}
	else
	{
			for (int i = 0; i < pos; i++)
			{
				current = current->next;
			}
			cout << pos << "位置的data值为: " << current->next->data << endl;

	}
}


 int main()
{
	linklist *list;
	list =linklist_create();
	//char a[50] = "a卡卡molaiwei";
	//char b[10] = "莫莱维";
	//char c[10] = "1一";
	//char d[10] = "2-2";

	linklistnode a1, b1, c1, d1;
	//strcpy_s(a1.data,);
	a1.data = 1;
	cout << a1.data << endl;
	//memcpy(a1.data, a, sizeof(a));
	cout << a1.data << endl;
	//strcpy_s(b1.data, "这是第二个插入到0的值");
	b1.data = 2;
	list = linklist_insert(list, 0, &a1);
	linklistnode* styer = (linklistnode*)list;
	cout << "插入一次之后list->next的地址" << styer->next << endl;
	cout << "node的首地址" << &a1<<endl;
	list = linklist_insert(list, 0, &b1);
	linklist_node_show(list, 0);

	cout << "分割线+++++++++++++++" << endl;
#if 0
	linklistnode *node;
	node = (linklistnode*)malloc(sizeof(linklistnode));
	node->data = 67;
	node->next = &a1;
	cout <<"当前data:   "<< node->data<< "  尾接的data:"<<node->next->data << endl;
	//linklistnode  *current = node->next;
	//cout << "当前临时尾接data:" << current->data<</*<<"  临时尾接:"<<current->next->data<<*/endl;
	//b1.next = current;
	//current = &b1;
	//cout << "当前临时尾接 curren->data:" << current->data<<endl;
	//cout << "当前尾接    node->next->data:" << node->next->data<< endl;  //为什么还是原值。因为只是改变了current指针的指向,虽然current和node->指向的是同一块区域,
	//																														 //但是本质上是两个指针,修改current指向并未改变node->next的指向,
	//																														 //正确的应该是先让current(游标)接收node->的指针地址,然后再让current指向node->next ;

	linklistnode *current_new = node;
	cout << "此时游标位置current_new-> data: " << current_new->data << endl;
	cout << "此时游标位置下一个current_new->next-> data: " << current_new->next->data << endl;
	current_new = current_new->next;
	cout << "后移后游标位置current_new-> data: " <<current_new->data<<endl;

	b1.next = current_new->next;   //尾接
	current_new->next = &b1;
	cout << "当前尾接    node->next->data:" << node->next->data <<"末尾位置:"<<node->next->next->data<< endl; 
#endif
	system("pause");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值