在C中的另一个函数中插入列表元素
大家晚上好,我有下面的代码,我试图在列表中添加元素,然后显示它们。我使用一个名为smooth的函数,在该函数中,我找到9个数字之间的平均数,然后在该函数中,我使用在开头插入函数将它们添加到列表中,然后使用显示打印它们 当我在main(普通代码)中使用来自smooth函数的代码时,程序运行得很好。但是,当我试图通过平滑功能使其工作时,我没有要打印的列表。这意味着我的清单上什么都没有 任何帮助都是非常感激的 先谢谢你在C中的另一个函数中插入列表元素,c,windows,C,Windows,大家晚上好,我有下面的代码,我试图在列表中添加元素,然后显示它们。我使用一个名为smooth的函数,在该函数中,我找到9个数字之间的平均数,然后在该函数中,我使用在开头插入函数将它们添加到列表中,然后使用显示打印它们 当我在main(普通代码)中使用来自smooth函数的代码时,程序运行得很好。但是,当我试图通过平滑功能使其工作时,我没有要打印的列表。这意味着我的清单上什么都没有 任何帮助都是非常感激的 先谢谢你 #include <stdio.h> #include <std
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define M 6
#define N 6
struct node {
int data;
struct node *next;
};
typedef struct node LIST_NODE;//setting up aliases
typedef struct node *LIST_PTR;
struct node *head = NULL;
void init_list(LIST_PTR *head)//arxikopisi tis listas
{
*head=NULL;
}
void free_list(LIST_PTR *head)//free memory of each node.
{
LIST_PTR ptr;//temporary pointer variable
while(*head!=NULL)//while head is not empty(NULL)
{
ptr=*head;//set temporary as head
*head=(*head)->next;//go to next
free(ptr);//free memory
}
}
void insert_at_beginning(LIST_PTR *head,float value)
{
LIST_PTR newnode;// new node
newnode=(LIST_NODE *)malloc(sizeof(LIST_NODE));//memory allocation forthe new node
if(!newnode)//check if memory alocation has been successfull
{
printf("memory allocation failed");
}
newnode->data=value;//insert average value from smooth function
newnode->next=*head;//set next of node to where head is pointing
*head=newnode;//head will now point at new node
}
void display(LIST_PTR *head)
{
LIST_PTR current = *head;//temporary pointer variable as to where head is pointing
while(current !=NULL)//while head is not empty(NULL)
{
printf("%d -> ",current->data);//print data
current = current->next;//goto next node
}
if(current==NULL)
printf("list empty");
}
void smooth(int K[M][N])
{
int q=0,h,i,j,l=0;
int repi = 3;
int repj = 3;
float sum;
float avg;
/*test 1 */
printf("\n-----------\n");
for(h=0;h<(N-2)*(N-2);h++){
sum = 0;
avg = 0,0;
for(i=q;i<repi;i++){
for(j=l;j<repj;j++)
{
printf("\t%d " ,K[i][j]);
sum+=K[i][j];
avg = round(sum/9);
}
printf("\n");
}
insert_at_beginning(&head,avg);
printf("-------%d---avg = %.0f------\n",h,avg);
if(l<3){
l++;
repj++;
}
else
{
l=0;
q+=1;
repi+=1;
repj=3;
}
}
}
int main(){
system("chcp 1253>nul");
int K[M][N]={60,68,61,63,59,61,156,161,162,159,156,158,162,159,170,163,165,166,234,255,157,234,211,244,245,232,225,253,200,205,67,66,58,65,66,71};
int elem;
int i,j,r=1;
LIST_PTR head;
init_list(&head);
/*test array*/
printf("K = [ ");
for(i=0;i<M;i++){
for(j=0;j<N;j++){
printf("\t%d " ,K[i][j]);
}
printf("\n");
}
printf("]");
smooth(K);
display(&head);
free_list(&head);
return 0;
}
#包括
#包括
#包括
#定义m6
#定义n6
结构节点{
int数据;
结构节点*下一步;
};
类型定义结构节点列表\u节点//设置别名
typedef结构节点*列表\u PTR;
结构节点*head=NULL;
void init_list(list_PTR*head)//arxikopisi tis listas
{
*head=NULL;
}
void free_list(list_PTR*head)//释放每个节点的内存。
{
LIST_PTR PTR;//临时指针变量
while(*head!=NULL)//while head不为空(NULL)
{
ptr=*head;//将临时设置为head
*head=(*head)->next;//转到next
空闲(ptr);//空闲内存
}
}
在开头插入空白(列表头,浮动值)
{
LIST_PTR newnode;//新节点
newnode=(LIST_NODE*)malloc(sizeof(LIST_NODE));//新节点的内存分配
if(!newnode)//检查内存分配是否已成功
{
printf(“内存分配失败”);
}
newnode->data=value;//从平滑函数插入平均值
newnode->next=*head;//将节点的next设置为head指向的位置
*head=newnode;//head现在将指向新节点
}
无效显示(列表头)
{
LIST_PTR current=*head;//关于head指向的位置的临时指针变量
while(当前!=NULL)//while head不为空(NULL)
{
printf(“%d->”,当前->数据);//打印数据
当前=当前->下一步;//转到下一个节点
}
如果(当前==NULL)
printf(“列表为空”);
}
虚空平滑(整数K[M][N])
{
int q=0,h,i,j,l=0;
int repi=3;
int-repj=3;
浮点数;
浮动平均值;
/*测试1*/
printf(“\n------------\n”);
对于(h=0;h您有两个同名的变量。一个struct node*head=NULL;
是全局变量。另一个LIST\PTR head;
是主函数的本地变量。查看放置变量的位置,所有变量都将被清除。调试器显示?没有问题,只是列表是空的!system(“chcp 1253>nul”)告诉我这是Windows操作系统。请更新标签。代码不编译。提供适当的例子。我使用DEV C++,并且确实编译,确保您在顶部添加库。