Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<stdio.h>
#include<conio.h>
struct node *head=NULL;
static int count=0;

struct node
{
	int coef;
	int pow;
	struct node *link;
};

void showoff()
{
	struct node *trace;
	trace=head;
	printf("\n");
	printf("it's working!!!!!!!!!!!!!!!!'1");
	while(trace!=NULL)
	{
		printf("|  | |  | |%x|-->",trace->link);
		trace=trace->link;
		printf("it's working!!!!!!!!!!!!!!!!'2");
	}
	printf("it's working!!!!!!!!!!!!!!!!'3");
}

void omega(int degree)
{	
	int i;
	struct node *temp,*t;
	temp=head;
	t=head;

	for(i=0;i<=degree;i++)	
	{
		temp=(struct node*)malloc(sizeof(struct node));
		//printf("temp  it's working!!!!!!!!!!!!!!!!'\n");
		if(count==0)
		{
			temp->link=head;
			head=temp;
			count++;
			//printf("count==0 it's working!!!!!!!!!!!!!!!!'\n");
		}
		else if (count==1)
		{
			temp->link=head->link;
			head->link=temp;
			count++;
			//printf("count==1 it's working!!!!!!!!!!!!!!!!'\n");
		}
		else
		{
			printf("else  it's working!!!!!!!!!!!!!!!!'\n");
			while(t->link!=NULL)
			{
				t=t->link;	
				//printf("while loop of else it's working!!!!'\n");
			}	
			temp->link=t->link;
			t->link=temp;	
			count++;	
			//printf("outside else it's working!!!!!!!!!!!!!!!!'\n");
		}
		printf("omega it's working!!!!!!!!!!!!!!!!'\n");
	}	
}

int main()
{
	int opt,d;
	printf("---->!!!OPTIONS!!!<----\n");
	printf("1.Enter first polynomial\n");
	scanf("%d",&opt);
	
	switch(opt)
	{
		case 1:
			printf("Enter the degree of polynomial\n");
			scanf("%d",&d);
			omega(d);
			showoff();
			break;
	}
}


What I have tried:

