All pastes #223020 Raw Edit

Something

public c v1 · immutable
#223020 ·published 2006-10-26 18:15 UTC
rendered paste body
/////////////////////////////////MAIN FILE#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdio.h>#include <stdlib.h>#include <time.h>#include "list.h"void input_table(int*,int,int);int main(int argc, char *argv[]){	int cnt1,cnt2,a[3][4],*b;	input_table((int*)a,3,4); /*filling in the Table */	list *mylist = ttol(a,3,4); /*Converting the table to list */	b = ltot(mylist); /*Converting the List to "Table" */	/*b is a pointer and its hard to "use" it like 2 dimentions array,	  we ll use it this way: b[X*linesize + offset] */	/*SHOW TABLE1	for (cnt1 = 0; cnt1 < 3; cnt1++){		for (cnt2 = 0; cnt2 < 4; cnt2++)			printf ("%d\t",a[cnt1][cnt2]);		printf("\n");	}	printf("\n\n");	******************/	/*SHOW LIST	node *tmpnode;	for (tmpnode = mylist->head; tmpnode; tmpnode = tmpnode->next)		printf("%d\t%d\t%d\n",tmpnode->coors[0],tmpnode->coors[1],tmpnode->val);	printf("\n\n");	******************/	/*SHOW TABLE2 (made from the list)	for (cnt1 = 0; cnt1 < 3; cnt1++){		for (cnt2 = 0; cnt2 < 4; cnt2++)			printf ("%d\t",b[cnt1*4 + cnt2]);		printf("\n");	}	******************/	return EXIT_SUCCESS; /*Finished */}void input_table(int *tbl, int x, int y)/*Filling Random table cells (less than half)  with random numbers */{	srand (time(NULL));	int tmp,elm = rand()%((x*y)/2);	/*Fill table with zero */	for (tmp = 0; tmp < x*y; tmp++)		tbl[tmp] = 0;	/*Filling random cells with random numbers */	for (tmp = rand()%(x*y);elm > 0;tmp = rand()%(x*y)){		if (!*(tbl+tmp)){ /*Not zero */			tbl[tmp] = rand()%100;			elm--;		}	}	return;}///////////////////////////////////////LIST.C#include <malloc.h>#include <stdlib.h>#include "list.h" /*definition of structs *//*list tools */node * list_createitem(int,int,int);void list_additem(list*,node*);/*main functions */list* ttol(int*,int,int); /*table to list */int* ltot(list*); /*list to table */node * list_createitem(int x, int y, int val){	node *newnode = (node *)malloc(sizeof(node));	newnode->coors[0] = x;	newnode->coors[1] = y;	newnode->val = val;	newnode->next = NULL;	return newnode;}void list_additem(list *mylist, node *newnode){	if (mylist->head == NULL) /*empty list */	{		mylist->head = newnode; /*Setting to Head */		mylist->tail = mylist->head;	}	else	{		mylist->tail->next = newnode; /*Adding to tail */		mylist->tail = mylist->tail->next;	}	return; /*Finished & return */}list* ttol(int *tbl, int x, int y){	int cnt1,cnt2;	node *tmpnode;	list *mylist = (list*)malloc(sizeof(list));	/*setting null to avoid problems */	mylist->head = NULL;	mylist->tail = NULL;		tmpnode = list_createitem(x,y,0); /*1st node with dimentions */	list_additem(mylist, tmpnode);	/*search table for Non-zero "cells" */	for (cnt1 = 0; cnt1 < x; cnt1++){		for (cnt2 = 0; cnt2 < y; cnt2++){			if (tbl[cnt1*y + cnt2] != 0){ /*not zero */				/*Create and add to list */				tmpnode = list_createitem(cnt1,cnt2,tbl[cnt1*y + cnt2]);				list_additem(mylist, tmpnode);			}		}	}	return mylist; /*Finished & return */}int* ltot(list *mylist){	int x,y,tmp,*tbl;	node *tmpnode;	/*The dimentions for allocation */	x = mylist->head->coors[0];	y = mylist->head->coors[1];	tbl=(int*)malloc(x*y*sizeof(int));	for (tmp = 0; tmp < x*y; tmp++)		tbl[tmp] = 0; /*Setting the table to "0" */	/*"Reading" the list and placing non zero values */	for (tmpnode = mylist->head->next; tmpnode != NULL; tmpnode = tmpnode->next)		tbl[tmpnode->coors[0]*y + tmpnode->coors[1]] = tmpnode->val;	return tbl; /*Finished & return */}/////////////////////////////////////LIST.H#ifndef LIST_H#define LIST_H/*node struct */typedef struct list_entry{       int coors[2],val; /*coordinates and value */       struct list_entry *next;}node;/*List struct */typedef struct{        node *head;        node *tail;}list;#endif