Skip to content

Instantly share code, notes, and snippets.

@zigen
Created April 27, 2015 16:57
Show Gist options
  • Save zigen/679acea6532d35f37355 to your computer and use it in GitHub Desktop.
Save zigen/679acea6532d35f37355 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
struct Cell {
struct Cell *next;
int value;
} ;
struct Cell* head;
void free_all();
void fatal_error(){
printf("fatal error");
free_all(head);
}
struct Cell* new_Cell(int val)
{
struct Cell* c;
c = (struct Cell*)malloc(sizeof(struct Cell));
if(c == NULL)
{
fatal_error();
}
c->value = val;
return c;
}
void append(struct Cell* c, int val)
{
if(c->next == 0){
c->next = new_Cell(val);
return;
}
struct Cell* ptr = c->next;
while(ptr->next != 0){
ptr = ptr->next;
}
ptr->next = new_Cell(val);
}
void print_all_cell ( struct Cell* c )
{
if(c->next != NULL){
printf("addr : %x, value : %d\n",c->next, c->value);
print_all_cell(c->next);
} else {
printf("addr : %x, value : %d\n",c->next, c->value);
}
}
void free_all(struct Cell* c)
{
if(c->next == NULL){
return;
} else {
free_all(c->next);
free(c);
}
}
int main()
{
head = (struct Cell*)malloc(sizeof(struct Cell));
head->value = 3;
for(int i=0;i<100;++i){
append(head,i);
}
// printf("head points %x\n",head->next);
print_all_cell(head);
free_all(head);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment