#include <stdio.h>
#include <malloc.h>
# define null 0
typedef struct stacknode
{ int data;
struct stacknode *next;
}stacklink;
typedef struct
{
stacklink *top;
int stacksize;
}stackk;
void initlink(stackk *s)
{s->top=(stacklink *)malloc(sizeof(stacklink));
s->top->data=0;
s->top->next=null;
}
int poplink(stackk *s)
{
stacklink *p;int v;
if(s->top->next==null) printf("the stackis empty\n");
else
{
v=s->top->next->data;
p=s->top->next;
s->top=s->top->next;
free(p);
}
return v;
}
void pushlink(stackk *s,int x)
{stacklink *p;
p=(stacklink *)malloc(sizeof(stacklink));
p->data=x;
p->next=s->top->next;
s->top->next=p;
}
int gettop(stackk *s)
{int e;
if(s==null) printf("the stack is empty!\n");
e=s->top->next->data;
return e;
}
void display(stackk *s)
{stacklink *p;
p=s->top->next;
printf("display the stacklink:\n");
if (s->top=null) printf("the stacklink is empty!\n");
else {while(p)
{printf("->%d",p->data);
p=p->next;}
}
}
void main()
{int n,k,i,select,h,x1,x2;
stackk *p ; initlink(p);
printf("create a empty stacklink!\n");
printf("input a stacklink length:\n");
scanf("%d",&n);
for (i=1;i<=n;i++)
{printf("input a stacklink value:\n");
scanf("%d",&k);
pushlink(p,k);
}
printf("select 1:display()\n");
printf("select 2:pushlink()\n");
printf("select 3:poplink()\n");
printf("select 4:gettop()\n");
printf("input a your select(1-4):\n");
scanf("%d",&select);
switch(select)
{case 1:{display(p);break;}
case 2:{printf("input a push a value :\n");
scanf("%d",&h);
pushlink(p,h);
display(p);
break;}
case 3:{x1=poplink(p);printf("x1->%d\n",x1);
display(p);
break;}
case 4: {x2=gettop(p);printf("x2->%d",x2);
break;}
}
}
希望那个大哥帮个忙! 这个程序 在C-Free `3.5里能够通过编译``并结果正确,但在vc中那么通不过啊主要问题在了
这里
void initlink(stackk *s)
{s->top=(stacklink *)malloc(sizeof(stacklink));
s->top->data=0;
s->top->next=null;
}
通过调式出现了`内存分配有问题
Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
First-chance exception in huiwenpanduan.exe: 0xC0000005: Access Violation.