473,325 Members | 2,712 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,325 software developers and data experts.

Inserting a no. after some other no. in link list

Parul Bagadia
188 100+
Here is the code i hav written for inserting a no., after given no. in a link list;
i guess the logic is ofcourse right.
there is no error in it,
but at the time of display its not displaying the updated value.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct linklist
  4. {
  5.     int value;
  6.     struct linklist*next;
  7. }* first;
  8. //Inserting a number after given number
  9. void insertafter(struct linklist*first)
  10. {
  11.     int p_no;
  12.     struct linklist*hold;
  13.     struct linklist*temp;
  14.     hold=(struct linklist*)malloc(sizeof(struct linklist));
  15.     temp=(struct linklist*)malloc(sizeof(struct linklist));
  16.     printf("\nEnter a particular no. after which you want to add a no.\n");
  17.     scanf("\n%d",&p_no);
  18.  
  19.         if(first->value==p_no)
  20.         {
  21.             printf("\nEnter the desired number:\n");
  22.             scanf("\n%d",&hold->value);
  23.             hold->next=first->next;
  24.             first->next=hold;
  25.             first=first->next;
  26.         }
  27.         else
  28.         {
  29.             temp=first;
  30.             while(temp->next!=NULL &&temp->value!=p_no)
  31.             {
  32.              temp=temp->next;
  33.             }
  34.             if(temp->value!=p_no)
  35.             {
  36.               printf("\nThe number after which you want to add another no. does not exist in list.\n ");
  37.             }
  38.             else
  39.             {
  40.               printf("\nEnter the desired number:\n");
  41.               scanf("\n%d",&hold->value);
  42.               hold->next=temp->next;
  43.               temp=hold;
  44.             }
  45.         }
  46. }
  47. //Display of list
  48. void display(struct linklist*first )
  49. {
  50.     printf("\nFollowing are the elements you have added in the list till now\n");
  51.     while(first!=NULL)
  52.     {
  53.         printf("\n%d\n",first->value);
  54.         first=first->next;
  55.     }
  56. }
Mar 13 '08 #1
14 2233
whodgson
542 512MB
So what does it print out?
Mar 13 '08 #2
weaknessforcats
9,208 Expert Mod 8TB
Why is there data entry in a function that inserts into a linked list? That means you can never use this function anywhere else unless you accept the data entry, which also forces you to accept the screen layout.

What you should do is:

1) do the data entry
2) pass the data, a pointer to the linked list and the position to your insertafter function.

Then insertafter:
1) calls a funciton to create the node by passing the data to it. The function returns a pointer to the new node.
2) traverse the list to locate the insertion point
- be sure to save the next address of the previous node or you can't insert
3) set the new node next to the previous node next
4) set the previous node next to the new node

The insertafter() should have less than 10 lines of code in it.
Mar 13 '08 #3
Parul Bagadia
188 100+
Why is there data entry in a function that inserts into a linked list? That means you can never use this function anywhere else unless you accept the data entry, which also forces you to accept the screen layout.

What you should do is:

1) do the data entry
2) pass the data, a pointer to the linked list and the position to your insertafter function.

Then insertafter:
1) calls a funciton to create the node by passing the data to it. The function returns a pointer to the new node.
2) traverse the list to locate the insertion point
- be sure to save the next address of the previous node or you can't insert
3) set the new node next to the previous node next
4) set the previous node next to the new node

The insertafter() should have less than 10 lines of code in it.
That's one way of doing it; but m sure even my way is right!
I have already written a function for insertion; and not for creating a node!
what output i grt by this is my original list; and not the one i have edited.......
as in the new value ebterwed by this function
Mar 13 '08 #4
Banfa
9,065 Expert Mod 8TB
That's one way of doing it; but m sure even my way is right!
"Right" is a very moveable feast. What weaknessforcats suggests is certainly one way of writing this program and what you have done is certainly another.

However it is generally considered best practice to keep your user interface and program functionality separate with a defined interface between them. This normal makes it easier to write, test, maintain and change (if required) either the user interface or the functionality as they are not interfering with each other.

The term "best practice" normally means everyone's consensus on the best approach to take to a problem given the several decades of thought and programming successes and failures that have happened.

