#include <stdio.h>
#include <malloc.h>
#include <conio.h>
typedef struct node
{
int info;
struct node* next;
} node;
node* fi = NULL, *la = NULL, *ptr;
void insertfi()
{
ptr = (node*)malloc(sizeof(node));
printf("\nEnter info\n");
scanf("%d", &ptr->info);
ptr->next = fi;
if (fi == NULL && la == NULL)
{
fi = la = ptr;
}
else
{
fi = ptr;
}
}
void print()
{
ptr = fi;
while (ptr != NULL)
{
printf("|%d|-->", ptr->info);
ptr = ptr->next;
}
}
int main()
{
int i, ch;
do
{
printf("\nEnter your choice \n1.insert node\n2.view node\n3.exit\n");
scanf("%d", &ch);
if (ch == 1)
{
insertfi();
}
else if (ch == 2)
{
print();
}
else
printf("Exiting\n");
} while (ch != 3);
return 0;
}
I want to save space in a program that I am working on. This program creates nodes and joins them by storing address of the node in the next pointer in the structure. I want some way to not use the *next pointer to go to the next node - if I do this I will be able to save 2 bytes of memory.
I have been thinking if there is a way of contiguous allocation of address in dynamic memory allocation, so that I will store the address of the first node in a pointer then traverse or do whatever with it after tinkering with the address.
And how do addresses work - I mean how can I access them by inputting an address in the pointer and then printing it?
Here is a sample program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main ()
{
int a[10],i,*ptr;
for(i=0;i<10;i++)
{
a[i]=rand()%15;
}
for(i=0;i<10;i++)
{
printf("%6x\n",&a[i]);
}
ptr=&a[0];
printf("\n\n\n%d\n",*ptr);
}
It prints addresses in a contiguous manner but I don't know how to allocate them manually, like ptr=0022ff04 and then using `printf("%d",*ptr);
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#define max 20
typedef struct node
{
int info;
int row;
int colum;
struct node *next;
}node;
node *ptr,*fi=NULL,*la=NULL;
int r,c,i,j,sparse[max][max],decompmatx[max][max];
void makenode(int getrow,int getcolum,int getinfo)
{
ptr=(node*)malloc(sizeof(node));
ptr->next=NULL;
if(fi==NULL)
{
ptr->info=getinfo;
ptr->row=getrow;
ptr->colum=getcolum;
fi=la=ptr;
}
else
{
la->next=ptr;
la=ptr;
ptr->info=getinfo;
ptr->row=getrow;
ptr->colum=getcolum;
}
}
void decompress()
{
int temp;
temp=0;
ptr=fi;
while(ptr!=NULL)
{
decompmatx[ptr->row][ptr->colum]=ptr->info;
ptr=ptr->next;
}
for(i=0;i<=r-1;i++)
{
for (j=0;j<=c-1;j++)
{
if(decompmatx[i][j]>0)
{
}
else
decompmatx[i][j]=0;
}
}
printf("\nValue decompressed\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d ",decompmatx[i][j]);
if((temp%c)==0)
printf("\n");
else
temp++;
}
}
void showsparse()
{
int temp;
temp=0;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d ",sparse[i][j]);
if((temp%c)==0)
printf("\n");
else
temp++;
}
}
void print()
{
ptr=fi;
while(ptr!=NULL)
{
printf("|%d|%d||%d|--->",ptr->info,ptr->row,ptr->colum);
ptr=ptr->next;
}
}
int main()
{
int ch;
do
{
printf("\nInput choice\n1.Make Sparse Matrix\n2.compress Matrix\n3.Decompress
Matrix\n4.Exit...");
scanf("%d",&ch);
if(ch==1)
{
printf("Input the rows\n");
scanf("%d",&r);
printf("Enter colum\n");
scanf("%d",&c);
printf("Printing the %dx%d matrix\n",r,c);
for(i=0;i<=r-1;i++)
{
for (j=0;j<=c-1;j++)
{
sparse[i][j]=rand()%2;
}
}
showsparse();
printf("\nTotal Byte comsuming is =%d\n",r*c*2);
}
else if(ch==2)
{
for(i=0;i<=r-1;i++)
{
for (j=0;j<=c-1;j++)
{
if(sparse[i][j]!=0)
{
makenode(i,j,sparse[i][j]);
}
}
}
printf("Data compressed\n");
print();
printf("size of node =%d",sizeof(node));
}
else if(ch==3)
{
decompress();
}
else
{
printf("\nExiting..............\n");
}
}while(ch!=4);
getch();
}