The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Linked list -
Discuss Linked list - in the C Programming forum on Dev Shed. Linked list - C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 19th, 2012, 07:07 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 19
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?
|

December 19th, 2012, 07:22 AM
|
 |
Contributed User
|
|
|
|
> 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.
|

December 19th, 2012, 07:50 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 19
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.
|

December 19th, 2012, 09:41 AM
|
 |
Contributed User
|
|
|
|
|
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.
|

December 19th, 2012, 09:56 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 19
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!
|

December 19th, 2012, 10:33 AM
|
 |
Contributed User
|
|
|
|
> 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).
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|