First of all, this code I wrote is for creating linked list,so when user enter no. of nodes(here in the program it's denoted by degree) then the program should create nodes linked with each other, but instead of that, when I run the program it stops after taking inputs, it does feel like it's running and then it shows
"Process exited after 4.933 seconds with return value 3221225477
Press any key to continue . . ."

I tried to debug the program with my crazy style of writing printf statements as you can see in the code and found that 'while loop' which is inside 'else' is not working,and that's the reason why the program is crashing. Anyone knows why??? There is no error in the code . Please help it's driving me nuts, Thanks!
Posted
Updated 19-Aug-18 15:39pm
v5

Well... look at your code.
C++
printf("else  it's working!!!!!!!!!!!!!!!!'\n");
	while(t->link!=NULL)
	{
		t=t->link;	    (/*<--------------Here's the culprit*/)
			
	}
But what is in t when it starts? You don't set it to any value in that code ... or even show the definition of it! So unless you set it to a specific value at some point - and I doubt you do because it probably needs to be set inside your for loop - it could be pointing anywhere.

Use the debugger, and see exactly what it contains.

BTW: do yourself a favour and indent that lot correctly! It makes it a load easier to follow...
 
Share this answer
 
Comments
Member 13954179 19-Aug-18 11:54am    
t is just a another pointer which i created to traverse through the linked list
struct node *t;
t=head;
I declared it as above
OriginalGriff 19-Aug-18 11:58am    
Where? When? What was in head when you did that?

What does the debugger show?
Member 13954179 19-Aug-18 12:04pm    
You can see through the entire code which I posted in comment, and about the head it's value was NULL at the start, but after first iteration of for loop head will have the address of first node and in second iteration head->link will be equal to second node and so on.. the linked list should be created, but it doesn't
OriginalGriff 19-Aug-18 12:13pm    
"What does the debugger show?"
Not "what do you think the debugger would show if you used it?" - "What does the debugger actually show you is in the variables while your code is running?"

Never assume your code is right, or that what you think is happening actually is - check it! And teh perfect tool to check it is the debugger!
Member 13954179 19-Aug-18 11:57am    
here's the entire code


#include<stdio.h>
#include<conio.h>
struct node *head=NULL;
static int count=0;

struct node
{
int coef;
int pow;
struct node *link;



};
void showoff()
{
struct node *trace;
trace=head;
printf("\n");
printf("it's working!!!!!!!!!!!!!!!!'1");
while(trace!=NULL)
{
printf("| | | | |%x|-->",trace->link);
trace=trace->link;
printf("it's working!!!!!!!!!!!!!!!!'2");
}
printf("it's working!!!!!!!!!!!!!!!!'3");

}
void omega(int degree)
{
int i;
struct node *temp,*t;
temp=head;
t=head;

for(i=0;i<=degree;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
//printf("temp it's working!!!!!!!!!!!!!!!!'\n");
if(count==0)
{

temp->link=head;
head=temp;
count++;
//printf("count==0 it's working!!!!!!!!!!!!!!!!'\n");
}
else if (count==1)
{
temp->link=head->link;
head->link=temp;
count++;
//printf("count==1 it's working!!!!!!!!!!!!!!!!'\n");
}
else
{
printf("else it's working!!!!!!!!!!!!!!!!'\n");
while(t->link!=NULL)
{
t=t->link;
//printf("while loop of else it's working!!!!!!!!!!!!!!!!'\n");
}
temp->link=t->link;
t->link=temp;
count++;

//printf("outside else it's working!!!!!!!!!!!!!!!!'\n");

}


printf("omega it's working!!!!!!!!!!!!!!!!'\n");

}



}
int main()
{
int opt,d;
printf("---->!!!OPTIONS!!!<----\n");
printf("1.Enter first polynomial\n");
scanf("%d",&opt);

switch(opt)
{
case 1:
printf("Enter the degree of polynomial\n");
scanf("%d",&d);
omega(d);
showoff();
break;







}







}
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include<stdio.h>
#include<conio.h>
struct node *head=NULL;
static int count=0;

struct node
{
	int coef;
	int pow;
	struct node *link;
};

for(i=0;i<=degree;i++)
{
	temp=(struct node*)malloc(sizeof(struct node));
	//printf("temp  it's working!!!!!!!!!!!!!!!!'\n");
	if(count==0)
	{

		temp->link=head;
		head=temp;
		count++;
		//printf("count==0 it's working!!!!!!!!!!!!!!!!'\n");
	}
	else if (count==1)
	{
		temp->link=head->link;
		head->link=temp;
		count++;
		//printf("count==1 it's working!!!!!!!!!!!!!!!!'\n");
	}
	else
	{
		printf("else  it's working!!!!!!!!!!!!!!!!'\n");
		while(t->link!=NULL)
		{
			t=t->link;	    (/*<--------------Here's the culprit*/)
		}
		temp->link=t->link;
		t->link=temp;
		count++;

		//printf("outside else it's working!!!!!!!!!!!!!!!!'\n");

	}
}

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

C++
#include<stdio.h>
#include<conio.h>
static int count=0;

struct node
{
	int coef;
	int pow;
	struct node *link;
};
struct node *head=NULL; // Always define a structure before using it !

for(i=0;i<=degree;i++) // what is degree, it comes from nowhere
{

Quote:
I tried to debug the program with my crazy style of writing printf statements

Give a try to debugger, it will allow you to see the contain of variable during code execution.


There is an almost universal solution: Run your code on debugger step by step, inspect variables.
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 know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
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.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Member 13954179 19-Aug-18 14:01pm    
degree is just a input, which will given by user during runtime, and ppolymorphe thanks for your advice, sounds silly but I am new to programming and I don't know much about debuging.
Patrice T 19-Aug-18 14:02pm    
"I don't know much about debuging"
give it a try
Member 13954179 19-Aug-18 14:06pm    
So when I try to debug the program the IDE always crash, by the way it's Dev C++
Patrice T 19-Aug-18 14:11pm    
Can you try on another PC ? to see if it also crash.
Member 13954179 19-Aug-18 14:09pm    
Well I don't know if it's right, and somehow this time it didn't crash.
After debugging it show error "Program received signal SIGSEGV,Segmentation fault"

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