写一篇水文纪念一下我调试了几个小时的程序。。。

原题其实挺简单的 luogu P1160 队列安排

涉及到的就是简单的队列插入删除等操作

但是不知道为什么就是一直内存访问越界

调试的时候也一头雾水

后来经过三四个小时的调试才知道是我自己写的链表类初始化有问题。。。。

当时写的时候没注意,现在酿成大错了

template<class T>
LinkedList<T>::LinkedList()
{
	Node<T> a(0,NULL);
	front=pre=&a;
	cur=rear=NULL;
	size=0;
	position=0;
}

 就是上面的初始化头结点的时候,使用的是局部变量,结果后面的程序在cin操作结束之后,局部变量似乎被释放空间了,导致我的头结点所指向的节点为NULL,所以链表相当于一直再被清空。。。。。。。 然而这个错我当时在写链表类的时候还没发现。。

这回真是可以记好久了 我TM两天自习没写作业就调了这么一个破程序 凸(艹皿艹 )

将上面的初始化代码改为下列代码后顺利运行

template<class T>
LinkedList<T>::LinkedList()
{
	Node<T> *a=NewNode(0,NULL);
	front=pre=a;
	cur=rear=NULL;
	size=0;
	position=0;
}
#include<iostream>
using namespace std;
template<class T>
class Node
{
	public:
		T data;
		Node<T>* next;
		Node(const T& item,Node<T>* ptr=NULL){
		data=item;
		next=ptr;
		}
		~Node(){}
};
template<class T>
class LinkedList
{
	public:
		Node<T> *front,*rear;
		Node<T> *pre,*cur;
		int size;
		int position;
		Node<T> *NewNode(const T& item,Node<T> *ptr=NULL);
		void FreeNode(Node<T> *p);
	public:
		LinkedList();
		~LinkedList();
		LinkedList<T> &operator=(const LinkedList<T>& orgList);
		int Size();
		bool IsEmpty();
		int NextNode();
		int SetPosition(int pos);
		int GetPosition()const;
		void InsertAt(const T& item);
		void InsertAfter(const T& item);
		void DeleteAt();
		void DeleteAfter();
		T GetData()const;
		void SetData(const T& item);
		void Clear();
		void Print();
		void Printfront()
		{
			cout<<front->data<<endl;
		}
};
template<class T>
Node<T>* LinkedList<T>::NewNode(const T& item,Node<T> *ptr)
{
	Node<T>* a=new Node<T>(item,NULL);
	return a;
}
template<class T>
void LinkedList<T>::FreeNode(Node<T> *ptr)
{
	if(!ptr)
	{
		cerr<<"FreeNode:invalid node pointer!"<<endl;
		return;
	}
	delete ptr;
}
template<class T>
LinkedList<T>::LinkedList()
{
	Node<T>* a=NewNode(0,NULL);
	front=pre=a;
	cur=rear=NULL;
	size=0;
	position=0;
}
template<class T>
LinkedList<T>::~LinkedList()
{
	cur=front;
	while(!front->next){
		
		DeleteAfter();
	}
}
//template<class T>
//LinkedList<T>&LinkedList<T>::operator=(const LinkedList<T>& orgList)
//{
//	Node<T> *p=orgList.front;
//	Clear();
//	p=p->next;
//	while(p)
//	{
//		InsertAfter(p->data);
//		p=p->next;
//	}
//	SetPostion(orgList.position);
//	return *this;
//}
template<class T>
void LinkedList<T>::Clear()
{
	Node<T> * curNode=front,*nextNode;
	while(curNode)
	{
		nextNode=curNode->next;
		FreeNode(curNode);
		curNode=nextNode;
	}
	Node<T> a(0,NULL);
	front=pre=&a;
	cur=rear=NULL;
	size=0;
	position=-1;
}
template<class T>
int LinkedList<T>::Size()
{
	return size;
}
template<class T>
bool LinkedList<T>::IsEmpty()
{
	return size==0;
}
template<class T>
int LinkedList<T>::NextNode()
{
	if(position>0&&position<=size)
	{
		position++;
		pre=cur;
		cur=cur->next;
	}
	else{
		position=size+1;
	}
	return position;
}
template<class T>
int LinkedList<T>::SetPosition(int pos)
{
	if(!size){
		return -1;
	}
	if(pos<=0||pos>size){
		cerr<<"position error"<<endl;
		return -1;
	}
	pre=front;
	cur=front->next;
	position=1;
	for(int k=0;k<pos-1;k++)
	{
		position++;
		pre=cur;
		cur=cur->next;
	}
	return position;
}
template<class T>
int LinkedList<T>::GetPosition()const{
	return position;
}
template<class T>
void LinkedList<T>::InsertAt(const T& item)
{
	Node<T> *newNode=new Node<T>(item,NULL);
	newNode->next=cur;
	pre->next=newNode;
	cur=newNode;
	size++;
	if(!rear)rear=newNode;
}
template<class T>
void LinkedList<T>::InsertAfter(const T&item)
{
	Node<T> *newNode=new Node<T>(item,NULL);
	if(!cur)
	{
		front->next=newNode;
		rear=newNode;
		pre=front;
	}
	else{
		newNode->next=cur->next;
		cur->next=newNode;
		if(!newNode->next)rear=newNode;
		pre=cur;
	}
	size++;
	position++;
	cur=newNode;
}
template<class T>
void LinkedList<T>::DeleteAt()
{
	Node<T> * oldNode;
	if(!cur)
	{
		cerr<<"DeleteAt:current position is invalid!"<<endl;
		return;
	}
	else
	{
		if(cur->next){
			pre->next=cur->next;
			FreeNode(cur);
			cur=pre->next;
		}
		else{
			int a=GetPosition()-2;
			pre->next=cur->next;
			FreeNode(cur);
			SetPosition(a);
		}
		size--;
	}
}
template<class T>
void LinkedList<T>::DeleteAfter()
{
	if(!cur||cur==rear){
		cerr<<"DeleteAfter:current position is invalid!"<<endl;
		return;
	}
	if(cur->next==rear)
	{
		cur->next=NULL;
		FreeNode(rear);
		rear=cur;
	}
	else{
		Node<T>* oldNode=cur->next;
		cur->next=cur->next->next;
		FreeNode(oldNode);
	}
	size--;
}
template<class T>
T LinkedList <T>::GetData()const
{
	if(!cur)
	{
		cerr<<"Data:current node not exist!"<<endl;
	}
	return cur->data;
}
template<class T>
void LinkedList<T>::SetData(const T& item)
{
	if(!cur)
	{
		cerr<<"Data:current node dose not existed!"<<endl;
		return ;
	}
	cur->data=item;
}
template<class T>
void LinkedList<T>::Print()
{
	if(!cur){
	cout<<"Empty!"<<endl;
	return;
	}
	else{
		Node<T>* curNode=front->next;
		while(curNode!=NULL){
			cout<<curNode->data<<' ';
			curNode=curNode->next;
		}
	}
	cout<<endl;
}
int main()
{
	int n,k,p;
	cin>>n;
	LinkedList<int> mens;
	if (n==0)return 0;
	else
	{
		mens.InsertAt(1);
	}
	for(int i=2;i<=n;i++)
	{
		mens.SetPosition(1);
		int text1=mens.GetData();
		cin>>k>>p;
		while(mens.GetData()!=k)
		{
			int text2=mens.GetData();
			mens.SetPosition(mens.GetPosition()+1);
		}
		if (p)mens.InsertAfter(i);
		else mens.InsertAt(i);
	}
	mens.Print();
	int m,x;
	cin>>m;
	for(int i=0;i<m;i++)
	{
		cin>>x;
		mens.SetPosition(1);
		while(mens.GetData()!=x && mens.GetPosition()!=mens.Size())
		{
			mens.SetPosition(mens.GetPosition()+1);
		}
		if(mens.GetPosition()<=mens.Size() && mens.GetData()==x)mens.DeleteAt();
		else continue;
	}
	mens.Print();
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值