qq_45794976
qq_45794976
2021-01-24 21:35

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

  • c++
  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条回答