Link to home
Start Free TrialLog in
Avatar of Asasas ASASAS
Asasas ASASAS

asked on

What's the problem with this code? plz anyone tell! I am stuck on this since last month. Trying to get the problem`

Linked List code to get a list of n nodes.

The Code compiles without errors and warnings, but at the run time, the functions don't run, they just print the first line, and after that i have to close the TURBO C application. :(

Here's the CODE
"#include<stdio.h>
#include<conio.h>
#include<stdlib.h>


typedef struct link
{
int info;
struct link* next;
}node;

void create  (node*);
void del (node*);
void modify (node*);
node* mysearch( node*, int );
void display (node*);

//node* start; //not global, declared as variable local to main

void create  (node* st)
	{
	int aftr,inf;
	node* afternode;
	node* n;
	node* temp;
	node* p;
	p=start;
	printf("\nHello!! So you wish to add a new node\n\nEnter the node after which the insertion to be done.");
	scanf("%d", &aftr);
	//afternode = mysearch(st,aftr);
	while((p->info!=aftr)||(p!=NULL))
		p=p->next;
	afternode=p;
	printf("Found %d", aftr);
	if (afternode!=NULL)
		{
		printf("Enter the data for the new node");
		scanf("%d", &inf);
		n = (node*)malloc(sizeof(node));
		n->info=inf;
		temp = afternode->next;
		n->next = temp;
		afternode->next = n;
		}
	}


void del (node* st)
	{
	int inf;
	node* l;
	node* p=st;
	node* culprit;
	printf("Enter the node data which you wish to delete.");
	scanf("%d", &inf);
	//culprit = mysearch (st,inf);
	while((p->info !=inf)||(p!=NULL))
		p=p->next;
	culprit=p;
	if (culprit!= NULL)
		{
		while(l->next != culprit)
		l=l->next;
		l->next=culprit->next;
		culprit->next = NULL;
		free(culprit);
		}
	}


void modify (node* st)
	{
	int inf,newdata;
	node* key;
	node* p=st;
	printf("Enter the node data that needs to be modified.");
	scanf("%d", &inf);
	//key= mysearch(st, inf);
	while((p->info !=inf)||(p!=NULL))
		p=p->next;
	key=p;
	if(key!=NULL)
		{
		printf("Enter new data");
		scanf("%d", &newdata);
		key->info = newdata;
		}
	}


node* mysearch (node* sta1, int key_data)
	{
	node* p = (node*)sta1;
	while((p->info !=key_data)||(p!=NULL))
		p=p->next;
	return p;
	}


void display (node* st)
	{
	node* p = (node*)st;
	while(p!=NULL)
		{
		printf("  %d  ", p->info);
		p = p->next;
		}
	}


void main()
{
int i, n, choice, data, search_ele;
node* p;
node* key_ele;
node* start=NULL;
node* left= (node*)start;
printf("How many nodes will there be??");
scanf("%d",&n);
if(n<0)
printf("Invalid");
if(n==0)
printf("No node will be inserted");
else
{
for(i=0;i<n;i++)
	{
	p = (node*)malloc(sizeof(node));
	printf("Enter data of node at Position %d.",i+1);
	scanf("%d", &data);
	p->info = data;
	p->next = NULL;
	if (start == NULL)
		{
		start=p;
		}
	else
		{
		//while(left->next!=NULL)
		//left=left->next;
		left->next=p;
		left=p;
		}
	}

while(1)
	{
	printf("\nSelect what do want to do?\n\n1. Add          new        node.\n2. Delete        a         node\n3. Modify        a         node.\n4. Search for a particular node.\n5. Display List.\n6. Exit program.");
	scanf("%d", &choice);

	switch(choice)
		{
		case 1: create(start); break;
		case 2: del(start); break;
		case 3: modify (start); break;
		case 4: printf("Enter value to be searched"); scanf("%d", &search_ele); key_ele = mysearch (start, search_ele); printf("The element is found with value %d at position %p", search_ele, key_ele); break;
		case 5: display(start); break;
		case 6: exit(0);
		default: printf("\n\nHell!! Choose an option from the choices mentioned!!\n And stop testing my program!!");
		}


	}
}
}

Open in new window


That's the code, I have written.
Avatar of phoffric
phoffric

For these more advanced problems, it is important that you use a debugger to step through your code. If you are working on Windows, then you can get visual studio editor and debugger for free.

