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删了就行。

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

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况