473,396 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how the *q value is being changed in the else statement

Siddarth777
here am sending u the code of the simple linked list implementation in C language
when we go to the append function definition,in the if statement a new node is being created if address of the structure is NULL
then after the creation the address is being assigned to the temp.
there is nothing weird in that

the weird part comes in else statement if you find a node
already created you goto the last node and then you append it by adding a new node

my doubt is that you are not returning the address of the new node to the main function i.e you are not assigning to *p then how the address of the *p is getting updated?
please help........
thanks a lot

Expand|Select|Wrap|Line Numbers
  1. #include "alloc.h"
  2. struct node
  3. {
  4. int x;
  5. int y;
  6. struct node *link;
  7. };
  8. main()
  9. {
  10. struct node *p;
  11. p=NULL;
  12. append(&p,5,74);
  13. display(p);
  14. printf("THE NO OF ELEMENTS IN THE LINKED LIST IS:%d",count(p));
  15. getch();
  16. }
  17. append(struct node **q,int num1,int num2)
  18. {
  19. struct node *temp;
  20. temp=*q;
  21. if(*q==NULL)
  22. {
  23. *q=malloc(sizeof(struct node));
  24. temp=*q;
  25. }
  26. else
  27. {
  28. while(temp->link!=NULL)
  29. temp=temp->link;
  30. temp->link=malloc(sizeof(struct node));
  31. temp=temp->link;
  32. }
  33. temp->x=num1;
  34. temp->y=num2;
  35. temp->link=NULL;
  36. }
  37.  
Sep 29 '10 #1
3 1570
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. append(struct node **q,int num1,int num2) 
  2. struct node *temp; 
  3. temp=*q;
  4. etc... 
You call this function with &p as the argument node**q.

Therefore, *q is p.
Therefore, temp is p.

Anywhere you update *q or temp you are changing the node pointed at by p.
Sep 30 '10 #2
donbock
2,426 Expert 2GB
Where did this code come from?

The first argument to append() is a pointer to the list (although this is accomplished via a pointer to the first node in the list). It is not intended to be a pointer to the newly added node. That's why *p is altered only if the list was initially empty.

I have some suggestions:
  • Function append() should protect itself from argument q being NULL. In general, you should confirm that a pointer is not NULL before you dereference it.
  • Each call to malloc should be immediately followed by code that traps the case where malloc fails (returns NULL).
Sep 30 '10 #3
hey everyone thanks a lot for your reply:)
Sep 30 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

18
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
9
by: MR | last post by:
Why does the string value assigned to the RequestElementName in the SoapDocumentMethodAttribute get changed? The value below with a colon is changed to the hex value surrounded by underscores....
2
by: paul | last post by:
Hi all, I've been handed some code and, unless I've got the numbering of parentheses wrong, one of the functions has a curious if-else statement. The thing compiles but is it right? I know the...
3
by: Bundy | last post by:
Hi How do I change the background colour of an input box in a form as soon as the value is changed? Also the background should revert back to it's original colour if the user decides that they...
2
by: juan-manuel.behrendt | last post by:
Hello together, I wrote a script for the engineering software abaqus/CAE. It worked well until I implemented a selection in order to variate the variable "lGwU" through an if elif, else...
5
by: clear1140 | last post by:
Good day!! I was wondering if it is possible to put an if else statement inside a where clause of an sql statement... you see i would like my update statement to do this: update...
2
by: MicaK | last post by:
Good Morning, I am new to this forum, and extremely new to VBA, so there may be a very simple explanation to this. I also apologize if I am giving you and excessive amount of detail. I have a...
4
by: maveri4201 | last post by:
I have written a php script (test3.php), which I attached as a text file. Its includes are also attached as text files. I'm trying to run the script here: http://www.wondergy.com/phptestbed/test3.php...
6
by: DanicaDear | last post by:
I have this update strSQL that is working great, but I'd like to make it smarter in hopes of preventing input errors. Here is my working code: Else Me.txtScan_Box_Num =...
6
Frinavale
by: Frinavale | last post by:
I have a problem and I don't know why I'm having such a hard time solving it. The problem comes from a VB6 COM object that raises an event periodically. The call back method for the event (the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.