C语言:0xC0000005: 读取位置 0x00000000 时发生访问冲突。求解

不报错运行时弹出

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<time.h>
#define Elem int
#define MAXSIZE 1000
#define bool int
#define true 1
#define false 0

typedef struct tagnode
{
Elem data;
struct tagnode *next;
}Node;

typedef struct
{
Node *top;
int length;
}Stack;

bool Stackcreate(Stack &S,int maxsize)
{
S.length=0;
S.top=NULL;
return true;
}

bool Stackpush(Stack &S,const Elem &it)
{
Node *temp;
temp=(Node*)malloc(sizeof(Node));
if(temp==NULL)
return false;
temp->data=it;
temp->next=S.top;
S.top=temp;
S.length++;
return true;
}

bool Stackinverse(Stack &S)
{
Elem elem;
Node *temp;
temp=(Node*)malloc(sizeof(Node));
while(S.top->data)
{
elem=S.top->data;
printf("%d ",elem);
temp=S.top->next;
S.top=temp;
}
return true;
}

bool Stackclear(Stack &S)
{
Node *temp;
while(S.top!=0)
{
temp=S.top;
S.top=S.top->next;
free(temp);
}
S.length=0;
return true;
}
void main()
{
Elem a;
int na;
Stack A;
printf("请输入结点数:");
scanf("%d",&na);
srand((unsigned)time(0));
Stackcreate(A,na);
printf("该链表内容为:\n");
for(int i=0;i<na;i++)
{
a=rand()%100;
Stackpush(A,a);
printf("%d ",a);
}
printf("\n其倒序为:\n");
Stackinverse(A);
Stackclear(A);
system("pause");
}

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

#define Elem int
#define MAXSIZE 1000
#define bool int
#define true 1
#define false 0

typedef struct tagnode {
Elem data;
struct tagnode *next;
}Node;

typedef struct {
Node *top;
int length;
}Stack;

bool StackCreate(Stack &S,int maxsize) {
S.length = 0;
S.top = NULL;
return true;
}

bool StackPush(Stack &S,const Elem &it) {
Node *temp;
temp = (Node *)malloc(sizeof(Node));
if(temp == NULL) return false;
temp->data = it;
temp->next = S.top;
S.top = temp;
S.length++;
return true;
}

bool StackPop(Stack &S,Elem *elem) {
Node *temp;
if(S.top) {
*elem = S.top->data;
temp = S.top;
S.top = S.top->next;
--S.length;
free(temp);
return true;
}
return false;
}

void StackInverse(Stack &S) {
Node *p = NULL,*q = S.top,*r;
while(q) {
r = q->next;
q->next = p;
p = q;
q = r;
}
S.top = p;
}

void StackAll_over(Stack &S) {
Node *p = S.top;
while(p) {
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}

bool StackClear(Stack &S) {
Node *temp;
while(S.top != 0) {
temp = S.top;
S.top = S.top->next;
free(temp);
}
S.length = 0;
return true;
}

void main() {
Elem a;
int na;
Stack A;
printf("请输入结点数:");
scanf("%d",&na);
srand((unsigned)time(0));
StackCreate(A,na);
printf("该链表内容为:\n");
for(int i = 0;i < na;i++) {
a = rand()%100;
StackPush(A,a);
}
printf("该链表内容为:\n");
StackAll_over(A);
StackInverse(A);
printf("\n其倒序为:\n");
StackAll_over(A);
StackClear(A);
system("pause");
}

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网