When posting code, use the code tags by hitting the code button.
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry, Julian Hansen, but you're wrong, start is used later in the while loop, and for sure it has to be used since otherwise all nodes except the last added one would be orphaned.
@Zoppo - I was referring to line 27 of the code above - in the function create() - start is not defined within the create function. He defined it globally but then commented it out and moved it to main. In my previous post I made a typo on the line number (7 instead of 27) which I have fixed.
Sorry, then we talked about different things (I saw the wrong global, commented out start too, but on the one side I suspect this has something to do with the described problem/behavior, on the other side I think this is probably not the real used code, because it should fail to compile in line 27 due to undeclared start)

So to be honest I started searching problems in main and found those I mentioned above (with local declared start), I didn't yet really take a look at the create function because the errors I found take place before create can be called.

Now I took a look at it, I think line 27 should be p = st;, beside this I think it look ok allthough it's unneeded complicated (i.e. using a temporary pointer isn't needed to insert a new node after another one).

Best regards,

ZOPPO
No problem - my initial comment was really meant to address the fact that the author claims the code compiles. I was questioning that because the code cannot compile if line 27 is left in place. This means if he has code that compiles then it must be different from what he posted - which makes the process of finding issues a bit more challenging as we are potentially looking at a different code base.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Heyo! Thank u for answering people. Btw i m not a boy I'm a girl.

Now With your suggestions, i have improved the code and it runs and compiles well, and with a thanks again, I want you to quote errors in the delete function in this improvised version of the code.

That would really be helpful.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Posted the improvised code. Here the problem is with delete function now. It runs only till
found %d

Open in new window

after that it doesn't run the while loop with "l".

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>


typedef struct link
{
int info;
struct link* next;
}node;

void create  (node*);
void del (node*);
void modify (node*);
node* mysearch( node*, int );
void display (node*);

//node* start; //not global, declared as variable local to main

void create  (node* st)
	{
	int aftr,inf;
	node* afternode;
	node* n;
	node* temp;
	node* p;
	p=st;
	printf("\nHello!! So you wish to add a new node\n\nEnter the node after which the insertion to be done.");
	scanf("%d", &aftr);
	//afternode = mysearch(st,aftr);
	while((p->info!=aftr)&&(p!=NULL))
		p=p->next;
	if(p==NULL)
		printf("Element not found");
	else
		{
		afternode=p;
		printf("\nFound %d\n", aftr);
		if (afternode!=NULL)
			{
			printf("Enter the data for the new node\t");
			scanf("%d", &inf);
			n = (node*)malloc(sizeof(node));
			n->info=inf;
			temp = afternode->next;
			n->next = temp;
			afternode->next = n;
			}
	       }
	}