The structure weaknessforcats proposes very clearly splits the code into different areas of functionality with user interface and program logic separate. On the other hand your implementation has everything munged together in 1 confusing mass.
Mar 13 '08 #5
Parul Bagadia
188 100+
Thanx for that.
I have made changes in my code and its absolutely working fine now...
Here is the one for inserting a no. after given no,
Expand|Select|Wrap|Line Numbers
  1. //Inserting a number after given number.
  2. void insertafter(struct linklist*first)
  3. {
  4.     int p_no;
  5.     struct linklist*temp;
  6.     struct linklist*assign;
  7.     temp=(struct linklist*)malloc(sizeof(struct linklist));
  8.     assign=(struct linklist*)malloc(sizeof(struct linklist));
  9.     printf("\nEnter a particular no. after which you want to add a no.\n");
  10.     scanf("\n%d",&p_no);
  11.     if(p_no!=NULL && first!=NULL)
  12.     {
  13.         temp=first;
  14.         while(p_no!=temp->value && temp->next!=NULL)
  15.         {
  16.           temp=temp->next;
  17.         }
  18.         if(temp->value==p_no)
  19.         {
  20.           printf("\nEnter the no. which you actually want to add \n");
  21.           scanf("%d",&assign->value);
  22.           assign->next=temp->next;
  23.           temp->next=assign;
  24.           printf("\nThe new edited list is as follows:\n");
  25.           display(first);
  26.         }
  27.         else
  28.         {
  29.           printf("\nThe no.is not in the list.\n");
  30.         }
  31.     }
  32.     else
  33.     {
  34.         printf("Please, enter the valid input.");
  35.     }
  36. }
But now my another code for inserting a number before the given no. is not giving me the required output; anyone who's able to suggest sth;
here is the code for it; i even debugged it; and in debuggung its doing all yhe things which i expect from it;
Expand|Select|Wrap|Line Numbers
  1. //Insert a number before given number.
  2. void insertbefore(struct linklist*first)
  3. {
  4.     int take;
  5.     struct linklist*store;
  6.     struct linklist* bno;
  7.     store=(struct linklist*)malloc(sizeof(struct linklist));
  8.     bno=(struct linklist*)malloc(sizeof(struct linklist));
  9.     printf("\nEnter the no. before which you want to add a number\n");
  10.     scanf("\n %d",&take);
  11.     if(take!=NULL && first!=NULL)
  12.     {
  13.         store=first;
  14.         while(take!=store->value && store->next!=NULL)
  15.         {
  16.             store=store->next;
  17.         }
  18.         if(store->value==take)
  19.         {
  20.             printf("\nEnter the no. which you want to add\n");
  21.             scanf("%d",&bno->value);
  22.             bno->next=store;
  23.             store=bno;
  24.             printf("\nThe new edited list is as follows:\n");
  25.             display(first);
  26.         }
  27.         else
  28.         {
  29.             printf("\nThe required no. is not in the list.\n");
  30.         }
  31.     }
  32.  
  33. }
Please reply fast!
Mar 26 '08 #6
Parul Bagadia
188 100+
I need to know this a bit early,
thanx in advance.
Mar 26 '08 #7
Banfa
9,065 Expert Mod 8TB
I have made changes in my code and its absolutely working fine now...
Not by my definition of "fine", both of your functions insertafter and insertbefore leak memory, at least sizeof(struct linklist) everytime they are called sometimes twice that.

You function insertbefore is not working because in the heart of the function where you are attempting to insert the new list member
Expand|Select|Wrap|Line Numbers
  1.         if(store->value==take)
  2.         {
  3.             printf("\nEnter the no. which you want to add\n");
  4.             scanf("%d",&bno->value);
  5.             bno->next=store;
  6.             store=bno;
  7.             printf("\nThe new edited list is as follows:\n");
  8.             display(first);
  9.         }
You are only altering local data and data malloc'd in the function, at no time do you alter any of the data in the current list so the current list is unaffected.


Finally you have many posts now, please can you start using [code=c]...[/code] tags when you post code, it makes it a lot easier to read your posts.
Mar 26 '08 #8
weaknessforcats
9,208 Expert Mod 8TB
And while you work on insert before and insert after, remember that everything is really insertafter or insertbefore, whichever you choose.

Expand|Select|Wrap|Line Numbers
  1. insertafter(A,B);              //inserts B after A
  2. insertafter(B,A)              //inserts A after B << really an insert before
  3.  
You have too many functions.
Mar 26 '08 #9
Parul Bagadia
188 100+
Not by my definition of "fine", both of your functions insertafter and insertbefore leak memory, at least sizeof(struct linklist) everytime they are called sometimes twice that.

