qq_45794976 2021-01-24 21:35 采纳率: 100%
浏览 22
已采纳

数据结构链表,完全对应书上,但在vs2019上无法运行。

  1. #pragma once
    #ifndef _clist_
    #define _clist_
    
    struct node;
    typedef struct head* phead;
    typedef struct node* pnode;
    
    struct head
    {
    	int length;
    	pnode next;
    };
    struct node
    {
    	int data;
    	pnode next;
    };
    phead clistcreate();
    int getlength(phead ph);
    int isempty(phead ph);
    int clistinsert(phead ph, int pos, int val);
    void print(phead ph);
    
    
    #endif // !_clist_//头文件
    
    #include"标头.h"
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
    	int m, n;
    	printf("请输入约瑟夫环的总人数!\n");
    	scanf_s("%d", &m);
    	printf("请输入需要踢出的人数!\n");
    	scanf_s("%d", &n);
    	phead ph = NULL;
    	ph = clistcreate();
    	if (ph == NULL)
    	{
    		printf("约瑟夫环创建失败!\n");
    		return 0;
    	}
    
    	for (int i = m; i > 0; i++)
    	{
    		clistinsert(ph, 0, i);
    	}
    	print(ph);
    	printf("被踢顺序:\n");
    	pnode node = ph->next;
    	while (node->next != node)
    	{
    		for (int i = 1; i < n - 1; i++)
    		{
    			node = node->next;
    		}
    		pnode ptmp = node->next;
    		if (ptmp == ph->next)
    		{
    			ph->next = ptmp->next;
    			node->next = ptmp -> next;
    			printf("%d", ptmp->data);
    			free(ptmp);
    			ph->length--;
    		}
    		else
    		{
    			node->next = ptmp->next;
    			printf("%d", ptmp->data);
    			free(ptmp);
    			ph->length--;
    		}
    		node = node->next;
    
    	}
    	node->next = node;
    	printf("\n");
    	printf("链表中最后留下的是:");
    	print(ph);
    	system("pause");
    	return 0;
    
    }//函数
    
    #include"标头.h"
    #include <stdio.h>
    #include<stdlib.h>
    
    phead clistcreate()
    {
    	phead ph = (phead)malloc(sizeof(struct head));
    	if (ph == NULL)
    		printf("头节点分配失败!");
    	ph->length = 0;
    	ph->next = NULL;
    	return ph;
    }
    int isempty(phead ph)
    {
    	if (ph == NULL)
    		printf("传入的链表有误!");
    	if (ph->length == 0)
    		return 1;
    	else
    		return 0;
    }
    
    int clistinsert(phead ph, int pos, int val)
    {
    	if (ph == NULL || pos<0 || pos>ph->length)
    	{
    		printf("插入元素时,元素传入有误!");
    	}
    	pnode pval;
    	pval = (pnode)malloc(sizeof(node));
    	pval = NULL;
    	pval->data = val;
    	if (isempty)
    	{
    		ph->next = pval;
    		pval->next = pval;
    	}
    	else
    	{
    		pnode prear = ph->next;
    		if (pos == 0)
    		{
    			while (prear->next != ph->next)
    			{
    				prear = prear->next;
    			}
    			pval->next = ph->next;
    			ph->next = pval;
    			prear->next = pval;
    		}
    		else
    		{
    			pnode pcur = ph->next;
    			for (int i = 1; i < pos; i++)
    			{
    				pcur = pcur->next;
    			}
    			pval->next = pcur->next;
    			pcur->next = pval;
    		}
    	}
    	ph->length++;
    	return 1;
    }
    
    void print(phead ph)
    {
    	if (ph == NULL || ph->length == 0)
    	{
    		printf("参数传入时有误!");
    	}
    	pnode ptmp = ph->next;
    
    	for (int i = 0; i < ph->length; i++)
    	{
    		printf("%d", ptmp->data);
    		ptmp = ptmp->next;
    	}
    	printf("\n");
    }
    

vs2019显示问题出在插入函数中的pval->data=val;

引发了异常: 写入访问权限冲突。
**pval** 是 nullptr。

  • 写回答

1条回答 默认 最新

  • 泡视界 2021-01-25 10:28
    关注

    书也是人写的,人非圣贤,孰能无过。

    自己改代码就好,这就是插入函数运行 pval->data=val; 的时候 pval是空指针,所欲pval->xxxx会报错,引起程序崩溃。

    	
    	pnode pval;
    	pval = (pnode)malloc(sizeof(node));
    	pval = NULL;     //这一句将pavl设置成空指针了,删了此句就行
    	pval->data = val;

    如上面注释的一样,把 pval = NULL删了就行。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应