void del (node* st)
	{
	int inf;
	node* l=st;
	node* p=st;
	node* culprit;
	printf("Enter the node data which you wish to delete.");
	scanf("%d", &inf);
	//culprit = mysearch (st,inf);
	while((p->info !=inf)&&(p!=NULL))
		p=p->next;

		if (p!= NULL)
			{
			if(p==st)
				{
				printf("The data %d is at the starting of the list",inf);
				st=p->next;
				p->next=NULL;
				free(p);
				printf("\nDELETED %d", inf);
				display (st);
				}
			else
				{
				printf("\nFound %d", inf);
				while(l->next != culprit)
					l=l->next;
				printf("got it");
				l->next=culprit->next;
				culprit->next = NULL;
				free(culprit);
				}
		}


void modify (node* st)
	{
	int inf,newdata;
	node* key;
	node* p=st;
	printf("\nEnter the node data that needs to be modified.");
	scanf("%d", &inf);
	//key= mysearch(st, inf);
	while((p->info !=inf)&&(p!=NULL))
		p=p->next;
	key=p;
	if(key!=NULL)
		{
		printf("\nEnter new data\t");
		scanf("%d", &newdata);
		key->info = newdata;
		}
		else
		printf("\nElement to be modified i.e. %d Does Not Exist",inf);
	}


node* mysearch (node* sta1, int key_data)
	{
	node* p = (node*)sta1;
	while((p->info !=key_data)&&(p!=NULL))
		p=p->next;
	return p;
	}


void display (node* st)
	{
	node* p = (node*)st;
	while(p!=NULL)
		{
		printf("  %d  ", p->info);
		p = p->next;
		}
	}


void main()
{
int i, n, choice, data, search_ele;
node* p;
node* key_ele;
node* start=NULL;
node* left= (node*)start;
printf("How many nodes will there be??");
scanf("%d",&n);
if(n<0)
printf("Invalid");
if(n==0)
printf("No node will be inserted");
else
{
for(i=0;i<n;i++)
	{
	p = (node*)malloc(sizeof(node));
	printf("Enter data of node at Position %d.",i+1);
	scanf("%d", &data);
	p->info = data;
	p->next = NULL;
	if (start == NULL)
		{
		start=p;
		}
	else
		{
	       //	while(left->next!=NULL)
	       //	left=left->next;
		left->next=p;
		}
		left=p;
	}

while(1)
	{
	printf("\nSelect what do want to do?\n\n1. Add          new        node.\n2. Delete        a         node\n3. Modify        a         node.\n4. Search for a particular node.\n5. Display List.\n6. Exit program.");
	scanf("%d", &choice);

	switch(choice)
		{
		case 1: create(start); break;
		case 2: del(start); break;
		case 3: modify (start); break;
		case 4: printf("Enter value to be searched"); scanf("%d", &search_ele); key_ele = mysearch (start, search_ele); printf("The element is found with value %d at position %p", search_ele, key_ele); break;
		case 5: display(start); break;
		case 6: exit(0);
		default: printf("\n\nHell!! Choose an option from the choices mentioned!!\n And stop testing my program!!");
		}


	}
}
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@sarabande,
plz can u tell me more on this?

Why is necessary to interchange conditions in while loop?

I mean does it ever make a difference?
Can u plz cite an example where it does?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank u very much @Julian_Hanser. Now tell me if there's a way to give feedback to you people. It was really amazing, hearing my mistakes from you all. Really a big thanks to all.

So now I've modified the code once again and now the one and only problem is with delete function when the element to be deleted is the first one in the list i.e. the first if condition block in delete function's "if" condition.

Here it goes:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>


typedef struct link
{
int info;
struct link* next;
}node;

void create  (node*);
void del (node*);
void modify (node*);
node* mysearch( node*, int );
void display (node*);

//node* start; //not global, declared as variable local to main

void create  (node* st)
	{
	int aftr,inf;
	node* afternode;
	node* n;
	node* temp;
	node* p;
	p=st;
	printf("\nHello!! So you wish to add a new node\n\nEnter the node after which the insertion to be done.");
	scanf("%d", &aftr);
	//afternode = mysearch(st,aftr);
	while((p->info!=aftr)&&(p!=NULL))
		p=p->next;
	if(p==NULL)
		printf("Element not found");
	else
		{
		afternode=p;
		printf("\nFound %d\n", aftr);
		if (afternode!=NULL)
			{
			printf("Enter the data for the new node\t");
			scanf("%d", &inf);
			n = (node*)malloc(sizeof(node));
			n->info=inf;
			temp = afternode->next;
			n->next = temp;
			afternode->next = n;
			}
	       }
	}


void del (node* st)
	{
	int inf;
	node* l=st;
	node* p=st;
	node* culprit;
	printf("Enter the node data which you wish to delete.");
	scanf("%d", &inf);
	//culprit = mysearch (st,inf);
	while((p!=NULL)&&(p->info !=inf))
		{
		l=p;
		p=p->next;
		}

		if (p!= NULL)
			{
			culprit=p;
			if(culprit==st)
				{
				printf("The data %d is at the starting of the list",inf);
				st=culprit->next;
				culprit->next=NULL;
				free(culprit);
				printf("\nSuccesfully DELETED the entry %d. Here's the modified list:\n", inf);
				display (st);
				}
			else
				{
				printf("\nFound %d", inf);
				l->next=culprit->next;
				culprit->next = NULL;
				free(culprit);
				printf("Succesfully DELETED the entry %d. Here's the modified list:\n",inf);
				display(st);
				}
			}
		  else
			printf("\nThe list is empty!");
	}

void modify (node* st)
	{
	int inf,newdata;
	node* key;
	node* p=st;
	printf("\nEnter the node data that needs to be modified.");
	scanf("%d", &inf);
	//key= mysearch(st, inf);
	while((p->info !=inf)&&(p!=NULL))
		p=p->next;
	key=p;
	if(key!=NULL)
		{
		printf("\nEnter new data\t");
		scanf("%d", &newdata);
		key->info = newdata;
		}
		else
		printf("\nElement to be modified i.e. %d Does Not Exist",inf);
	}


node* mysearch (node* sta1, int key_data)
	{
	node* p = (node*)sta1;
	while((p->info !=key_data)&&(p!=NULL))
		p=p->next;
	return p;
	}


void display (node* st)
	{
	node* p = (node*)st;
	while(p!=NULL)
		{
		printf("  %d  ", p->info);
		p = p->next;
		}
	}


void main()
{
int i, n, choice, data, search_ele;
node* p;
node* key_ele;
node* start=NULL;
node* left= (node*)start;
printf("How many nodes will there be??");
scanf("%d",&n);
if(n<0)
printf("Invalid");
if(n==0)
printf("No node will be inserted");
else
{
for(i=0;i<n;i++)
	{
	p = (node*)malloc(sizeof(node));
	printf("Enter data of node at Position %d.",i+1);
	scanf("%d", &data);
	p->info = data;
	p->next = NULL;
	if (start == NULL)
		{
		start=p;
		}
	else
		{
	       //	while(left->next!=NULL)
	       //	left=left->next;
		left->next=p;
		}
		left=p;
	}

while(1)
	{
	printf("\n\n\n1. Add          new        node.\n2. Delete        a         node\n3. Modify        a         node.\n4. Search for a particular node.\n5. Display List.\n6. Exit program.\n\nEnter your choice:\t");
	scanf("%d", &choice);

	switch(choice)
		{
		case 1: create(start); break;
		case 2: del(start); break;
		case 3: modify (start); break;
		case 4: printf("Enter value to be searched"); scanf("%d", &search_ele); key_ele = mysearch (start, search_ele); if (key_ele==NULL) printf("\n Could not find %d",search_ele); else printf("\nThe element is found with value %d with pointer %p in RAM.", search_ele, key_ele); break;
		case 5: display(start); break;
		case 6: exit(0);
		default: printf("\n\nHell!! Choose an option from the choices mentioned!!\n And stop testing my program!!");
		}


	}
}
}

Open in new window

Take a look at line 74-75
You are setting
st=culprit->next;
culprit->next=NULL;

Open in new window

Do you see it?
After line 74 what is st pointing to.
What are you doing to that same memory location in the very next line? Wiping it out.
It is unnecessary to clear the ->next of the element you are going to delete - in this case while trying to clean up you are actually destroying live data.
Just remove line 75.
But I'm doing this after assigning the value to "st", I am deleting something in cuprit->next. Why will it be deleted from st?
st has already been assigned the previous value of the pointer, and will keep pointing to it.
Next problem.

You are passing the start parameter to your del function by value - that means when you set
st=culprit->next;

You are changing the local copy of st not the original.

You need to pass the address of start to the del function and then use *st in the del function to access start.
Why will it be deleted from st?
You are correct - it won't be affected.
However, the setting of culprit->next is still redundant given you are destroying it in the next line.

The reason the delete is not working is because of the pass by value issue described above.
In main()
		case 2: del(&start); break;

Open in new window

del()
void del (node** st)
	{
	int inf;
	node* l=*st;
	node* p=*st;
	node* culprit;
	printf("Enter the node data which you wish to delete.");
	scanf("%d", &inf);
	//culprit = mysearch (st,inf);
	while((p!=NULL)&&(p->info !=inf))
		{
		l=p;
		p=p->next;
		}

		if (p!= NULL)
			{
			culprit=p;
			if(culprit==*st)
				{
				printf("The data %d is at the starting of the list",inf);
				*st=culprit->next;
				free(culprit);
				printf("\nSuccesfully DELETED the entry %d. Here's the modified list:\n", inf);
				display (*st);
				}
			else
				{
				printf("\nFound %d", inf);
				l->next=culprit->next;
				culprit->next = NULL;
				free(culprit);
				printf("Succesfully DELETED the entry %d. Here's the modified list:\n",inf);
				display(*st);
				}
			}
		  else
			printf("\nThe list is empty!");
	}

Open in new window

But I have passed "*st" in the function....that means pointer value of start is what I've passed. So why won't the original start change?
And if I'm wrong, how do I pass the address of the start function?

Another question:
and why would the value of st be changed, when it has already been assigned to culprit->next
I am making changes to culprit->next, not to st.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank u all !!!Thanks a lot.... a lot.....!! :) :) :)Now I have succefully made my program :)Thanks all....reallly a big thanks :)