1
#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;
}

作業中のプログラムのスペースを節約したい。このプログラムは、ノードを作成し、構造体の次のポインターにノードのアドレスを格納することによってそれらを結合します。*next ポインターを使用して次のノードに移動しないようにする方法が必要です。これを行うと、2 バイトのメモリを節約できます。

動的メモリ割り当てでアドレスを連続して割り当てる方法があるかどうかを考えていたので、最初のノードのアドレスをポインターに格納し、アドレスをいじってからトラバースするか、何でもします。

また、アドレスはどのように機能しますか?つまり、ポインターにアドレスを入力してから印刷することで、アドレスにアクセスするにはどうすればよいでしょうか?

サンプルプログラムは次のとおりです。

#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);

}

アドレスを連続して出力しますが、ptr=0022ff04 のように手動でアドレスを割り当てる方法がわかりません。


#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();


}
4

1 に答える 1