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.

#include<stdio.h>
#include<conio.h>

#define max 50
typedef struct
{
	 int elem;
	 int next;
}heapspace[max];
typedef struct
{
	 heapspace h;
	 int avail;
}virtualheap;
typedef int list;

void initialize(virtualheap *vh);
void insert_end(virtualheap *vh, list *l, int x);

void initialize(virtualheap *vh)
{
	 int n = 0;

	 vh->avail = n;
	 for(; n < max - 1; vh->h[n].next = n++);
	 vh->h[n].next = -1;
}
void insert_end(virtualheap *vh, list *l, int x)
{
	 int p, temp;

	 if(vh->avail != -1)
	 {
		  vh->h[vh->avail].elem = x;
		  temp = vh->avail;
		  vh->avail = vh->h[vh->avail].next;
		  for(p = *l; p != -1; p = vh->h[p].next);
		  vh->h[temp].next = -1;
		  if(*l == -1)
				*l = temp;
		  else
				vh->h[p].next = temp;
	 }
	 else
		  printf("No available heap\n");

}
void main(void)
{
	 virtualheap vh;
	 list l = -1;
	 int x;
	 char ans;

	 initialize(&vh);
	 do
	 {
		  printf("What do you want to put? \n");
		  scanf("%d", &x);
		  insert_end(&vh, &l, x);

		  printf("Do you want to continue? \n");
		  ans = getch();

	 }while(ans == 'Y' || ans == 'y');

	 view(vh, l);
}

Please help me. Thank you!

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]

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.

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?

ok, here it is. by the way, Im using borland c. Its not standard.

void view(virtualheap vh, list l)
{
	 list n;

	 for(n = l; n != -1; n = vh.h[n].next)
		  printf("%d\n", vh.h[n].elem);
}

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.

void insert_end(virtualheap *vh, list *l, int x)
{
	 int n, p, temp;

	 if(vh->avail != -1)
	 {
		  vh->h[vh->avail].elem = x;
		  temp = vh->avail;
		  vh->avail = vh->h[vh->avail].next;
		  vh->h[temp].next = -1;
		  for(p = *l; p != -1 && vh->h[p].next != -1; p = vh->h[p].next);
		  if(*l == -1)
				*l = temp;
		  else
				vh->h[p].next = temp;
	 }
	 else
		  printf("No available heap\n");


}
void initialize(virtualheap *vh)
{
	 int n = 0;

	 vh->avail = n;
	 for(; n < max - 1; vh->h[n].next = ++n);
	 vh->h[n].next = -1;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.