Home » Archimedes archive » Acorn User » AU 1995-09.adf » !Regulars » Regulars/C/c/LinkList
Regulars/C/c/LinkList
This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.
Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.
Tape/disk: | Home » Archimedes archive » Acorn User » AU 1995-09.adf » !Regulars |
Filename: | Regulars/C/c/LinkList |
Read OK: | ✔ |
File size: | 0607 bytes |
Load address: | 0000 |
Exec address: | 0000 |
File contents
/* C for Yourself Acorn User September 1995 An example of structures in linked lists by Steve Mumford */ #include <stdio.h> #include <stdlib.h> /* Global variables */ struct list { int num1; int num2; struct list *next; }; struct list *root; struct list *current; int main(void) { int temp1, temp2, count; struct list *spare; root = malloc(sizeof(struct list)); root->next = NULL; current = root; printf("Please enter a pair of integers\n"); printf("(enter two zeros to finish)\n\n"); count = 1; do { printf("Please enter the first number for pair %d:\n", count); scanf("%d", &temp1); printf("Please enter the second number for pair %d:\n", count); scanf("%d", &temp2); if (temp1 != 0 || temp2 != 0) { current->num1 = temp1; current->num2 = temp2; current->next = malloc(sizeof(struct list)); current = current->next; current->next = NULL; count++; } } while(temp1 != 0 || temp2 !=0); printf("Your list of numbers was as follows:\n"); current = root; count = 1; do { printf("Pair %d) %d, %d\n", count, current->num1, current->num2); if (current->next != NULL) { count++; current = current->next; } } while (current->next != NULL); printf("Freeing memory:\n"); current = root->next; free(root); do { spare = current->next; free(current); current = spare; } while (current != NULL); printf("Successful.\n"); }
00000000 2f 2a 20 43 20 66 6f 72 20 59 6f 75 72 73 65 6c |/* C for Yoursel| 00000010 66 20 41 63 6f 72 6e 20 55 73 65 72 20 53 65 70 |f Acorn User Sep| 00000020 74 65 6d 62 65 72 20 31 39 39 35 0a 20 20 20 41 |tember 1995. A| 00000030 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20 73 74 72 |n example of str| 00000040 75 63 74 75 72 65 73 20 69 6e 20 6c 69 6e 6b 65 |uctures in linke| 00000050 64 20 6c 69 73 74 73 0a 20 20 20 62 79 20 53 74 |d lists. by St| 00000060 65 76 65 20 4d 75 6d 66 6f 72 64 20 2a 2f 0a 0a |eve Mumford */..| 00000070 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e |#include <stdio.| 00000080 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 |h>.#include <std| 00000090 6c 69 62 2e 68 3e 0a 0a 2f 2a 20 47 6c 6f 62 61 |lib.h>../* Globa| 000000a0 6c 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 0a |l variables */..| 000000b0 73 74 72 75 63 74 20 6c 69 73 74 20 7b 0a 20 20 |struct list {. | 000000c0 20 69 6e 74 20 6e 75 6d 31 3b 0a 20 20 20 69 6e | int num1;. in| 000000d0 74 20 6e 75 6d 32 3b 0a 20 20 20 73 74 72 75 63 |t num2;. struc| 000000e0 74 20 6c 69 73 74 20 2a 6e 65 78 74 3b 0a 20 20 |t list *next;. | 000000f0 20 7d 3b 0a 0a 73 74 72 75 63 74 20 6c 69 73 74 | };..struct list| 00000100 20 2a 72 6f 6f 74 3b 0a 73 74 72 75 63 74 20 6c | *root;.struct l| 00000110 69 73 74 20 2a 63 75 72 72 65 6e 74 3b 0a 0a 69 |ist *current;..i| 00000120 6e 74 0a 6d 61 69 6e 28 76 6f 69 64 29 0a 7b 0a |nt.main(void).{.| 00000130 20 20 20 69 6e 74 20 74 65 6d 70 31 2c 20 74 65 | int temp1, te| 00000140 6d 70 32 2c 20 63 6f 75 6e 74 3b 0a 20 20 20 73 |mp2, count;. s| 00000150 74 72 75 63 74 20 6c 69 73 74 20 2a 73 70 61 72 |truct list *spar| 00000160 65 3b 0a 0a 20 20 20 72 6f 6f 74 20 3d 20 6d 61 |e;.. root = ma| 00000170 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 73 74 72 75 |lloc(sizeof(stru| 00000180 63 74 20 6c 69 73 74 29 29 3b 0a 20 20 20 72 6f |ct list));. ro| 00000190 6f 74 2d 3e 6e 65 78 74 20 3d 20 4e 55 4c 4c 3b |ot->next = NULL;| 000001a0 0a 20 20 20 63 75 72 72 65 6e 74 20 3d 20 72 6f |. current = ro| 000001b0 6f 74 3b 0a 20 20 20 70 72 69 6e 74 66 28 22 50 |ot;. printf("P| 000001c0 6c 65 61 73 65 20 65 6e 74 65 72 20 61 20 70 61 |lease enter a pa| 000001d0 69 72 20 6f 66 20 69 6e 74 65 67 65 72 73 5c 6e |ir of integers\n| 000001e0 22 29 3b 0a 20 20 20 70 72 69 6e 74 66 28 22 28 |");. printf("(| 000001f0 65 6e 74 65 72 20 74 77 6f 20 7a 65 72 6f 73 20 |enter two zeros | 00000200 74 6f 20 66 69 6e 69 73 68 29 5c 6e 5c 6e 22 29 |to finish)\n\n")| 00000210 3b 0a 20 20 20 63 6f 75 6e 74 20 3d 20 31 3b 0a |;. count = 1;.| 00000220 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 70 72 | do {. pr| 00000230 69 6e 74 66 28 22 50 6c 65 61 73 65 20 65 6e 74 |intf("Please ent| 00000240 65 72 20 74 68 65 20 66 69 72 73 74 20 6e 75 6d |er the first num| 00000250 62 65 72 20 66 6f 72 20 70 61 69 72 20 25 64 3a |ber for pair %d:| 00000260 5c 6e 22 2c 20 63 6f 75 6e 74 29 3b 0a 20 20 20 |\n", count);. | 00000270 20 20 20 73 63 61 6e 66 28 22 25 64 22 2c 20 26 | scanf("%d", &| 00000280 74 65 6d 70 31 29 3b 0a 20 20 20 20 20 20 70 72 |temp1);. pr| 00000290 69 6e 74 66 28 22 50 6c 65 61 73 65 20 65 6e 74 |intf("Please ent| 000002a0 65 72 20 74 68 65 20 73 65 63 6f 6e 64 20 6e 75 |er the second nu| 000002b0 6d 62 65 72 20 66 6f 72 20 70 61 69 72 20 25 64 |mber for pair %d| 000002c0 3a 5c 6e 22 2c 20 63 6f 75 6e 74 29 3b 0a 20 20 |:\n", count);. | 000002d0 20 20 20 20 73 63 61 6e 66 28 22 25 64 22 2c 20 | scanf("%d", | 000002e0 26 74 65 6d 70 32 29 3b 0a 20 20 20 20 20 20 69 |&temp2);. i| 000002f0 66 20 28 74 65 6d 70 31 20 21 3d 20 30 20 7c 7c |f (temp1 != 0 ||| 00000300 20 74 65 6d 70 32 20 21 3d 20 30 29 20 7b 0a 20 | temp2 != 0) {. | 00000310 20 20 20 20 20 20 20 20 63 75 72 72 65 6e 74 2d | current-| 00000320 3e 6e 75 6d 31 20 3d 20 74 65 6d 70 31 3b 0a 20 |>num1 = temp1;. | 00000330 20 20 20 20 20 20 20 20 63 75 72 72 65 6e 74 2d | current-| 00000340 3e 6e 75 6d 32 20 3d 20 74 65 6d 70 32 3b 0a 20 |>num2 = temp2;. | 00000350 20 20 20 20 20 20 20 20 63 75 72 72 65 6e 74 2d | current-| 00000360 3e 6e 65 78 74 20 3d 20 6d 61 6c 6c 6f 63 28 73 |>next = malloc(s| 00000370 69 7a 65 6f 66 28 73 74 72 75 63 74 20 6c 69 73 |izeof(struct lis| 00000380 74 29 29 3b 0a 20 20 20 20 20 20 20 20 20 63 75 |t));. cu| 00000390 72 72 65 6e 74 20 3d 20 63 75 72 72 65 6e 74 2d |rrent = current-| 000003a0 3e 6e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 20 |>next;. | 000003b0 63 75 72 72 65 6e 74 2d 3e 6e 65 78 74 20 3d 20 |current->next = | 000003c0 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 63 |NULL;. c| 000003d0 6f 75 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a |ount++;. }.| 000003e0 20 20 20 7d 20 77 68 69 6c 65 28 74 65 6d 70 31 | } while(temp1| 000003f0 20 21 3d 20 30 20 7c 7c 20 74 65 6d 70 32 20 21 | != 0 || temp2 !| 00000400 3d 30 29 3b 0a 0a 20 20 20 70 72 69 6e 74 66 28 |=0);.. printf(| 00000410 22 59 6f 75 72 20 6c 69 73 74 20 6f 66 20 6e 75 |"Your list of nu| 00000420 6d 62 65 72 73 20 77 61 73 20 61 73 20 66 6f 6c |mbers was as fol| 00000430 6c 6f 77 73 3a 5c 6e 22 29 3b 0a 0a 20 20 20 63 |lows:\n");.. c| 00000440 75 72 72 65 6e 74 20 3d 20 72 6f 6f 74 3b 0a 20 |urrent = root;. | 00000450 20 20 63 6f 75 6e 74 20 3d 20 31 3b 0a 20 20 20 | count = 1;. | 00000460 64 6f 20 7b 0a 20 20 20 20 20 20 70 72 69 6e 74 |do {. print| 00000470 66 28 22 50 61 69 72 20 25 64 29 20 25 64 2c 20 |f("Pair %d) %d, | 00000480 25 64 5c 6e 22 2c 20 63 6f 75 6e 74 2c 20 63 75 |%d\n", count, cu| 00000490 72 72 65 6e 74 2d 3e 6e 75 6d 31 2c 20 63 75 72 |rrent->num1, cur| 000004a0 72 65 6e 74 2d 3e 6e 75 6d 32 29 3b 0a 20 20 20 |rent->num2);. | 000004b0 20 20 20 69 66 20 28 63 75 72 72 65 6e 74 2d 3e | if (current->| 000004c0 6e 65 78 74 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a |next != NULL) {.| 000004d0 20 20 20 20 20 20 20 20 20 63 6f 75 6e 74 2b 2b | count++| 000004e0 3b 0a 20 20 20 20 20 20 20 20 20 63 75 72 72 65 |;. curre| 000004f0 6e 74 20 3d 20 63 75 72 72 65 6e 74 2d 3e 6e 65 |nt = current->ne| 00000500 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 7d |xt;. }. }| 00000510 20 77 68 69 6c 65 20 28 63 75 72 72 65 6e 74 2d | while (current-| 00000520 3e 6e 65 78 74 20 21 3d 20 4e 55 4c 4c 29 3b 0a |>next != NULL);.| 00000530 0a 20 20 20 70 72 69 6e 74 66 28 22 46 72 65 65 |. printf("Free| 00000540 69 6e 67 20 6d 65 6d 6f 72 79 3a 5c 6e 22 29 3b |ing memory:\n");| 00000550 0a 0a 20 20 20 63 75 72 72 65 6e 74 20 3d 20 72 |.. current = r| 00000560 6f 6f 74 2d 3e 6e 65 78 74 3b 0a 20 20 20 66 72 |oot->next;. fr| 00000570 65 65 28 72 6f 6f 74 29 3b 0a 20 20 20 64 6f 20 |ee(root);. do | 00000580 7b 0a 20 20 20 20 20 20 73 70 61 72 65 20 3d 20 |{. spare = | 00000590 63 75 72 72 65 6e 74 2d 3e 6e 65 78 74 3b 0a 20 |current->next;. | 000005a0 20 20 20 20 20 66 72 65 65 28 63 75 72 72 65 6e | free(curren| 000005b0 74 29 3b 0a 20 20 20 20 20 20 63 75 72 72 65 6e |t);. curren| 000005c0 74 20 3d 20 73 70 61 72 65 3b 0a 20 20 20 7d 20 |t = spare;. } | 000005d0 77 68 69 6c 65 20 28 63 75 72 72 65 6e 74 20 21 |while (current !| 000005e0 3d 20 4e 55 4c 4c 29 3b 0a 20 20 20 70 72 69 6e |= NULL);. prin| 000005f0 74 66 28 22 53 75 63 63 65 73 73 66 75 6c 2e 5c |tf("Successful.\| 00000600 6e 22 29 3b 0a 7d 0a |n");.}.| 00000607