Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C++
#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;
}
Posted
Updated 14-Oct-17 17:48pm
v3

1 solution

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

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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900