| | |
Cursor-based implementation of list
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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.
Please help me. Thank you!
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.
C Syntax (Toggle Plain Text)
#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); }
•
•
•
•
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]
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
ok, here it is. by the way, Im using borland c. Its not standard.
C Syntax (Toggle Plain Text)
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.
C Syntax (Toggle Plain Text)
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; }
![]() |
Similar Threads
- JSP database connectivity according to Model View Controller (MVC) Model 2 (JSP)
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- Cannot find server or DNS Error - please help! (Viruses, Spyware and other Nasties)
- Here is the doc file for the assignment (C++)
Other Threads in the C Forum
- Previous Thread: File listing issue
- Next Thread: Printing gets me incorrecting answers
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez graphics gtkgcurlcompiling gtkwinlinux hardware highest homework i/o ide inches initialization intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives loopinsideloop. lowest match matrix microsoft motherboard mqqueue multi mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi





