c programming 3 - if(newaddr == (struct MyList *) NULL) {...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Sheet1 Page 1 struct MyList { char value[MAXCHARS] struct MyList *nextAddr } struct MyList *firstRec int main() { void datainput() void display() int i firstRec = NULL printf("\nEnter five values from the keyboard:\n") for(i=0 i<5 i++) { printf("Value #%d:",i+1) datainput() } display() printf("\nEnter another value to add to the list: ") datainput() display() system("pause") return 0 } void datainput() { char value[MAXCHARS] void insert(char *) gets(value) insert(value) } void insert(char *value) { struct MyList *newaddr, *here newaddr = (struct MyList *) malloc(sizeof(struct MyList))
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if(newaddr == (struct MyList *) NULL) { Sheet1 Page 2 printf(&quot;\nCould not allocate the requested space\n&quot;) exit(1) } if(firstRec == NULL) { newaddr -&gt; nextAddr = NULL firstRec = newaddr } else { newaddr -&gt; nextAddr = firstRec firstRec = newaddr } strcpy(newaddr -&gt; value,value) } void display() { struct MyList *contents contents = firstRec printf(&quot;\nThe values contained in your list are: \n&quot;) while (contents != NULL) { printf(&quot;%s\n&quot;, contents -&gt; value) contents=contents -&gt; nextAddr } }...
View Full Document

This document was uploaded on 02/13/2008.

Page1 / 2

c programming 3 - if(newaddr == (struct MyList *) NULL) {...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online