如何从链表中删除元素 [英] How to remove an element from linked list

查看:94
本文介绍了如何从链表中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个删除功能但是当我运行程序并删除一个元素然后显示后它显示我的垃圾值我不知道我做错了什么



我尝试过:



I have tried to create a remove function but when I run program and removes an element then after display it shows me garbage value I don't know what am I doing wrong

What I have tried:

#include<iostream>
using namespace std;
class node{
	int x;
	node *next;
	public:
		void set(int x)
		{
			this->x=x;
		}
		int get()
		{
			return x;
		}
		void setnext(node *next)
		{
			this->next=next;
		}
		node *getnext()
		{
			return next;
		}
};
class list{
	int size,elenum;
	node *head,*current;
	public:
		list()
		{
			head=new node();
			head->setnext(NULL);
			size=0;
			elenum=0;
			current=new node();
			current=NULL;
		}
		void create()
		{
			int v;
			cout<<"Enter value :";
			cin>>v;
			node *newnode=new node();
			newnode->set(v);
			if(current!=NULL)
			{
				newnode->setnext(NULL);
				current->setnext(newnode);
				current=newnode;
			}
			else
			{
				newnode->setnext(NULL);
				head->setnext(newnode);
				current=newnode;
			}
			size++;
		}
		void insert(int pos)
		{
			int val;
			cout<<"Enter value :";
			cin>>val;
			current=head->getnext();
			node *newnode=new node();
			newnode->set(val);
			for(int i=1;i<=pos;i++)
			{
				if(i=pos)
				{
					newnode->setnext(current->getnext());
					current->setnext(newnode);
					current=newnode;
				}
				current=current->getnext();
			}
			elenum++;
		}
		void remove(int pos)
		{
			node *temp=new node();
			current=head->getnext();
			for(int i=1;i<=pos;i++)
			{
				if(i=pos)
				{
					temp=current; 
					current->setnext(current->getnext());
					delete temp;
				}
				current=current->getnext();
			}
			elenum--;
		}
		int find(int x,int si)
		{
			current=head->getnext();
			for(int i=1;i<=size;i++)
			{
			    if(x=current->get())
			    {
			    	return i;
			    }
			}
			return 0;
		}
		int length()
		{
			return size;
		}
		int actlength()
		{
			int sum=size+elenum;
			return sum;
		}
		void display()
		{
			if(current!=NULL)
			{
			cout<<current->get();
		    }
		}
		void start()
		{
			current=head->getnext();
		}
		void next()
		{
			current=current->getnext();
		}
};
int main()
{
	list l1;
	int n1,n2,num;
	int ans,pos;
	cout<<"How many elements do you want to enter :";
	cin>>num;
	for(int i=1;i<=num;i++)
	{
		l1.create();
	}
	do
	{
		cout<<"\n1.Insert\n2.Remove\n3.Size of list\n4.Size after update\n5.Find\n6.Display\n7.End"<<endl;
		cout<<"Enter your choice :";
		cin>>ans;
		if(ans==1)
		{
			cout<<"Enter position where you want to enter :";
			cin>>pos;
			l1.insert(pos);
		}
		else if(ans==2)
		{
			cout<<"Enter position from which you want to remove your element :";
			cin>>pos;
			l1.remove(pos);
		}
		else if(ans==3)
		{
			cout<<"Size is "<<l1.length();
		}
		else if(ans==4)
		{
			cout<<"Size after some modification done by you "<<l1.actlength();
		}
		else if(ans==5)
		{
			n2=l1.actlength();
			cout<<"Enter the value you want to enter :";
			cin>>num;
			int n1=l1.find(num,n2);
			if(n1!=0)
			{
				cout<<"Your element was found at "<<n1<<endl;
			}
			else
			{
				cout<<"Your element is not in the list !"<<endl;
			}
		}
		else if(ans==6)
		{
			n2=l1.actlength();
			l1.start();
			for(int i=1;i<=n2;i++)
			{
				l1.display();
				cout<<endl;
				l1.next();
			}
		}
		else
		{
			break;
		}
	}while(1);
	return 0;
}

推荐答案

Quote:

它显示了我的垃圾价值我不知道我做错了什么

it shows me garbage value I don't know what am I doing wrong



最可能的原因是你搞砸了下一个删除元素时的指针。您是否考虑过更新上一个元素的 next 指针?



DIY解决方案:迟早你必须学习如何自己找bug,选择的工具是调试器。

拿一张纸和一支铅笔模拟你的链表。

假设你模拟一个5个元素的列表,绘制10列,每个元素2个; 1表示值,1表示指针,表示元素地址是A,B,C,D和E.

每次更新列表时都会在新行上写入新列表。

并删除第三个元素,第4和第5个元素保留在相同的列上,更新指针以反映更改。

现在使用指针查看代码正在执行的操作并查看当它不符合您的期望时,这就是您的错误。



有一个工具可以让您查看您的代码正在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。

然后测试删除过程中的第一个和最后一个元素。



调试器 - 维基百科,免费的百科全书 [ ^ ]



在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

调试器在这里o告诉你你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


The most probable reason is that you messed the next pointers when you deleted the element. Did you thought about updating the next pointer of previous element?

DIY solution: Sooner or later you have to learn how to find bugs yourself, the tool of choice is the debugger.
Take a sheet of paper and a pencil and simulate your linked list.
Say that you simulate a list of 5 elements, draw 10 columns, 2 per element; 1 for the value, 1 for the pointer, say that the elements addresses are A, B, C, D and E.
each times you update the list write the new list on a new row.
and delete the third element, 4th and 5th element stay on same columns, update pointers to reflect the changes.
Now use the pointer to see what your code is doing and see when it don't conform to your expectations, this is where is your bug.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Then test your delete procedure for first and last elements.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于如何从链表中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