Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Deleting the duplicate integer repeating in a linked list . The code is not working , can you give me the reason that why this code is not working.

What I have tried:

#include<stdio.h>
#include<stdlib.h>
typedef struct nodetype 
{
	int info ;
	struct nodetype *next ;
}node;
void rem(node*);
int main()
{
	int a=1,item;
	node *head=NULL,*ptr,*lol=NULL;
	printf("enter the integers");
	while(a<=5)
	{   
	head=(node*)malloc(sizeof(node));
    scanf("%d",&head->info);
    if(lol != NULL)
    {
    	ptr->next=head;
    	ptr = head;
	}
    else
	{
	 lol=ptr=head;
	}	
    	   	
    a++;
	}
	ptr->next=NULL;
	ptr=lol;
	rem(ptr);
	return 0;	
}
void rem(node *head)
{
	int i;
	node *brown,*ptr,*blue,*red,*pink,*yellow;
	brown=yellow=ptr=pink=blue=red=head;
	while(head!=NULL)
	{
		i=head->info;
		head=head->next;
		red=head;
		while(red!=NULL)
		{
			brown=red;
			while(red->info!=i)
			{
				pink=red;
				red=red->next;
			}
			if(red->info==i)
			{	
				yellow=red;
				pink->next=red->next;
				free(yellow);
			}
			red=brown;
			red=red->next;
		}
		
	}
		while(ptr!=NULL)
	{
		printf("%d\n",ptr->info);
		ptr=ptr->next;
	}
}
Posted
Updated 11-Mar-17 6:55am

This is your homework, and getting it working is part of the task! If we do it for you, you don't learn how to do it later, when projects are a lot more complex.

So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
User-13020352 13-Mar-17 11:36am    
I dont know how to remove segmentation fault in this code ?
this is coming in debugging . please solve this for me . Many doughts will be cleared in use of pointers .
OriginalGriff 13-Mar-17 11:52am    
You wrote the code, you know what you think it should be doing.
Use the debugger, and find out where it is doing something you don't expect. That's part of your homework, and it's a skill - one that it's a lot easier to learn on a small program like this than it is on a multi-million line behemoth!

Give it a try - see what you can find out.
User-13020352 15-Mar-17 9:30am    
yeah I solved it and learnt debugging too . :)
OriginalGriff 15-Mar-17 9:40am    
Excellent! :thumbsup:
It gets to be fun, debugging - and the more you do it, the better you get at it.

Mind you, it can also have you banging your head on the desk in frustration... :laugh:
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, it is an incredible learning tool.

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
 
Comments
User-13020352 13-Mar-17 10:21am    
How to debug using dev c++ ?
Patrice T 13-Mar-17 11:01am    
I don't know "Dev c++".
But I think you will find some thing with "Google Dev c++"
User-13020352 15-Mar-17 9:31am    
Thanks for your support .
Learnt debugging .
Patrice T 15-Mar-17 9:46am    
Just curious, what do you think of debugger :)

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