Skip to content

Instantly share code, notes, and snippets.

@Stealthii
Created July 23, 2013 14:13
Show Gist options
  • Save Stealthii/6062667 to your computer and use it in GitHub Desktop.
Save Stealthii/6062667 to your computer and use it in GitHub Desktop.
Braun's linked list example from Capital Markets Academy
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSTRINGLEN 20
typedef struct node {
int id;
char * word;
void (*wordfunction)(char *);
node_t * next;
} node_t;
node_t * allocate_node ( node_t *);
void printword(char *);
void dump_list (node_t *);
void delete_list (node_t *);
void main(void) {
node_t * CURRENTNODE=NULL;
node_t * HEAD=NULL;
int i=0;
int id_array[11]={0,1,2,3,4,5,6,7,8,9,10};
char *name[11] = {"fee","fi","fo","fum","I","smell","the","blood","of","an","Englishman"};
for (i=0;i<11;i++) {
if (HEAD == NULL) {
CURRENTNODE = allocate_node(HEAD);
HEAD = CURRENTNODE;
} else {
CURRENTNODE = allocate_node(CURRENTNODE);
}
CURRENTNODE->id = id_array[i];
strncpy(CURRENTNODE->word,name[i],strlen(name[i]));
CURRENTNODE->wordfunction = &printword;
}
dump_list(HEAD);
delete_list(HEAD);
}
node_t * allocate_node(node_t * current) {
node_t * newnode;
newnode = (node_t *) malloc(sizeof(node_t));
newnode->word = (char *) malloc(MAXSTRINGLEN);
if (current == NULL) {
newnode->next = NULL;
} else {
current->next = newnode;
}
return(newnode);
}
void dump_list(node_t * snode) {
if (snode->next == NULL) {
printf("id = %d\n",snode->id);
snode->wordfunction(snode->word);
return;
} else {
dump_list (snode->next);
printf("id = %d\n",snode->id);
snode->wordfunction(snode->word);
}
}
void delete_list (struct node * start) {
struct node * sptr;
if (start == NULL) {
return;
}
do {
free(start->word);
sptr = start->next;
free(start);
start = sptr;
} while (start != NULL);
}
void printword(char * s) {
printf("The word is %s\n",s);
}
/* Program that shows how to take data from a file and put it into a structure */
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 1024
// Define the personnel record
typedef struct personnel_record {
int id;
char * fname;
char * lname;
float salary;
}precord;
// Note that these two variables are global since they are declared outside of main
int p_record_index=0;
precord parray[255];
int main(void) {
FILE * fp;
char buffer[MAXSIZE];
// Read the data in and call fill_p_array to insert each buffer
// string into the personnel record struct
fp = fopen("mydatafile","r");
if (fp == NULL) {
perror("File open error");
return(1);
}
while ((fgets(buffer,MAXSIZE,fp)) != NULL) {
if (fill_p_array(buffer) == -1) {
return(1);
}
}
// Print it out and free any malloc'd memory
print_p_array();
cleanup();
}
int print_p_array(void) {
int i;
for (i=0;i<p_record_index;i++) {
printf("%d %s %s %f\n",parray[i].id,parray[i].fname,parray[i].lname, parray[i].salary);
}
}
int fill_p_array(char * bufstring) {
// We're using malloc to allocate memory for the first name and last name rather than using
// static array declarations, just as a different way of doing things
parray[p_record_index].fname = (char *) malloc(MAXSIZE);
if ((parray[p_record_index].fname) == NULL) {
perror("Malloc failure!");
return(-1);
}
parray[p_record_index].lname = (char *) malloc(MAXSIZE);
if ((parray[p_record_index].lname) == NULL) {
perror("Malloc failure!");
return(-1);
}
// Stick the contents of the data buffer into the structure. Note that we're using an array of structures, so we have to tell the compiler
// which element of the structure array we want. We're using p_record_index to point to the (latest) last defined element of the array.
sscanf(bufstring,"%d %s %s %f",&(parray[p_record_index].id), (parray[p_record_index]).fname,(parray[p_record_index]).lname,&(parray[p_record_index].salary));
p_record_index++;
}
int cleanup(void){
int i;
// Always free your malloc's memory!
for (i=0;i<p_record_index;i++) {
free(parray[p_record_index].fname);
free(parray[p_record_index].lname);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment