梦⁠回⁣从前 2021-06-27 17:44 采纳率: 25%
浏览 15

c语言链表输不出来值

# include <stdio.h>
# include <stdlib.h>

struct node
{
	int val;
	struct node *next;
};

void linked_list_insert_after(struct node *head,int n);
int linked_list_traverse(struct node *head);

int main()
{
	struct node *head=(struct node *)malloc(sizeof(struct node));
	linked_list_insert_after(head,10);
	linked_list_traverse(head);
}

void linked_list_insert_after(struct node *head,int n)
{
	struct node *node , *end ;
	int i;
	printf("请输入值\n");
	for(i=0 ; i<n ; i++ )
	{
		node=(struct node *)malloc(sizeof(struct node));
		scanf("%d",&node->val);
		node->next=NULL;
		if(i==0)
		{
			head=node;
			head->next=NULL;
			end=head;
		}
		else
		{
			end->next=node;
			end=node;
		}
	}
	end->next=NULL;
}
int linked_list_traverse(struct node *head)
{
	struct node *p=head;
	if(p==NULL)
	{
		printf("此链表为空\n");
		return -1;
	}
	else
	{	
		printf("输出链表中的值\n");
		while(p!=NULL)
		{
			printf("%d\n",p->val);
			p=p->next;
		}
		return 0;
	}
}

输不出来值

  • 写回答

1条回答 默认 最新

  • superJJJKKKLLL 2021-11-11 15:52
    关注

    第25行,你把n变成n-1试试

    评论

报告相同问题?

悬赏问题

  • ¥15 Java与Hbase相关问题
  • ¥20 bash代码推送不上去 git fetch origin master #失败了
  • ¥15 LOL外服加入了反作弊系统,现在游戏录像rofl文件离线都无法打开
  • ¥15 在centos7安装conda
  • ¥15 c#调用yolo3 dll文件获取的数据对不上
  • ¥20 WPF 如何实现多语言,label 和cs(live Charts)中是否都能翻译
  • ¥15 STM32F103上电短路问题
  • ¥15 打开软件提示错误:failed to get wglChoosePixelFormatARB
  • ¥15 (标签-python|关键词-char)
  • ¥15 python+selenium,在新增时弹出了一个输入框