You function insertbefore is not working because in the heart of the function where you are attempting to insert the new list member
Expand|Select|Wrap|Line Numbers
  1.         if(store->value==take)
  2.         {
  3.             printf("\nEnter the no. which you want to add\n");
  4.             scanf("%d",&bno->value);
  5.             bno->next=store;
  6.             store=bno;
  7.             printf("\nThe new edited list is as follows:\n");
  8.             display(first);
  9.         }
You are only altering local data and data malloc'd in the function, at no time do you alter any of the data in the current list so the current list is unaffected.


Finally you have many posts now, please can you start using
Expand|Select|Wrap|Line Numbers
  1. ...
tags when you post code, it makes it a lot easier to read your posts.
Then how come my insertafter is working?
I have done all the same things in it; except pointers are altered in other way.
Mar 26 '08 #10
Parul Bagadia
188 100+
And while you work on insert before and insert after, remember that everything is really insertafter or insertbefore, whichever you choose.

Expand|Select|Wrap|Line Numbers
  1. insertafter(A,B);              //inserts B after A
  2. insertafter(B,A)              //inserts A after B << really an insert before
  3.  
You have too many functions.
Yaeh, that was good idea but we are not supposed to do so!
Mar 26 '08 #11
Parul Bagadia
188 100+
M still left with the problem as it is!
Mar 26 '08 #12
Banfa
9,065 Expert Mod 8TB
I have done all the same things in it; except pointers are altered in other way.
That is exactly the point the difference between insertafter and insertbefore is that in insertafter the things pointed to are altered but in insertbefore the pointer itself is altered, take a look at your own code.
Mar 26 '08 #13
weaknessforcats
9,208 Expert Mod 8TB
Yaeh, that was good idea but we are not supposed to do so!
Then use a pass-through function:
Expand|Select|Wrap|Line Numbers
  1. //Insert B before A
  2. void insertbefore(Node* A, Node* B)
  3. {
  4.      insertafter(B,A);  //insert A after B is the same as inserting B before A
  5. }
  6.  
Mar 27 '08 #14
Parul Bagadia
188 100+
Thank you so much ppl, now my code is working because of your help.
//Insert a number before given number.
void insertbefore(struct linklist*first)
{
int take;
struct linklist*track;
struct linklist*store;
struct linklist* bno;
store=(struct linklist*)malloc(sizeof(struct linklist));
bno=(struct linklist*)malloc(sizeof(struct linklist));
printf("\nEnter the no. before which you want to add a number\n");
scanf("\n %d",&take);
if(take!=NULL && first!=NULL)
{
store=first;
while(take!=store->value && store->next!=NULL)
{
store=store->next;
}
if(store->value==take)
{
track=first;
while(track->next!=store)
{
track=track->next;
}
printf("\nEnter the no. which you want to add\n");
scanf("%d",&bno->value);
bno->next=store;
track->next=bno;
printf("\nThe new edited list is as follows:\n");
display(first);
}
else
{
printf("\nThe required no. is not in the list.\n");
}
}

}
I thought, may be if somebody still wants to suggest;on how to make it better.
So, above is the working code.
Mar 30 '08 #15

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

Similar topics

2
by: Mr Nobody | last post by:
Hi all, I am having a problem inserting a hyperlink to a document into a php function. I have tried quite a few ways but can't seem to do it. Has anybody got any pointers that may help me?...
2
by: j van c | last post by:
I have a menubar on my main page. when clicking on a menu item I want to open an existing htm page at a certain position in my main page I use no frames. It should be replacing a div-section....
3
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when...
1
by: ajk | last post by:
. Hi, All: I know how to insert files into a Word doc using C#. However, the program I've written to do this runs much too slowly. The "myObj".Application.Selection.InsertFile method executes...
3
by: Patrick Olurotimi Ige | last post by:
I have a webform with DropDownLists and and i would like to have a link that would open up a window popup for the USER to insert some data into the Database and then on closing the popub window...
2
by: Charles Wilt | last post by:
I have a IBM iSeries (aka AS-400) running v5r3 of OS/400 that I access via a linked server from SQL Server 2000. The following select works fine: select * from...
6
by: DWrek | last post by:
Here is my problem. I have an XML document that is returned to me by a third party service. The XML document contains results for a search but only lists a maximum of 10 results. If there are any...
3
by: Surya | last post by:
Dear All, I have problem on inserting a record to database..Although it looked easy.. i have caught up with following issue .. please go ahead and help me to find solution I Need to insert...
5
by: dos360 | last post by:
Hello, I have two tables, one is a list of activities, the other a list of participants. I want to insert one record in the activities table and then using its identity column as foreign key, I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.