Cursor-based implementation of list

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 60
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training

Cursor-based implementation of list

 
0
  #1
Dec 14th, 2008
This is a cursor-based implementation of list inserting at the end. In the program below, the output the program will show me is the last element of my input. It is suppose to show all elements from the first input to the last. Also, after two inputs, when inserting, it will skip everything and print "no available heap". It should not do that because the heapspace is 50.
I didnt post the view function to make the post here shorter. The problem is not in there anyway. I have tested it many times.
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. #define max 50
  5. typedef struct
  6. {
  7. int elem;
  8. int next;
  9. }heapspace[max];
  10. typedef struct
  11. {
  12. heapspace h;
  13. int avail;
  14. }virtualheap;
  15. typedef int list;
  16.  
  17. void initialize(virtualheap *vh);
  18. void insert_end(virtualheap *vh, list *l, int x);
  19.  
  20. void initialize(virtualheap *vh)
  21. {
  22. int n = 0;
  23.  
  24. vh->avail = n;
  25. for(; n < max - 1; vh->h[n].next = n++);
  26. vh->h[n].next = -1;
  27. }
  28. void insert_end(virtualheap *vh, list *l, int x)
  29. {
  30. int p, temp;
  31.  
  32. if(vh->avail != -1)
  33. {
  34. vh->h[vh->avail].elem = x;
  35. temp = vh->avail;
  36. vh->avail = vh->h[vh->avail].next;
  37. for(p = *l; p != -1; p = vh->h[p].next);
  38. vh->h[temp].next = -1;
  39. if(*l == -1)
  40. *l = temp;
  41. else
  42. vh->h[p].next = temp;
  43. }
  44. else
  45. printf("No available heap\n");
  46.  
  47. }
  48. void main(void)
  49. {
  50. virtualheap vh;
  51. list l = -1;
  52. int x;
  53. char ans;
  54.  
  55. initialize(&vh);
  56. do
  57. {
  58. printf("What do you want to put? \n");
  59. scanf("%d", &x);
  60. insert_end(&vh, &l, x);
  61.  
  62. printf("Do you want to continue? \n");
  63. ans = getch();
  64.  
  65. }while(ans == 'Y' || ans == 'y');
  66.  
  67. view(vh, l);
  68. }
Please help me. Thank you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 11
Reputation: Jawahar prabhu is an unknown quantity at this point 
Solved Threads: 1
Jawahar prabhu Jawahar prabhu is offline Offline
Newbie Poster

Re: [B]Cursor-based implementation of list[/B]

 
0
  #2
Dec 14th, 2008
hello,in your program you have called the view function but you did not write any definition for that , also i cant understood where have you get the inputs pls tell that clearly. thank you. Am here [email removed]
Last edited by Narue; Dec 14th, 2008 at 10:59 am. Reason: snipped email
Be patience
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 60
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training

Re: [B]Cursor-based implementation of list[/B]

 
0
  #3
Dec 14th, 2008
Originally Posted by Jawahar prabhu View Post
hello,in your program you have called the view function but you did not write any definition for that , also i cant understood where have you get the inputs pls tell that clearly. thank you. Am here [email removed]
I have a view function in my program, I just removed it here to make this one shorter. Im sure the problem is not in the view function.
The inputs are the numbers the user will press on the keyboard.
Last edited by Narue; Dec 14th, 2008 at 10:59 am. Reason: corrected quote after changing original
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 11
Reputation: Jawahar prabhu is an unknown quantity at this point 
Solved Threads: 1
Jawahar prabhu Jawahar prabhu is offline Offline
Newbie Poster

Re: [B]Cursor-based implementation of list[/B]

 
0
  #4
Dec 14th, 2008
ya i understand but when i was try to execute it compiler shows error about prototype for view function can you give the view function?
Be patience
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 60
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training

Re: [B]Cursor-based implementation of list[/B]

 
0
  #5
Dec 14th, 2008
ok, here it is. by the way, Im using borland c. Its not standard.

  1. void view(virtualheap vh, list l)
  2. {
  3. list n;
  4.  
  5. for(n = l; n != -1; n = vh.h[n].next)
  6. printf("%d\n", vh.h[n].elem);
  7. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 60
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training

Re: [B]Cursor-based implementation of list[/B]

 
0
  #6
Dec 14th, 2008
Problem solved! I solved it on my own! I changed somethings in the insert end function and initialize function. Here is the changed code of the two functions.
  1. void insert_end(virtualheap *vh, list *l, int x)
  2. {
  3. int n, p, temp;
  4.  
  5. if(vh->avail != -1)
  6. {
  7. vh->h[vh->avail].elem = x;
  8. temp = vh->avail;
  9. vh->avail = vh->h[vh->avail].next;
  10. vh->h[temp].next = -1;
  11. for(p = *l; p != -1 && vh->h[p].next != -1; p = vh->h[p].next);
  12. if(*l == -1)
  13. *l = temp;
  14. else
  15. vh->h[p].next = temp;
  16. }
  17. else
  18. printf("No available heap\n");
  19.  
  20.  
  21. }
  22. void initialize(virtualheap *vh)
  23. {
  24. int n = 0;
  25.  
  26. vh->avail = n;
  27. for(; n < max - 1; vh->h[n].next = ++n);
  28. vh->h[n].next = -1;
  29. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC