C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 19th, 2012, 07:07 AM
Avichal Avichal is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 19 Avichal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 51 m 41 sec
Reputation Power: 0
Linked list -

I just made a c program to insert, delete and print nodes of a linked list. I have made three functions - one for inserting, second for deleting and third for printing.

But I am having a problem - when I declare the head(the starting node) as global it works fine. But when I declare it inside main and pass it into the functions it doesn't wok. Is it necessary to keep head as global?

Reply With Quote
  #2  
Old December 19th, 2012, 07:22 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
> Is it necessary to keep head as global?
No it isn't.

You need to post code if you want us to see exactly where you went wrong.

Note, you need to do one of these things.
Code:
node *head = NULL;  // empty list

head = insert( head, value );
// or
insert( &head, value );


Inserting into an empty list modifies the head, so you need some way of updating the variable in main, either by assigning a return result, or by passing in a pointer to the head.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old December 19th, 2012, 07:50 AM
Avichal Avichal is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 19 Avichal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 51 m 41 sec
Reputation Power: 0
Well I'll post the main function and the insert function as otherwise it will get messy
Code:
typedef struct node
{
	int data;
	struct node *next;
}node;

void insert(node* head, int num)
{
	node *ptr1, *ptr2, *ptr3;
	ptr1 = malloc(sizeof(node));
	ptr1->data = num;
	ptr1->next = NULL;
	if(!head)
		head = ptr1;
	else
	{
		ptr2 = head;
		if(ptr2->data >= num)
		{
			ptr1->next = ptr2;
			head = ptr1;
		}
		else
		{
			while(ptr2 && (ptr2->data)<num)
			{
				ptr3 = ptr2;
				ptr2 = ptr2->next;
			}
			ptr3->next = ptr1;
			ptr1->next = ptr2;
		}
	}
}

int main()
{
	node *head;
	char cmd[2];
	int num;
	while(1)
	{
		scanf("%s", cmd);
		if(cmd[0]=='i')
		{
			scanf("%d", &num);
			insert(head, num);
		}
		else if(cmd[0]=='d')
		{
			scanf("%d", &num);
			delete(head, num);
		}
		else if(cmd[0]=='p')
			print(head);
		else if(cmd[0]=='q')
			break;
	}
	return 0;
}


I am taking commands(cmd)
If its 'i' I insert a number, if its 'd' i delete a number, if its 'p' I print the list while if its q i break.

Reply With Quote
  #4  
Old December 19th, 2012, 09:41 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
Like I said, your insert needs to be able to return a modified head pointer back to main.

This, you are not doing at the moment.

See my two example methods for achieving this, and pick one.

Reply With Quote
  #5  
Old December 19th, 2012, 09:56 AM
Avichal Avichal is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 19 Avichal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 51 m 41 sec
Reputation Power: 0
Well thanks, it works now ... but I don't understand -
Why didn't it work before?
When you pass an arrray(pointer to an array), changes made to the content of the array is reflected in the main function too right? Here too I passed a pointer so changes should reflect in the main function
So why didn't it work in my code?

P.S. I didn't really expect anyone to see my code and help. Thanks a lot. This site is great for programmers!

Reply With Quote
  #6  
Old December 19th, 2012, 10:33 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
> changes made to the content of the array is reflected in the main function too right?
True, but you're not changing the content, you're changing the pointer.

Consider
Code:
void foo ( int a ) {
  a = 1;
}
int main ( ) {
  int b = 0;
  foo(b);  // do you expect b to change here?
}


What about now?
Code:
void foo ( int *a ) {
  *a = 1;
}
int main ( ) {
  int b = 0;
  foo(&b);  // do you expect b to change here?
}


Now replace "int" with "node*" and try your code.

Remember, you're trying to modify where a pointer points to, not change what it points at (which you're also doing, but that isn't the only thing going on).

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Linked list -

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap