Well hello everybody.i planned to develop simple spreadsheet for my homeworks,and then
i decided to use List instead of matrix to represent the spreadsheet cell.
well, code below is type declaration for my list in file matriks.h
Code:
/********** DEKLARASI matriks **********/
/* File : matriks.h */
/* deklarasi list untuk representasi sel pada matriks */
#ifndef matriks_H
#define matriks_H
#include "boolean.h"
#include <stdlib.h>
typedef struct
{
void *value;
int tipe;
}infotype;
typedef struct tElmtCell *address;
typedef struct tElmtCell
{
infotype *Info;
int row;
int column;
address Next;
}ElmtCell;
typedef struct
{
address FirstCell;
}Matriks;
/*** Selector Notation***/
#define Nil NULL
#define First(M) (M).FirstCell
#define NextCell(C) (C)->Next
#define Info(C) (C)->Info->value
#define Tipe(C) (C)->Info->tipe
#define Row(C) (C)->row
#define Column(C) (C)->column
/*** Prototype Primitif ***/
void CreateMatriks(Matriks *M);
boolean IsEmptyMatriks(Matriks M);
address Allocate(int tipe,void *value,int row,int column);
The list node contains structure named infotype which hold the value of cell and the Value type.1 for integer type,2 string for type,and 3 for storing formula.So i use void * in order to be able to store any kind of data type (integer,string,etc)
this is file matriks.c contains function body from matriks.h
Code:
void CreateMatriks(Matriks *M)
{
First(*M) = Nil;
}
boolean IsEmptyMatriks(Matriks M)
{
return (First(M) == Nil);
}
address Alllocate(int tipe,void *value,int row,int column)
{
address P;
printf("setelah alokasi \n");
P=(address) malloc(sizeof(ElmtCell));
printf("alokasi \n");
if (P!=Nil)
{
Row(P) = rows;
Column(P) = column;
Tipe(P) = tipe;
Info(P) = isi;
NextCell(P) = Nil;
}
return (P);
}
well the problem is when i want to allocate some memory space for new list node,i use
Allocate function (umm, i made this function,you can see the function body in code above).The strange part is i can only use the Allocate function just once,you can try this code below to test the allocate function
Code:
#include "matriks.h"
int main (void)
{
Matriks M1;
CreateMatriks(&M1);
address P = Allocate(1,(void *)1,1,1);
address Q = Allocate(1,(void *)2,2,1);
/*
printf("it works \n");
//InsertElement(&M1,1,1,1,1);
printf("info cell is : %d \n",Info(First(M1)));
//InsertElement(&M1,2,"rama adhitia",2,23);
printf("hehe\n");*/
scanf("%d");
return 0;
}
So when i allocate address P the allocate function works nicely,but when i allocate address Q,it didnt work.I guess this problem occured because i use void * to store value in infotype structure.
Please help me guys!!