943,985 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5780
  • C RSS
Feb 1st, 2005
0

linked list problem!!!

Expand Post »
i've written a code for insertion in a linked list for all the 3 cases that is ...at the end,in the beginning & in between.....when i run this code then it runs successfully for insertion at end and in the beginning ,it is also showing no errors while compiling the program but when i run this code for insertion in between then it prints like this:
enter the size of list:5
enter list:
10
20
30
40
50
Enter 1.to insert at end
Enter 2.to insert in the beginning
Enter 3.to insert in the between
//so, when i enter here choice number 3.to insert in between
3.
enter the location after which the number has to be inserted:3
enter number to be inserted:100
then it doesn't print anything and comes back to the program...that is the screen where the source program was written.why?it's not printing anything.plz make necesaary changes in my code to make it properly run....plz help me out with my code...
  1. #include<stdio.h>
  2. #include<conio.h>
  3. struct link
  4. {
  5. int data;
  6. struct link*ptr;
  7. };
  8. typedef struct link node;
  9. void main()
  10. {
  11. int size,i,choice;
  12. char ch;
  13. node*start,*temp,*p;
  14. clrscr();
  15. do
  16. {
  17. printf("enter the size of list:");
  18. scanf("%d",&size);
  19. printf("enter list\n");
  20. start=NULL;
  21. for(i=0;i<size;i++)
  22. {
  23. if(start==NULL)
  24. {
  25. start=(node*)malloc(sizeof(node));
  26. scanf("%d",&start->data);
  27. start->ptr=NULL;
  28. }
  29. else
  30. {
  31. for(p=start;p->ptr!=NULL;p=p->ptr);
  32. temp=(node*)malloc(sizeof(node));
  33. scanf("%d",&temp->data);
  34. temp->ptr=NULL;
  35. p->ptr=temp;
  36. }
  37. }
  38. printf("Enter 1.To insert at the end\n");
  39. printf("Enter 2.To insert at the beginning\n");
  40. printf("Enter 3.To insert in between\n");
  41. scanf("%d",&choice);
  42. switch(choice)
  43. {
  44. case 1:
  45. end(start,p);
  46. break;
  47. case 2:
  48. beg(start,p);
  49. break;
  50. case 3:
  51. between(start,p,size);
  52. break;
  53. default:
  54. printf("wrong number entered\n");
  55. }
  56. printf("Enter y to continue\n");
  57. scanf("%c",&ch);
  58. }
  59. while(ch=='Y'||ch=='y');
  60. getch();
  61. }
  62. end(node*start,node*p)
  63. {
  64. node*temp;
  65. for(p=start;p->ptr!=NULL;p=p->ptr)
  66. temp=(node*)malloc(sizeof(node));
  67. printf("enter the number to be inserted\n");
  68. scanf("%d",&temp->data);
  69. temp->ptr=NULL;
  70. p->ptr=temp;
  71. for(p=start;p!=NULL;p=p->ptr)
  72. printf("%d",p->data);
  73. return(0);
  74. }
  75. beg(node*start,node*p)
  76. {
  77. node*temp;
  78. temp=(node*)malloc(sizeof(node));
  79. printf("enter the number to be inserted\n");
  80. scanf("%d",&temp->data);
  81. temp->ptr=start;
  82. start=temp;
  83. printf("new linked list is:\n");
  84. for(p=start;p!=NULL;p=p->ptr)
  85. printf("%d",p->data);
  86. return(0);
  87. }
  88. between(node*start,node*p,int size)
  89. {
  90. node*temp;
  91. int loc,count=0;
  92. printf("enter the location after which the number has to be inserted\n");
  93. scanf("%d",&loc);
  94. if(loc<=size)
  95. {
  96. temp=(node*)malloc(sizeof(node));
  97. printf("enter the number to be inserted\n");
  98. scanf("%d",&temp->data);
  99. for(p=start;p!=NULL;p=p->ptr)
  100. {
  101. count++;
  102. if(count==loc)
  103. {
  104. temp->ptr=p->ptr;
  105. p->ptr=temp;
  106. printf("new linked list is:");
  107. for(p=start;p!=NULL;p=p->ptr)
  108. printf("%d",p->data);
  109. return(0);
  110. }
  111. else
  112. {
  113. printf("location is not found\n");
  114. return(0);
  115. }
  116. }
  117. }
  118. return(0);
  119. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
galmca is offline Offline
47 posts
since Oct 2004
Feb 1st, 2005
1

Re: linked list problem!!!

It's been at least three months since I last looked at one of your posts, and you've learned nothing. Maybe if you actually tried to write good code, it would be easier to find your many mistakes.

>plz make necesaary changes in my code to make it properly run
This is incredibly rude. You're assuming that we care about you and your program and that you're entitled to a complete rewrite by us. This sentence alone is why I refuse to help you.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 1st, 2005
0

Re: linked list problem!!!

i am sorry....but i didn't mean to be rude at all....i said please if u could make some necessary changes that r reuired to make my program run successfuly because i know that you guys can only help me out here.....so i request you again to help me out...coz i have tried everything but i can't think of anything so that it can run properly...i have written the entire code but just can't able to run it properly.....so please help me out ....
Reputation Points: 10
Solved Threads: 0
Light Poster
galmca is offline Offline
47 posts
since Oct 2004
Feb 2nd, 2005
0

Re: linked list problem!!!

plz reply.....to my code.....its not running still..there is some mistake in the betwen section iam sure...please help me out with my code...
Reputation Points: 10
Solved Threads: 0
Light Poster
galmca is offline Offline
47 posts
since Oct 2004
Apr 2nd, 2007
0

Re: linked list problem!!!

Hello my friend.
This is exactly what you need. but it is written in C++
  1. #include<iostream>
  2. #include<conio.h>
  3. #include<cassert>
  4. using namespace std;
  5.  
  6. struct nodeType
  7. {
  8. int info;
  9. nodeType *link;
  10. };
  11.  
  12. int main()
  13. {
  14. int choice, num;
  15. nodeType *first, *last, *newNode, *current;
  16. cout<<"Enter numbers and press -999 to quit-->";
  17. cin>>num;
  18. first = 0; last = 0; current = 0; newNode = 0;
  19. while(num!=-999)
  20. {
  21. newNode=new nodeType;
  22. assert(newNode!=NULL);
  23. newNode->info=num;
  24. newNode->link=NULL;
  25. if(first==NULL)
  26. {
  27. first=newNode;
  28. last=newNode;
  29. }
  30. else
  31. {
  32. last->link=newNode;
  33. last=newNode;
  34. }
  35. cin>>num;
  36. }
  37.  
  38. cout<<"Enter 1.To insert at the end\n";
  39. cout<<"Enter 2.To insert at the beginning\n";
  40. cout<<"Enter 3.To insert in between\n";
  41. cin>>choice;
  42. if(choice==1)
  43. {
  44. cout<<"Enter number to insert at the end-->";
  45. cin>>num;
  46. newNode=new nodeType;
  47. assert(newNode!=NULL);
  48. newNode->info=num;
  49. newNode->link=NULL;
  50. if(first==NULL)
  51. {
  52. first=newNode;
  53. last=newNode;
  54. }
  55. else
  56. {
  57. last->link=newNode;
  58. last=newNode;
  59. }
  60. }
  61.  
  62. else if(choice==2)
  63. {
  64. cout<<"Enter number to insert first-->";
  65. cin>>num;
  66. newNode=new nodeType;
  67. assert(newNode!=NULL);
  68. newNode->info=num;
  69. newNode->link=first;
  70. first=newNode;
  71. }
  72. else if(choice==3)
  73. {
  74. int loc, count=1;
  75. current=first;
  76. cout<<"Enter the location after which number has to be inserted-->";
  77. cin>>loc;
  78. cout<<"Enter the number-->";
  79. cin>>num;
  80. newNode=new nodeType;
  81. assert(newNode!=NULL);
  82. newNode->info=num;
  83. newNode->link=NULL;
  84. while(current!=NULL && count<loc)
  85. {
  86. current=current->link;
  87. count++;
  88. }
  89. if(count==loc)
  90. {
  91. nodeType *temp;
  92. temp=current->link;
  93. current->link=newNode;
  94. newNode->link=temp;
  95. }
  96. }
  97. else
  98. cerr<<"Enter correct input";
  99.  
  100.  
  101. current = first;
  102. while (current!=NULL)
  103. {
  104. cout<<current->info<<" ";
  105. current = current->link;
  106. }
  107. getch();
  108. return 0;
  109. }
Last edited by ~s.o.s~; Apr 4th, 2007 at 11:03 pm. Reason: Added code tags, learn to use them.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Friday is offline Offline
2 posts
since Apr 2007
Apr 2nd, 2007
1

Re: linked list problem!!!

I'm sure it was worth waiting 2 YEARS for you to show up with an answer :rolleyes:

In the meantime, feel free to swing by the "how to use code tags" readme thread at the top of the forum on how to post code which isn't an unindented mess.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Apr 4th, 2007
0

Re: linked list problem!!!

Hey first I am not interested in this forum.
Second I come across this on the web and wanted to help him,
with that purpose I registered and wasted my time writing the code.
and "Don't teach your Grandfather how to suck an egg k?"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Friday is offline Offline
2 posts
since Apr 2007
Apr 4th, 2007
0

Re: linked list problem!!!

>Hey first I am not interested in this forum.
Then why did you post?

>Second I come across this on the web and wanted to help him, with that purpose I registered and wasted my
>time writing the code.
Yes, you certainly did waste your time, because I don't think someone is going to wait 2 years for an answer. The OP has already given up, or solved the problem. Next time look at the date before posting.

And by being so hasty to reply, you missed the forum announcements which explain things such as how to use code tags (as already mentioned by Salem), rules, and such. Believe me, it's not a good idea to try to suddenly reply to a thread without familiarizing yourself with the fourm and the community first.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to pass dynamic arrays?
Next Thread in C Forum Timeline: Simple char array question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC